过滤器代码: public class EncodingFilter implements Filter { /** * 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; /** * Take this filter out of service. */ public void destroy() { this.encoding = null; this.filterConfig = null; } /** *选择并设置这个request要使用的字符编码 */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) { request.setCharacterEncoding(encoding); } } chain.doFilter(request, response); } /** *初始化过滤器参数 */ 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 String selectEncoding(ServletRequest request) { return (this.encoding); }}在web.xml中相应的配置
<html:form action="/dataRecord">
<html:submit property="method">
<bean:message key="button.add">
</html:submit>
<html:submit property="method">
<bean:message key="button.view">
</html:submit>
<html:submit property="method">
<bean:message key="button.update">
</html:submit>
<html:submit property="method">
<bean:message key="button.delete">
</html:submit>
</html:form>
其中button.add,button.view等是在resource.properties文件中写的键值对的键,也就是key,对应的值可以是任何文字,包括中文。
eg
button.add="增加"
struts就是用这一招来解决国际化问题的。
至于你想通过不同的提交按钮来响应不同的操作,应该使用LookUpDispatchAction来解决,需要注意的是所有的<html:submit>标签的property属性必须相同,而且需要在struts-config.xml中配置,
eg:
<action
path="/dataRecord"
type="app.recordDispatchAction"
name="dataForm"
scope="request"
input="/data.jsp"
parameter="method"/>注意parameter与<html:submit>标签的property属性必须相同
这样LookUpDispatchAction才知道是同一提交,然后LookUpDispatchAction会根据key值来确定相应的操作。详细情况参见:Struts in action
再来一个传统的,设一个flag hidde的,按钮按下时,script改变它的值,
到了后台判断时那个按钮按下。
1、在ApplicationResource中加入button.add="增加"是不可以的,只能用button.add=增加;
2、用button.add=增加也存在问题,因为如果用的是LookUpDispatchAction,点击相应的Button后出现NullPointerException,但是用E文可以,比如button.add=Add;不知道怎么解决,高手请回复。
问题大概是这样:
在使用LookupDispatchAction的情况下--jsp页面中是这样:
<html:submit property="method">
<bean:message key="button.add">
</html:submit>
……//多个submit
--Action中有一个map
protected Map getKeyMethodMap()
{
Map map = new HashMap();
map.put("button.add", "add");
......//多个
return map;
}--用英文,没有任何问题
--用中文的转码,连不到相应的Action方法--猜测:struts中从页面中的submit中得到button.add的具体的值“\u589e\u52a0”与map的中取到button.add的值进行比较的,因为转码格式不同,所以找不要匹配的方法,
解决:用map中的转码方式处理中文得到统一,可是现在不知道map的转码格式。
问题存在:
1、用ASCII将汉字写给一个Button:解决了Button中文显示问题,但是当提交一个Button的时候,会将Button.value(中文)写入request中,这个时候存入的是一个中文的编码,这个编码显然不是中文ASCII,所以这个值最后和LookupDispatchActin中的Map进行对比并匹配导向相应方法的时候,出错。问题解决:
加过滤器,对request中的值进行合理的编码,成功!
public class EncodingFilter
implements Filter
{
/**
* 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; /**
* Take this filter out of service.
*/
public void destroy()
{
this.encoding = null;
this.filterConfig = null;
} /**
*选择并设置这个request要使用的字符编码
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException
{
if (ignore || (request.getCharacterEncoding() == null))
{
String encoding = selectEncoding(request);
if (encoding != null)
{
request.setCharacterEncoding(encoding);
}
}
chain.doFilter(request, response);
} /**
*初始化过滤器参数
*/
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 String selectEncoding(ServletRequest request)
{
return (this.encoding);
}}在web.xml中相应的配置
try {
if (str == null) {
return null;
}
else {
str = new String(str.getBytes("ISO8859_1"), "GBK");
return str;
}
}
catch (Exception e) {
return null;
}
}
不就可以了吗,嘻嘻