posts - 495,  comments - 11,  trackbacks - 0
创建工程,比如Struts2

struts-<?xml:namespace prefix = st1 />2.0.6\lib中的的jar文件全部(为了后面的功能扩张)粘贴工程Struts2lib目录中。

二、           配置项目的web.xml      

Struts2.0所有的配置被整合在一个Filter里面,该Filter位于org.apache.struts2.dispatcher.FilterDispatcher,因此,在web.xml中应该这样声明:

<filter>

       <filter-name>struts</filter-name>

       <filter-class>

           org.apache.struts2.dispatcher.FilterDispatcher

       </filter-class>

    </filter>

    

    <filter-mapping>

       <filter-name>struts</filter-name>

       <url-pattern>/*</url-pattern>

</filter-mapping>

但是,该Filter一个问题,就是从页面传到后台的中文经过这个过滤器后会变成乱码,为了解决这个问题,需要重写这个过滤器,最简单的方法是写一个类继承FilterDispatcher,在src目录下创建com.filter包,在包中建立NewFilterDispatcher类,继承FilterDispatcher,代码如下:

packagecom.filter;

importjava.io.IOException;

importjavax.servlet.FilterChain;

importjavax.servlet.FilterConfig;

importjavax.servlet.ServletException;

importjavax.servlet.ServletRequest;

importjavax.servlet.ServletResponse;

importorg.apache.struts2.dispatcher.FilterDispatcher;

publicclassNewFilterDispatcherextendsFilterDispatcher {

    privatestaticStringencoding="GB2312";

        publicvoidinit(FilterConfig filterConfig)throwsServletException {

            super.init(filterConfig);

            String encodingParam = filterConfig.getInitParameter("encoding");

            if(encodingParam !=null&& encodingParam.trim().length() != 0) {

               encoding= encodingParam;

            }

        }

        publicvoiddoFilter(ServletRequest request, ServletResponse response,

                FilterChain chain)throwsIOException, ServletException {

            request.setCharacterEncoding(encoding);

            super.doFilter(request, response, chain);

        }

    

}(待续)

posted on 2007-09-24 14:46 jadmin 阅读(111) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: