JSP中文传到Spring MVC controller乱码问题分析

表单提交controller获得中文参数后乱码解决方案

注意:  jsp页面编码设置为UTF-8

form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果

[html]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
[html]  view plain  copy
  1. <form action="${ctx}/user/addUser" name="userForm" method="post">    

修改web.xml,增加编码过滤器,如下(注意,需要设置forceEncoding参数值为true)

[html]  view plain  copy
  1. <filter>  
  2.         <filter-name>characterEncodingFilter</filter-name>  
  3.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  4.         <init-param>  
  5.             <param-name>encoding</param-name>  
  6.             <param-value>UTF-8</param-value>  
  7.         </init-param>  
  8.         <init-param>  
  9.             <param-name>forceEncoding</param-name>  
  10.             <param-value>true</param-value>  
  11.         </init-param>  
  12.     </filter>  
  13.     <filter-mapping>  
  14.         <filter-name>characterEncodingFilter</filter-name>  
  15.         <url-pattern>/*</url-pattern>  
  16.     </filter-mapping>  





2:表单提交controller获得中文参数后正常显示控制台,然后保存数据库出现乱码
注意:

数据库编码是否支持中文

数据库表和表字段是否正确


在配置连接数据库的参数设置修改:

[html]  view plain  copy
  1. <property name="url" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8"></property>  

你可能感兴趣的:(spring,jsp,mvc,controller,乱码问题分析)