struts2相关设置

1、复制commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,commons-logging-1.0.4.jar,freemarker-2.3.13.jar,ognl-2.6.11.jar,struts2-core-2.1.6.jar,xwork-2.1.2.jar到/WEB-INF/lib目录下。
2、在web.xml中添加

 <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>
3、在src目录中新建struts.xml文件,内容示例如下:

<? xml version="1.0" encoding="UTF-8"  ?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

< struts >

 
< package  name ="struts2"  extends ="struts-default" >

  
< action  name ="login"  class ="com.test.action.LoginAction" >
   
< result  name ="input" > /login2.jsp </ result >
   
< result  name ="success" > /result.jsp </ result >
  
</ action >
 

 
</ package >

</ struts >



 4、标签库设置
在jsp文件前面加上<%@ taglib prefix="s" uri="/struts-tags"%>
5、XML校验设置
在需要校验的Action同目录中增加 Action名-validation.xml,如Action名为RegisterAction,则校验文件名为RegisterAction-validation.xml,示例如下

<! DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"
>

< validators >
    
< field  name ="username" >
        
< field-validator  type ="requiredstring" >
            
< message  key ="requiredstring" />
        
</ field-validator >
    
</ field >
    
< field  name ="password" >
        
< field-validator  type ="requiredstring" >
            
< message  key ="requiredstring" />
        
</ field-validator >
    
</ field >
</ validators >

 

你可能感兴趣的:(struts2)