我在jsp中有一个带有发送按钮的表单:
<form role="form" action="/admin/newUserTest/" method="post">
<input type="text" class="form-control" id="firstname" name="firstname">
<input type="text" class="form-control" id="lastname" name="lastname">
<input type ="send" value="Send">
</form>
和控制器:
@RequestMapping(value="/newUserTest", method = RequestMethod.POST)
public ModelAndView newUserTest(@RequestParam("firstname")String firstname,@RequestParam("lastname")String lastname){
ModelAndView model = new ModelAndView();
System.out.println(firstname);
System.out.println(lastname);
...
我在表(mysql)中写的名字和姓氏。当method =“get”时,它写入正确,但是当method =“post”时,它会写出错误的符号,如ÑзÐμÑ3。并且控制台写出符号?????μ?? 3。 我该如何解决这个问题?
答案 0 :(得分:0)
我解决了我的问题,添加到web.xml:
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
答案 1 :(得分:0)
这是由于spring无法使用正确的编码。 确保在web.xml中注册CharacterEncodingFilter
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果您使用的是基于Java类的配置。使用
messageSource.setDefaultEncoding("UTF-8");
希望有助于解决问题。