第二十一部分:接着编写 web.xml ,代码如下:
<? xml version="1.0" encoding="GB2312" ?> <! DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > < web-app > < display-name > 游戏软件管理系统 </ display-name > < context-param > < param-name > log4jConfigLocation </ param-name > < param-value > /WEB-INF/classes/log4j.properties </ param-value > </ context-param > < context-param > < param-name > contextConfigLocation </ param-name > < param-value > /WEB-INF/spring-context/applicationContext.xml </ param-value > </ context-param > < filter > < filter-name > Set Character Encoding </ filter-name > < filter-class > com.game.commons.SetCharacterEncodingFilter </ filter-class > < init-param > < param-name > encoding </ param-name > < param-value > GB2312 </ param-value > </ init-param > </ filter > < filter-mapping > < filter-name > Set Character Encoding </ filter-name > < url-pattern > /* </ url-pattern > </ filter-mapping > <!-- - Loads the root application context of this web app at startup. - The application context is then available via - WebApplicationContextUtils.getWebApplicationContext(servletContext). --> < listener > < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > < listener > < listener-class > org.springframework.web.util.Log4jConfigListener </ listener-class > </ listener > <!-- Action Servlet Configuration --> < servlet > < servlet-name > action </ servlet-name > < servlet-class > org.apache.struts.action.ActionServlet </ servlet-class > <!-- 缺省 --> < init-param > < param-name > config </ param-name > < param-value > /WEB-INF/struts-config/struts-config.xml </ param-value > </ init-param > < init-param > < param-name > debug </ param-name > < param-value > 3 </ param-value > </ init-param > < init-param > < param-name > detail </ param-name > < param-value > 3 </ param-value > </ init-param > < init-param > < param-name > nocache </ param-name > < param-value > yes </ param-value > </ init-param > < load-on-startup > 2 </ load-on-startup > </ servlet > <!-- Action Servlet Mapping --> < servlet-mapping > < servlet-name > action </ servlet-name > < url-pattern > *.do </ url-pattern > </ servlet-mapping > <!-- The Welcome File List --> < welcome-file-list > < welcome-file > products/index.jsp </ welcome-file > </ welcome-file-list > <!-- Struts Tag Library Descriptors --> < taglib > < taglib-uri > struts-bean </ taglib-uri > < taglib-location > /WEB-INF/tld/struts-bean.tld </ taglib-location > </ taglib > < taglib > < taglib-uri > struts-html </ taglib-uri > < taglib-location > /WEB-INF/tld/struts-html.tld </ taglib-location > </ taglib > < taglib > < taglib-uri > struts-logic </ taglib-uri > < taglib-location > /WEB-INF/tld/struts-logic.tld </ taglib-location > </ taglib > < taglib > < taglib-uri > struts-nested </ taglib-uri > < taglib-location > /WEB-INF/tld/struts-nested.tld </ taglib-location > </ taglib > </ web-app >
第二十二部分:大家可能注意到了这里有个 Set Character Encoding 过滤器。我们需要在 com.game.commons 包中
新建 SetCharacterEncodingFilter 类来过滤编码,类的代码如下:
package com.game.commons;
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 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
this .ignore = false ;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO 自动生成方法存根
if (ignore || (request.getCharacterEncoding() == null )) {
String encoding = selectEncoding(request);
if (encoding != null )
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void destroy() {
// TODO 自动生成方法存根
this .encoding = null ;
this .filterConfig = null ;
}
protected String selectEncoding(ServletRequest request) {
return ( this .encoding);
}
}
第二十三部分:为了项目的测试运行,我们还需要配置 log4j.properties ,这个文件放在 src 根目录下。代码如下:
log4j.rootLogger=info,console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.SimpleLayout log4j.logger.com.wehave=DEBUG # log4j.logger.org.springframework=DEBUG # SqlMap logging configuration # log4j.logger.com.ibatis=DEBUG # log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG # log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG # log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG # log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG #log4j.logger.java.sql.ResultSet=DEBUG #log4j.logger.javax.sql=DEBUG