UTF-8编码,JSP,jQuery,Spring的问题 [英] Problems with UTF-8 encoding, JSP, jQuery, Spring
问题描述
我有一个web应用程序spring,jsp和jquery在apache tomcat 6,一个jsp页面有一个形式,发送数据与ajax调用做为whit jquery,到我的后端的一个Spring MultiActionController。
问题是在表单输入中的UTF-8字符串。
我已经做了以下事情: p>
在我的HTML上:
<?xml version =1.0 encoding =UTF-8?>
<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">
<%@ taglib prefix =springuri =http://www.springframework.org/tags%>
<%@ page contentType =text / html; charset = utf-8%>
<%@ page language =javapageEncoding =utf-8%>
< html>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< head>
。
。
在jquery ajax调用上:
$。ajaxSetup({scriptCharset:utf-8,contentType:application / x-www-form-urlencoded; charset = UTF-8});
$ .ajax(
{
type:GET,
url:./saveData.action,
contentType:charset = utf-8 ,
数据:{name:$('#name')。val(),...
$ b b
在tomcat server.xml上:
< Connector connectionTimeout =20000port =8080URIEncoding = UTF-8protocol =HTTP / 1.1redirectPort =8443/>
MultiActionController
public ModelAndView saveData(HttpServletRequest请求,HttpServletResponse响应)
throws ServletException,IOException
{
request.setCharacterEncoding(UTF-8);
String name = request.getParameter(name);
所以,如果在name参数中我这样:
María
在后端我得到
我已经尝试过所有我读过的东西,我不知道是什么错误,感谢任何帮助:)
一切看起来都很好,直到你在变量中获得参数。它只是你的后端,仍然需要配置为使用UTF-8。例如, System.out.println()
或者您要将检索到的参数发送到的记录器也应该使用UTF-8。或者,存储检索参数的数据库也应使用UTF-8。或者与该数据库交互的JDBC驱动程序。或者正在向其中写入数据的文本文件。
另请参阅:
无关,您的具体问题
request.setCharacterEncoding(UTF-8);
应在之前执行。并且每当Spring在执行操作之前确定请求体时,这将失败(注意,这一行对GET请求完全没有影响)。 Spring有一个 CharacterEncodingFilter
这正是这样。在 web.xml
中注册:
< filter&
< filter-name> characterEncodingFilter< / 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> characterEncodingFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
I have a web app with spring,jsp and jquery in a apache tomcat 6, one jsp page has a form that send the data with a ajax call made whit jquery, to a Spring MultiActionController on my back end.
The problem is with the UTF-8 strings in the form inputs.
I already did the following things:
On my HTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page language="java" pageEncoding="utf-8"%>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
.
.
On the jquery ajax call:
$.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8" });
$.ajax(
{
type: "GET",
url: "./saveData.action",
contentType: "charset=utf-8",
data: { name: $('#name').val(),...
On the tomcat server.xml:
<Connector connectionTimeout="20000" port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" redirectPort="8443"/>
On the MultiActionController
public ModelAndView saveData(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
So if in the name parameter I put something like this:
María
in the backend I get
MarÃa.
I already tried all the things that I read about, and I don't know what is the error, thanks for any help :)
Everything looks fine until the point that you get the parameter in a variable. It's just your backend which still needs to be configured to use UTF-8. For example, the System.out.println()
or the logger where you're sending the retrieved parameter to should also use UTF-8. Or the database where you're storing the retrieved parameter should also use UTF-8. Or the JDBC driver which is interacting with that DB. Or the text file where you're writing the data to. Etcetera.
See also:
Unrelated to your concrete problem, the line
request.setCharacterEncoding("UTF-8");
should be executed before Spring kicks in. It sets the POST request encoding and this will fail whenever Spring determines the request body before executing the action (note that this line has totally no effect on GET requests). Spring has a CharacterEncodingFilter
which does exactly that. Register this in your web.xml
:
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这篇关于UTF-8编码,JSP,jQuery,Spring的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!