`
langzixin
  • 浏览: 127382 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

struts action接到form汉字乱码解决(过滤器)

阅读更多
首先在项目中建一个包com.common.filter。
然后再将tomcat的webapps/examples/WEB-INF/classes/filters中的SetCharacterEncodingFilter.java拷到项目新建的com.common.filter包中。
然后在web.xml中添加:
      注意添加的红色过滤器部分,位置固定在最前面,否则action得到的汉字就变为???了

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <!-- jsp过滤器 -->
    <filter>
          <filter-name>Set   Character   Encoding</filter-name>
           //这里根据文件实际位置来写
          <filter-class>com.common.filter.SetCharacterEncodingFilter</filter-class>
          <init-param>
              <param-name>encoding</param-name>
              <param-value>UTF-8</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>Set   Character   Encoding</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

    
    <!-- struts2 配置信息 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- spring 配置信息 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext.xml
        </param-value>
    </context-param>  


    <welcome-file-list>
        <welcome-file>registe/registe.jsp</welcome-file>
    </welcome-file-list>
</web-app>






附带SetCharacterEncodingFilter.java文件内容:

package com.common.filter;  //改为你放置文件的包

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 {

    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 {

        // 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);

    }


    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);

    }
}
分享到:
评论

相关推荐

    AutoCode代码生成器(Struts1.2版)

    ★ EncodingFilter 处理中文乱码的编码过滤器 ★ Action、Form、struts-config.xml web.xml struts的处理核心类及配置文件 ★ JSP调用页面(全面支持struts1.2)(增、删、改、查),分页功能自动实现 (如果数据库...

    名为责任链或者拦截器或者过滤器的简单模拟

    以上的这些功能,通过一种名为责任链或者拦截器或者过滤器(语义上的区别而技术上没有什么区别,知道做了什么就行了)的设计模式可以实现,那么就来看看什么是责任链的设计模式。 以下内容参考北京尚学堂的马士兵...

    深入浅出Struts2(附源码)

    1.3 带过滤器调度程序的Model 2 9 1.4 小结 13 第2章初识Struts 14 2.1 Struts的优点 14 2.2 Struts的动作处理流程 15 2.3 拦截器 17 2.4 Struts配置文件 18 2.4.1 struts.xml文件 19 2.4.2 struts....

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    1.3 带过滤器调度程序的Model 2 9 1.4 小结 13 第2章 初识Struts 14 2.1 Struts的优点 14 2.2 Struts的动作处理流程 15 2.3 拦截器 17 2.4 Struts配置文件 18 2.4.1 struts.xml文件 19 2.4.2 struts.properties文件...

    ssh(structs,spring,hibernate)框架中的上传下载

     以上是Spring+Hibernate将文件二进制数据持久化到数据库的解决方案,而Struts通过将表单中file类型的组件映射为ActionForm中类型为org.apache.struts.upload. FormFile的属性来获取表单提交的文件数据。  工程...

    《MyEclipse 6 Java 开发中文教程》前10章

    8.7 创建Filter(过滤器) 152 8.8 创建数据库访问层(DAO) 155 8.9 修改Servlet调用后台类 158 8.10 发布,重新发布,运行和测试应用 159 8.11 调试JSP应用 160 8.12 向现有Web项目添加Web开发功能 161 8.13高级设置 ...

    java web 视频、电子书、源码(李兴华老师出版)

    6.3.1、乱码解决 6.3.2、接收请求参数 6.3.3、显示全部的头信息 6.3.4、角色验证 6.3.5、其他操作 6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1...

    客户关系管理系统框架搭建(二)

    --4 创建事务管理器 aop切面--&gt; &lt;!--5 配置处理事务的注解--&gt; * 创建dao层共同的接口,放置在cn.itcast.crm.dao下 public interface ICommonDao&lt;T&gt; { public ...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 上

    6.3.1、乱码解决 6.3.2、接收请求参数 6.3.3、显示全部的头信息 6.3.4、角色验证 6.3.5、其他操作 6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得...

    MLDN+李兴华+Java+Web开发实战经典.part3.rar )

    6.3.1、乱码解决 6.3.2、接收请求参数 6.3.3、显示全部的头信息 6.3.4、角色验证 6.3.5、其他操作 6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1...

    李兴华 java_web开发实战经典 源码 完整版收集共享

    6.3.1、乱码解决 6.3.2、接收请求参数 6.3.3、显示全部的头信息 6.3.4、角色验证 6.3.5、其他操作 6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 下

    6.3.1、乱码解决 6.3.2、接收请求参数 6.3.3、显示全部的头信息 6.3.4、角色验证 6.3.5、其他操作 6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得...

    李兴华Java Web开发实战经典.pdf (高清版) Part1

    6.3.1、乱码解决 6.3.2、接收请求参数 6.3.3、显示全部的头信息 6.3.4、角色验证 6.3.5、其他操作 6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1...

    李兴华 Java Web 开发实战经典 高清扫描版Part3

    6.3.1、乱码解决 6.3.2、接收请求参数 6.3.3、显示全部的头信息 6.3.4、角色验证 6.3.5、其他操作 6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得...

    李兴华Java Web开发实战经典(高清版) Part2

    6.3.1、乱码解决 6.3.2、接收请求参数 6.3.3、显示全部的头信息 6.3.4、角色验证 6.3.5、其他操作 6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

Global site tag (gtag.js) - Google Analytics