struts的ActionForm不能存放中文?
热门软件下载:
我写了一个表担,
<form-bean name="providerform"type="...">
<action path="/add.do">
type="...action"
name="providerform"
scope="request"
</action>
在表单中,
<html:form action="add.do">
..
bean一个name属性,要用<html:text property="name" />
来读取中文
...
</html.form>
在我的action类中可以读到name属性,但只限于拼音,如果是中文就会显示乱麻
谢谢,谁告诉我该怎么办!!
推荐阅读
我也遇到同样的问题,请哪位高手回答一下吧
如何 过滤器啊, ?请指教啊, 。。
在web.xml中加上
<filter>
<filter-name>set character encoding</filter-name>
<filter-class>com.huahang.tj.struts.filters.setcharacterencodingfilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>set character encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
这里会涉及一个bean,源码如下:
/*
* xp forum
*
* copyright (c) 2002-2003 redsoft group. all rights reserved.
*
*/
package com.huahang.tj.struts.filters;
import javax.servlet.*;
import java.io.ioexception;
/**
* <p>filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - the character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. this parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - if set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectencoding()</code> method is set. if set to "false,
* <code>selectencoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. by default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectencoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>accept-language</code>
* and <code>user-agent</code> headers, or a value stashed in the current
* users session.</p>
*
* @author <a href="mailto:jwtronics@yahoo.com">john wong</a>
*
* @version $id: setcharacterencodingfilter.java,v 1.1 2002/04/10 13:59:27 johnwong exp $
*/
public class setcharacterencodingfilter implements filter {
// ----------------------------------------------------- instance variables
/**
* the default character encoding to set for requests that pass through
* this filter.
*/
protected string encoding = null;
/**
* the filter configuration object we are associated with. if this value
* is null, this filter instance is not currently configured.
*/
protected filterconfig filterconfig = null;
/**
* should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- public methods
/**
* take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterconfig = null;
}
/**
* select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request the servlet request we are processing
* @param result the servlet response we are creating
* @param chain the filter chain we are processing
*
* @exception ioexception if an input/output error occurs
* @exception servletexception if a servlet error occurs
*/
public void dofilter(servletrequest request, servletresponse response,
filterchain chain)
throws ioexception, servletexception {
// conditionally select and set the character encoding to be used
if (ignore || (request.getcharacterencoding() == null)) {
string encoding = selectencoding(request);
if (encoding != null)
request.setcharacterencoding(encoding);
}
// pass control on to the next filter
chain.dofilter(request, response);
}
/**
* place this filter into service.
*
* @param filterconfig the filter configuration object
*/
public void init(filterconfig filterconfig) throws servletexception {
this.filterconfig = filterconfig;
this.encoding = filterconfig.getinitparameter("encoding");
string value = filterconfig.getinitparameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsignorecase("true"))
this.ignore = true;
else if (value.equalsignorecase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// ------------------------------------------------------ protected methods
/**
* select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. if no character encoding should be set, return
* <code>null</code>.
* <p>
* the default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request the servlet request we are processing
*/
protected string selectencoding(servletrequest request) {
return (this.encoding);
}
}//eoc
加上这个后,在action中就可以直接从form中接收gb2312编码的数据了,返回时自然也是gb2312了。
但是这个好像需要servlet 2.2以上的容器
只有servlet2.3及更高版本才支持filter,但并不是在filter中声明字符集,就可以完全解决字符问题。有时,你仍需在页面中声明字符集。
.
相关评论