你扩展一下ActionServlet好了.
代码如下:
public class MainServlet extends ActionServlet { protected static final String DEFAULT_CHARSET = "GBK";
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
request.setCharacterEncoding(DEFAULT_CHARSET);
process(request, response);
} public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
request.setCharacterEncoding(DEFAULT_CHARSET);
process(request, response);
}
}
在Web.xml中参数配置同ActionServlet
这样,在页面输入中文就不用再转码了
代码如下:
public class MainServlet extends ActionServlet { protected static final String DEFAULT_CHARSET = "GBK";
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
request.setCharacterEncoding(DEFAULT_CHARSET);
process(request, response);
} public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
request.setCharacterEncoding(DEFAULT_CHARSET);
process(request, response);
}
}
在Web.xml中参数配置同ActionServlet
这样,在页面输入中文就不用再转码了
解决方案 »
- 怎么用hql筛选所有的子类属性
- 判断登录IP是否在指定IP区间 方法
- 在struts2中从页面得到输入的值
- JSF的MyFaces调试错误java.lang.IllegalStateException
- struts标签<html:checkbox>如何使用
- 初学java,求一段代码(socket,多线程,mysql)
- jsp页面局部打印
- 请高手解答:使用Jena将本体存入MySQL
- 为什么我的JB 9里支持GBK码而eclipse里却不支持呢?
- 有人用过<jsp:forward page=""></jsp:forward>标签吗?
- [请教]struts-example中iterate property 用法请教
- weblogic问题
非常谢谢!
Up一下吧 将“流氓无赖”测试到底
——始于2003年7月
树欲止而风不停,行云流水匆匆去;
树梢蚂蚱凭空望,江边浪花碎巨石; 支持“流金岁月”!!!
——2003年12月24日am^@^
宠物店的源码中有现成的
关于Struts支持中文的问题,大多都是写个过滤器在doFilter函数里设置字符集
package com.web;
import javax.servlet.*;
import java.io.IOException;
public class EncodeFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
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;
}
}
然后,在web.xml文件配置一个过滤器,给出encoding和ignore设置过滤*.do就可以了,如下:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.web. EncodeFilter </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-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
其他的I18N的支持,就不用管太多了,直接使用它的message就可以了。
这是什么呀,编译出错