我的问题是这样的:
一个form 表单里有个文本框:输入英文,在给struts(1.3)的formbean 赋值时没问题,但是,输入中文时,formbean 对应的属性就会出现乱码啦。
(struts 和jpa 及spring 集成的一个项目)<form action="/product/query.do" method="post" name="search" id="search"> 商品搜索:<input id="word" name="word" accesskey="s" size="100" maxlength="100" value=""/> <input type="submit" value="搜 索" id="DoSearch"/>
</form>
action:
@Controller("/product/query")
public class ProductQueryAction extends Action {
@Resource ProductSearchService productSearchService; @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ProductQueryForm formbean = (ProductQueryForm)form;=================》》》中文赋值时有乱码
PageView<ProductInfo> pageView = new PageView<ProductInfo>(12,formbean.getPage());
pageView.setQueryResult(productSearchService.search(formbean.getWord(),
pageView.getFirstResult(), pageView.getMaxresult()));
request.setAttribute("pageView", pageView);
return mapping.findForward("list");
}}
一个form 表单里有个文本框:输入英文,在给struts(1.3)的formbean 赋值时没问题,但是,输入中文时,formbean 对应的属性就会出现乱码啦。
(struts 和jpa 及spring 集成的一个项目)<form action="/product/query.do" method="post" name="search" id="search"> 商品搜索:<input id="word" name="word" accesskey="s" size="100" maxlength="100" value=""/> <input type="submit" value="搜 索" id="DoSearch"/>
</form>
action:
@Controller("/product/query")
public class ProductQueryAction extends Action {
@Resource ProductSearchService productSearchService; @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ProductQueryForm formbean = (ProductQueryForm)form;=================》》》中文赋值时有乱码
PageView<ProductInfo> pageView = new PageView<ProductInfo>(12,formbean.getPage());
pageView.setQueryResult(productSearchService.search(formbean.getWord(),
pageView.getFirstResult(), pageView.getMaxresult()));
request.setAttribute("pageView", pageView);
return mapping.findForward("list");
}}
* 通过jsp页面,html页面或者servlet中的表单元素提交参数给对应的jsp页面或servlet
而jsp和servlet接收的参数产生中文乱码
* 在接收post提交的参数之前,使用request.setCharactEncoding("utf-8")设定接收参数的内容使用"utf-8"编码
* 更好的解决办法是添加过滤器
gbk? gb2312、? utf8?
JSP页面编码方式设置好以后,通过POST方式传值,还要设置request.setCharacterEncoding("XXX"),其中XXX和你在JSP页面设置的编码方式一样。
如果从Servelet中取数据到JSP页面上,还要设置response.setContentType("XXX"),其中XXX跟JSP页面上的也一样
感谢朋友热心的参与:
首先讲一下我的问题:
我的问题是出在:
在配置过滤器时:位置不对。
我的项目中有8、9个filter ,应该把过滤字符编码的放在第一位,这样,就像一个链条一样,第一个一定把字符放好。其它的:都是小意思了。顺便提一下:
1:jsp 页面有乱码:pageEncode=utf8
2:servlet-->jsp 乱码:在sevlet里:
request.setCharacterEncoding("utf-8");
response.setContentType("text ml;charset=UTF-8");
3:表单参数乱码:
post:过滤器来处理:(网上很多)
4:URI 参数乱码:
配置tomcat(server.xml 加入URIEncoding="utf-8")
5:property乱码:
下个日本鬼子写的:jp.gr.java_conf.ussiy.app.propedit_4.8.2_for_eclipse3.0.zip
最后,最重要的一点:就是我琢磨了整整一整天的当有多个filter时:处理文字乱码的要放在第一个位置。
package filter; import java.io.IOException; import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class Filter implements javax.servlet.Filter { public void destroy() {
// TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
} public void init(FilterConfig arg0) throws ServletException {
System.out.println("filter--------init?"); } }