编码过滤器,struts在使用html:form标签时工作 [英] encoding filter , struts working just when using html:form tag
问题描述
我面对这个问题。我有一个过滤器,根据过滤器的配置(例如,到UTF-8)设置请求的字符编码。这与使用struts html:form标签编码的表单配合使用。但是,如果我使用普通的HTML表单标签,数据将无法正确编码。
这是web.xml中的过滤器定义:
< filter>
< filter-name>编码过滤器< / filter-name>
< filter-class> EncodingFilter< / filter-class>
< init-param>
< param-name> encoding< / param-name>
< param-value> UTF-8< / param-value>
< / init-param>
< / filter>
< filter-mapping>
< filter-name>编码过滤器< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
这是过滤器:
public class EncodingFilter实现javax.servlet.Filter {
private String encoding;
public void init(FilterConfig filterConfig)throws ServletException {
this.encoding = filterConfig.getInitParameter(encoding);
}
public void doFilter(ServletRequest请求,ServletResponse响应,FilterChain filterChain)throws IOException,ServletException {
request.setCharacterEncoding(encoding);
filterChain.doFilter(request,response);
}
public void destroy(){
}
}
如果您使用Struts标签< html:form>
并省略它默认为POST的METHOD属性。
如果您使用标准HTML < form>
并省略METHOD属性,那么它将默认为GET。
Tomcat将以不同的方式处理您的POST和GET参数:
POST:将使用过滤器。请注意,如果客户端尚未指定请求字符编码(您的过滤器始终将其设置为UTF-8),则应该只设置请求字符编码。 Tomcat附带一个过滤器SetCharacterEncodingFilter.java来执行此操作。
GET :Tomcat将使用ISO-8859-1作为默认字符编码。有两种方式来指定GET参数的解释方式:
- 将server.xml中的元素上的URIEncoding属性设置为特定的(例如URIEncoding =UTF-8)。
- 将server.xml中的元素的useBodyEncodingForURI属性设置为true。这将导致连接器使用请求正文的编码GET参数。
This is the filter definition in the web.xml:
<filter>
<filter-name>Encoding Filter</filter-name>
<filter-class>EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Here's the filter :
public class EncodingFilter implements javax.servlet.Filter {
private String encoding;
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
filterChain.doFilter(request, response);
}
public void destroy() {
}
}
If you use a Struts tag <html:form>
and omit the METHOD attribute it defaults to POST.
If you use a standard HTML <form>
and omit the METHOD attribute it defaults to GET.
Tomcat will process your POST and GET parameters differently:
POST: your filter will be used. Note that you should really only set the request character encoding if it has not been specified by the client (your filter is always setting it to UTF-8). Tomcat comes with a filter SetCharacterEncodingFilter.java that does this.
GET: Tomcat will use ISO-8859-1 as the default character encoding. There are two ways to specify how GET parameters are interpreted:
- Set the URIEncoding attribute on the element in server.xml to something specific (e.g. URIEncoding="UTF-8").
- Set the useBodyEncodingForURI attribute on the element in server.xml to true. This will cause the Connector to use the request body's encoding for GET parameters.
This is all in: http://wiki.apache.org/tomcat/FAQ/CharacterEncoding
这篇关于编码过滤器,struts在使用html:form标签时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!