“在计算机科学中,没有谁是权威,最权威的是计算机,结果就让计算机去说吧”,遇到不清楚的问题 动手实践吧!
李刚说:struts.i18n.encoding对于处理中文请求参数非常有用。对于获取中文请求参数,应该将该属性设置为gbk或db2312,当该参数为gbk时,相当于调用HttpServletRequest的setCharacterEncoding()
ahuaxuan说:struts.i18n.encoding是指定response中返回流的编码方式,明确指出struts.i18n.encoding参数没有用于HttpServletRequest的setCharacterEncoding()方法。<? xml version="1.0" encoding="UTF-8" ?> < web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > < filter > < filter-name > struts2 </ filter-name > < filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class > </ filter > < filter-mapping > < filter-name > struts2 </ filter-name > < url-pattern > /* </ url-pattern > </ filter-mapping > < welcome-file-list > < welcome-file > index.jsp </ welcome-file > </ welcome-file-list > </ web-app >
<? xml version="1.0" encoding="UTF-8" ?> <! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > < struts > < constant name ="struts.configuration.xml.reload" value ="true" /> < constant name ="struts.devMode" value ="true" /> < constant name ="struts.i18n.encoding" value ="GBK" /> < package name ="sohu" namespace ="/test" extends ="struts-default" > < action name ="hello" class ="com.sohu.game.HelloAction" > < result > /hello.jsp </ result > </ action > </ package > </ struts >
package com.sohu.game; import org.apache.struts2.ServletActionContext; public class HelloAction { private String encoding; public String getEncoding() { return encoding; } public String execute() throws Exception{ this .encoding = ServletActionContext.getRequest().getCharacterEncoding(); return " success " ; } }
<% @ page language = " java " pageEncoding = " GBK " %> <% @ taglib uri = " /struts-tags " prefix = " s " %> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > < html > < head > < title > hello </ title > < meta http-equiv ="pragma" content ="no-cache" > < meta http-equiv ="cache-control" content ="no-cache" > < meta http-equiv ="expires" content ="0" > </ head > < body >< h3 > request.getCharacterEncoding()的内容为: < s:property value ="encoding" /> </ h3 ></ body > </ html >
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; ServletContext servletContext = getServletContext(); String timerKey = " FilterDispatcher_doFilter: " ; try { UtilTimerStack.push(timerKey); request = prepareDispatcherAndWrapRequest(request, response); // 这里是重点 // 这里省略后面的代码 } finally { try { ActionContextCleanUp.cleanUp(req); } finally { UtilTimerStack.pop(timerKey); } } } }
protected HttpServletRequest prepareDispatcherAndWrapRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException { Dispatcher du = Dispatcher.getInstance(); if (du == null ) { Dispatcher.setInstance(dispatcher); dispatcher.prepare(request, response); // 这里是重点 } else { dispatcher = du; } // 省略了一些代码 return request; }
public void prepare(HttpServletRequest request, HttpServletResponse response) { String encoding = null ; if (defaultEncoding != null ) { encoding = defaultEncoding; } // 省略了一些代码 if (encoding != null ) { try { request.setCharacterEncoding(encoding); // 这里是重点 } catch (Exception e) { LOG.error( " Error setting character encoding to ' " + encoding + " ' - ignoring. " , e); } } // 省略了一些代码 }
@Inject(StrutsConstants.STRUTS_I18N_ENCODING) public static void setDefaultEncoding(String val) { defaultEncoding = val; }