Web 层代码量比较大,涉及的地方也比较多,考虑到文章过于庞大,所以分两篇写。
我们还是先从主要的 action 开始吧。
在 com.game.products.web.actions 包中新建 ProductsAction ,这是一个 DispatchAction ,代码如下:
package
com.game.products.web.actions;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
java.util.List;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
org.acegisecurity.AccessDeniedException;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.actions.DispatchAction;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
com.game.commons.Pager;
import
com.game.commons.PagerService;
import
com.game.products.model.Products;
import
com.game.products.services.iface.ProductsService;
import
com.game.products.web.forms.ProductsForm;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public
class
ProductsAction
extends
DispatchAction
{
private ProductsService productsService;
private PagerService pagerService;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** */ /**
* 显示所有信息
*/
public ActionForward doGetProducts(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
HttpServletResponse res)
{
String flag = req.getParameter( " flag " );
int totalRows = productsService.getRows();
String currentPage = req.getParameter( " currentPage " );
String pagerMethod = req.getParameter( " pagerMethod " );
Pager pager = pagerService.getPager(currentPage, pagerMethod, totalRows);
List productsList = productsService.getProducts(pager.getPageSize(), pager.getStartRow());
req.setAttribute( " productsList " , productsList);
req.setAttribute( " PAGER " , pager);
req.setAttribute( " flag " , flag);
req.setAttribute( " totalRows " , String.valueOf(totalRows));
return mapping.findForward( " all " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** */ /**
* 显示一条信息
*/
public ActionForward doGetProduct(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
HttpServletResponse res)
{
String flag = req.getParameter( " flag " );
String gameId = req.getParameter( " gameId " );
String fieldname = "" ;
String value = "" ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (flag.equals( " 2 " ))
{
fieldname = (String)req.getParameter( " fieldname " );
value = (String)req.getParameter( " value " );
req.setAttribute( " fieldname " , fieldname);
req.setAttribute( " value " , value);
}
Products pd = productsService.getProduct(gameId);
req.setAttribute( " pd " , pd);
req.setAttribute( " flag " , flag);
return mapping.findForward( " one " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** */ /**
* 添加信息页面
*/
public ActionForward doAddProductPage(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
HttpServletResponse res)
{
String flag = req.getParameter( " flag " );
req.setAttribute( " flag " , flag);
String fieldname = "" ;
String value = "" ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (flag.equals( " 2 " ))
{
fieldname = (String)req.getParameter( " fieldname " );
value = (String)req.getParameter( " value " );
req.setAttribute( " fieldname " , fieldname);
req.setAttribute( " value " , value);
}
String maxid = productsService.getMaxID();
req.setAttribute( " maxid " , maxid);
return mapping.findForward( " add " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** */ /**
* 添加信息
*/
public ActionForward doAddProduct(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
HttpServletResponse res)
{
ProductsForm pf = (ProductsForm)form;
String flag = pf.getFlag();
req.setAttribute( " flag " , flag);
String fieldname = "" ;
String value = "" ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (flag.equals( " 2 " ))
{
fieldname = pf.getFieldname();
value = pf.getValue();
req.setAttribute( " fieldname " , fieldname);
req.setAttribute( " value " , value);
}
Products pd = new Products();
pd.setGameCapacity(pf.getGameCapacity());
pd.setGameId(pf.getGameId());
pd.setGameMedia(pf.getGameMedia());
pd.setGameNameCn(pf.getGameNameCn());
pd.setGameNameEn(pf.getGameNameEn());
pd.setGameVersion(pf.getGameVersion());
pd.setGameCopyright(pf.getGameCopyright());
pd.setGameContent(pf.getGameContent());
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (pf.getGamePrice().equals( "" ))
{
pd.setGamePrice( null );
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else
{
pd.setGamePrice(pf.getGamePrice());
}
int sign = 1 ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
productsService.addProduct(pd);
sign = 1 ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} catch (Exception e)
{
sign = 2 ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (sign == 1 )
{
return mapping.findForward( " success " );
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else
{
return mapping.findForward( " failure " );
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** */ /**
* 修改信息
*/
public ActionForward doUpdateProduct(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
HttpServletResponse res)
{
ProductsForm pf = (ProductsForm)form;
String gameId = pf.getGameId();
String flag = pf.getFlag();
req.setAttribute( " flag " , flag);
String fieldname = "" ;
String value = "" ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (flag.equals( " 2 " ))
{
fieldname = pf.getFieldname();
value = pf.getValue();
req.setAttribute( " fieldname " , fieldname);
req.setAttribute( " value " , value);
}
Products pd = productsService.getProduct(gameId);
pd.setGameCapacity(pf.getGameCapacity());
pd.setGameId(pf.getGameId());
pd.setGameMedia(pf.getGameMedia());
pd.setGameNameCn(pf.getGameNameCn());
pd.setGameNameEn(pf.getGameNameEn());
pd.setGameVersion(pf.getGameVersion());
pd.setGameCopyright(pf.getGameCopyright());
pd.setGameContent(pf.getGameContent());
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (pf.getGamePrice().equals( "" ))
{
pd.setGamePrice( null );
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else
{
pd.setGamePrice(pf.getGamePrice());
}
int sign = 1 ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
productsService.updateProductd(pd);
sign = 1 ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} catch (Exception e)
{
sign = 2 ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (sign == 1 )
{
return mapping.findForward( " success " );
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else
{
return mapping.findForward( " failure " );
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** */ /**
* 删除信息
*/
public ActionForward doDeleteProduct(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
HttpServletResponse res)
{
String flag = req.getParameter( " flag " );
req.setAttribute( " flag " , flag);
String fieldname = "" ;
String value = "" ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (flag.equals( " 2 " ))
{
fieldname = (String)req.getParameter( " fieldname " );
value = (String)req.getParameter( " value " );
req.setAttribute( " fieldname " , fieldname);
req.setAttribute( " value " , value);
}
String gameId = req.getParameter( " gameId " );
Products pd = productsService.getProduct(gameId);
int sign = 1 ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
productsService.deleteProduct(pd);
sign = 1 ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} catch (Exception e)
{
sign = 2 ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (sign == 1 )
{
return mapping.findForward( " success " );
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else
{
return mapping.findForward( " failure " );
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** */ /**
* 返回信息
*/
public ActionForward doReturnProduct(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
HttpServletResponse res)
{
String flag = req.getParameter( " flag " );
req.setAttribute( " flag " , flag);
String fieldname = "" ;
String value = "" ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (flag.equals( " 1 " ))
{
int totalRows = productsService.getRows();
String currentPage = req.getParameter( " currentPage " );
String pagerMethod = req.getParameter( " pagerMethod " );
Pager pager = pagerService.getPager(currentPage, pagerMethod, totalRows);
List productsList = productsService.getProducts(pager.getPageSize(), pager.getStartRow());
req.setAttribute( " productsList " , productsList);
req.setAttribute( " PAGER " , pager);
req.setAttribute( " flag " , flag);
req.setAttribute( " totalRows " , String.valueOf(totalRows));
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else if (flag.equals( " 2 " ))
{
fieldname = (String)req.getParameter( " fieldname " );
value = (String)req.getParameter( " value " );
int totalRows = productsService.getRows(fieldname,value);
String currentPage = req.getParameter( " currentPage " );
String pagerMethod = req.getParameter( " pagerMethod " );
Pager pager = pagerService.getPager(currentPage, pagerMethod, totalRows);
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
req.setAttribute( " fieldname " , fieldname);
req.setAttribute( " value " , value);
List productsList = productsService.queryProducts(fieldname, value,pager.getPageSize(), pager.getStartRow());
req.setAttribute( " productsList " , productsList);
req.setAttribute( " PAGER " , pager);
req.setAttribute( " totalRows " , String.valueOf(totalRows));
}
return mapping.findForward( " all " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** */ /**
* 查询信息
*/
public ActionForward doQueryProduct(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
HttpServletResponse res)
{
String flag = req.getParameter( " flag " );
req.setAttribute( " flag " , flag);
String fieldname = "" ;
String value = "" ;
fieldname = (String)req.getParameter( " fieldname " );
value = (String)req.getParameter( " value " );
int totalRows = productsService.getRows(fieldname,value);
String currentPage = req.getParameter( " currentPage " );
String pagerMethod = req.getParameter( " pagerMethod " );
Pager pager = pagerService.getPager(currentPage, pagerMethod, totalRows);
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
req.setAttribute( " fieldname " , fieldname);
req.setAttribute( " value " , value);
List productsList = productsService.queryProducts(fieldname, value,pager.getPageSize(), pager.getStartRow());
req.setAttribute( " productsList " , productsList);
req.setAttribute( " PAGER " , pager);
req.setAttribute( " totalRows " , String.valueOf(totalRows));
return mapping.findForward( " all " );
}
public ProductsService getProductsService()
{
return productsService;
}
public void setProductsService(ProductsService productsService)
{
this .productsService = productsService;
}
public PagerService getPagerService()
{
return pagerService;
}
public void setPagerService(PagerService pagerService)
{
this .pagerService = pagerService;
}
}
在 com.game.products.web.forms 包中新建 ProductsForm ,他继承了 ValidatorForm 。代码如下:
package
com.game.products.web.forms;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
javax.servlet.http.HttpServletRequest;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.validator.ValidatorForm;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public
class
ProductsForm
extends
ValidatorForm
{
// Fields
private String gameId; // 编号
private String gameNameCn; // 中文名称
private String gameNameEn; // 英文名称
private String gameCapacity; // 碟数
private String gameVersion; // 版本
private String gameMedia; // 介质
private String gameCopyright; // 版权
private String gamePrice; // 价格
private String gameContent; // 攻略
private String flag;
private String fieldname;
private String value;
// Constructors
public ProductsForm()
{
gameId = null ;
gameNameCn = null ;
gameNameEn = null ;
gameCapacity = null ;
gameVersion = null ;
gameMedia = null ;
gameCopyright = null ;
gamePrice = null ;
gameContent = null ;
flag = null ;
fieldname = null ;
value = null ;
}
// reset
public void reset(ActionMapping mapping, HttpServletRequest request)
{
gameId = null ;
gameNameCn = null ;
gameNameEn = null ;
gameCapacity = null ;
gameVersion = null ;
gameMedia = null ;
gameCopyright = null ;
gamePrice = null ;
gameContent = null ;
flag = null ;
fieldname = null ;
value = null ;
}
// Property accessors
public String getGameCapacity()
{
return gameCapacity;
}
public void setGameCapacity(String gameCapacity)
{
this .gameCapacity = gameCapacity;
}
public String getGameId()
{
return gameId;
}
public void setGameId(String gameId)
{
this .gameId = gameId;
}
public String getGameNameCn()
{
return gameNameCn;
}
public void setGameNameCn(String gameNameCn)
{
this .gameNameCn = gameNameCn;
}
public String getGameNameEn()
{
return gameNameEn;
}
public void setGameNameEn(String gameNameEn)
{
this .gameNameEn = gameNameEn;
}
public String getGameVersion()
{
return gameVersion;
}
public void setGameVersion(String gameVersion)
{
this .gameVersion = gameVersion;
}
public String getGameMedia()
{
return gameMedia;
}
public void setGameMedia(String gameMedia)
{
this .gameMedia = gameMedia;
}
public String getFieldname()
{
return fieldname;
}
public void setFieldname(String fieldname)
{
this .fieldname = fieldname;
}
public String getFlag()
{
return flag;
}
public void setFlag(String flag)
{
this .flag = flag;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this .value = value;
}
public String getGameCopyright()
{
return gameCopyright;
}
public void setGameCopyright(String gameCopyright)
{
this .gameCopyright = gameCopyright;
}
public String getGameContent()
{
return gameContent;
}
public void setGameContent(String gameContent)
{
this .gameContent = gameContent;
}
public String getGamePrice()
{
return gamePrice;
}
public void setGamePrice(String gamePrice)
{
this .gamePrice = gamePrice;
}
}
接着编写配置文件。
在 struts-config 包中新建 struts-config.xml 。代码如下:
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<!
DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"
>
<
struts-config
>
<
form-beans
>
<
form-bean
name
="productsForm"
type
="com.game.products.web.forms.ProductsForm"
/>
</
form-beans
>
<
global-forwards
>
<
forward
name
="success"
path
="/products/product_success.jsp"
/>
<
forward
name
="failure"
path
="/products/product_failure.jsp"
/>
</
global-forwards
>
<
action-mappings
>
<
action
path
="/getProducts"
type
="org.springframework.web.struts.DelegatingActionProxy"
parameter
="method"
scope
="request"
validate
="false"
>
<
forward
name
="all"
path
="/products/products.jsp"
/>
</
action
>
<
action
path
="/getProduct"
type
="org.springframework.web.struts.DelegatingActionProxy"
parameter
="method"
scope
="request"
validate
="false"
>
<
forward
name
="one"
path
="/products/product.jsp"
/>
</
action
>
<
action
path
="/deleteProduct"
type
="org.springframework.web.struts.DelegatingActionProxy"
parameter
="method"
scope
="request"
validate
="false"
>
</
action
>
<
action
path
="/addProductPage"
type
="org.springframework.web.struts.DelegatingActionProxy"
parameter
="method"
scope
="request"
validate
="false"
>
<
forward
name
="add"
path
="/products/addproduct.jsp"
/>
</
action
>
<
action
path
="/addProduct"
name
="productsForm"
type
="org.springframework.web.struts.DelegatingActionProxy"
parameter
="method"
scope
="request"
validate
="false"
input
="/product_failure.jsp"
>
</
action
>
<
action
path
="/updateProduct"
name
="productsForm"
type
="org.springframework.web.struts.DelegatingActionProxy"
parameter
="method"
scope
="request"
validate
="false"
input
="/product_failure.jsp"
>
</
action
>
<
action
path
="/returnProduct"
type
="org.springframework.web.struts.DelegatingActionProxy"
parameter
="method"
scope
="request"
validate
="false"
>
<
forward
name
="all"
path
="/products/products.jsp"
/>
</
action
>
<
action
path
="/queryProducts"
type
="org.springframework.web.struts.DelegatingActionProxy"
parameter
="method"
scope
="request"
validate
="false"
>
<
forward
name
="all"
path
="/products/products.jsp"
/>
</
action
>
</
action-mappings
>
<
message-resources
parameter
="com.game.resources.ApplicationResourcesProducts"
/>
<
plug-in
className
="org.apache.struts.validator.ValidatorPlugIn"
>
<
set-property
property
="pathnames"
value
="/WEB-INF/struts-validator/validator-rules.xml,/WEB-INF/struts-validator/validation.xml"
/>
</
plug-in
>
</
struts-config
>
需要注意的是,这里的 action 交由 spring 的 DelegatingActionProxy 管理了。
打开 applicationContext.xml ,接着添加如下代码:
<!--
View
-->
<
bean
name
="/getProducts"
class
="com.game.products.web.actions.ProductsAction"
singleton
="false"
>
<
property
name
="productsService"
>
<
ref
bean
="productsService"
/>
</
property
>
<
property
name
="pagerService"
>
<
ref
bean
="pagerService"
/>
</
property
>
</
bean
>
<
bean
name
="/getProduct"
class
="com.game.products.web.actions.ProductsAction"
singleton
="false"
>
<
property
name
="productsService"
>
<
ref
bean
="productsService"
/>
</
property
>
</
bean
>
<
bean
name
="/deleteProduct"
class
="com.game.products.web.actions.ProductsAction"
singleton
="false"
>
<
property
name
="productsService"
>
<
ref
bean
="productsService"
/>
</
property
>
</
bean
>
<
bean
name
="/addProductPage"
class
="com.game.products.web.actions.ProductsAction"
singleton
="false"
>
<
property
name
="productsService"
>
<
ref
bean
="productsService"
/>
</
property
>
</
bean
>
<
bean
name
="/addProduct"
class
="com.game.products.web.actions.ProductsAction"
singleton
="false"
>
<
property
name
="productsService"
>
<
ref
bean
="productsService"
/>
</
property
>
</
bean
>
<
bean
name
="/updateProduct"
class
="com.game.products.web.actions.ProductsAction"
singleton
="false"
>
<
property
name
="productsService"
>
<
ref
bean
="productsService"
/>
</
property
>
</
bean
>
<
bean
name
="/returnProduct"
class
="com.game.products.web.actions.ProductsAction"
singleton
="false"
>
<
property
name
="productsService"
>
<
ref
bean
="productsService"
/>
</
property
>
<
property
name
="pagerService"
>
<
ref
bean
="pagerService"
/>
</
property
>
</
bean
>
<
bean
name
="/queryProducts"
class
="com.game.products.web.actions.ProductsAction"
singleton
="false"
>
<
property
name
="productsService"
>
<
ref
bean
="productsService"
/>
</
property
>
<
property
name
="pagerService"
>
<
ref
bean
="pagerService"
/>
</
property
>
</
bean
>
接着编写 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;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
java.io.IOException;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public
class
SetCharacterEncodingFilter
implements
Filter
{
protected String encoding = null ;
protected FilterConfig filterConfig = null ;
protected boolean ignore = true ;
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
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![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
# 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
这是一个简单的 log4j 配置方法,只是在后台打印出需要的数据。如果还有别的需求,可以参考这个文档:如何使用 Log4j ?
为了达到页面验证的目的,我们还需要使用到 struts 的验证框架。
在 struts-validator 中添加 validation.xml 。在这配置验证规则:
<!
DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd"
>
<
form-validation
>
<
formset
>
<
form
name
="productsForm"
>
<
field
property
="gameNameCn"
depends
="required"
>
<
arg0
key
="errors.gameNameCn"
/>
</
field
>
<
field
property
="gameNameEn"
depends
="required"
>
<
arg0
key
="errors.gameNameEn"
/>
</
field
>
<
field
property
="gameCapacity"
depends
="integer"
>
<
arg0
key
="errors.gameCapacity"
/>
</
field
>
<
field
property
="gamePrice"
depends
="float"
>
<
arg0
key
="errors.gamePrice"
/>
</
field
>
</
form
>
</
formset
>
</
form-validation
>
在同个目录下,添加 validator-rules.xml