Because the Struts framework directly sends the form data to the actionform, The httprequestservlet is not
Setcharacterencoding, so the default is to follow the ISO-8859-1 (see Tomcat
Protected void in org. Apache. Catalina. connector. httprequestbase in the source code
Parseparameters () method ),
The solution is to encode the request before the form is submitted to actionform.
The first method is to write a filter to filter all requests.
Filter code:
Package JP. co. Ricoh. gtis. Others. profile. filters; Import java. Io. ioexception; Import javax. servlet. filter; Import javax. servlet. filterchain; Import javax. servlet. filterconfig; Import javax. servlet. servletexception; Import javax. servlet. servletrequest; Import javax. servlet. servletresponse; Public class setcharacterencodingfilter implements filter { Private string encoding; Public void Init (filterconfig) throws servletexception { // Todo auto-generated method stub This. Encoding = filterconfig. getinitparameter ("encoding "); } Public void dofilter (servletrequest request, servletresponse response, Filterchain chain) throws ioexception, servletexception { // Todo auto-generated method stub Request. setcharacterencoding (this. Encoding ); Chain. dofilter (request, response ); } Public void destroy (){ // Todo auto-generated method stub } } |
Configuration File web. xml
<Filter> <Filter-Name> setcharacterencodingfilter </filter-Name> <Filter-class> JP. co. Ricoh. gtis. Others. profile. Filters. setcharacterencodingfilter </filter-class> <Init-param> <Param-Name> encoding </param-Name> <Param-value> UTF-8 </param-value> </Init-param> </Filter><Filter-mapping> <Filter-Name> setcharacterencodingfilter </filter-Name> <URL-pattern>/* </url-pattern> </Filter-mapping> |
The second method is to replace the default controller org. Apache. Struts. Action. actionservlet.
Subclass code:
Package JP. co. Ricoh. gtis. Others. profile. controllers; Import java. Io. ioexception; Import javax. servlet. servletexception; Import javax. servlet. http. httpservletrequest; Import javax. servlet. http. httpservletresponse; Import org. Apache. Struts. Action. actionservlet; Public class setencodingactionservlet extends actionservlet { Protected void process (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception { // Todo auto-generated method stub String encoding = getinitparameter ("encoding "); Request. setcharacterencoding (encoding ); Super. Process (request, response ); } } |
Configuration File web. xml
<Servlet> <Servlet-Name> testaction </servlet-Name> <Servlet-class> JP. co. Ricoh. gtis. Others. profile. controllers. setencodingactionservlet </servlet-class> <Init-param> <Param-Name> config </param-Name> <Param-value>/WEB-INF/struts-config.xml </param-value> </Init-param> <Init-param> <Param-Name> encoding </param-Name> <Param-value> UTF-8 </param-value> </Init-param> <Load-on-startup> 2 </load-on-startup> </Servlet><Servlet-mapping> <Servlet-Name> testaction </servlet-Name> <URL-pattern> *. testdo </url-pattern> </Servlet-mapping> |
In this example, all data requested through *. testdo is encoded by the value set by the encoding parameter.