struts2简单配置

新建一个struts2的项目步骤如下(当前版本为2.3.16.1)

整体如右图:

1、将struts2的jar包导入lib下

必须要导入的jar包有:

  • commons-fileupload-1.3.1.jar
  • commons-io-2.2.jar
  • commons-lang3-3.1.jar
  • freemarker-2.3.19.jar
  • javassist-3.11.0.GA.jar
  • ognl-3.0.6.jar
  • struts2-core-2.3.16.3.jar
  • xwork-core-2.3.16.3.jar

2、在WEB-INF下新建web.xml文件

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

    <filter>            
        <filter-name>struts2</filter-name>            
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>            
    </filter>            

    <filter-mapping>            
        <filter-name>struts2</filter-name>            
        <url-pattern>/*</url-pattern>            
    </filter-mapping>            
</web-app>            

3、在src下新建action 类及实体类

  • 新建LoginAction类
 1 package action;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 import com.opensymphony.xwork2.ActionSupport;
 7 
 8 import entity.User;
 9 
10 public class LoginAction extends ActionSupport{
11     /**
12      * serialID
13      */
14     private static final long serialVersionUID = 1L;
15     private User user;
16     private static Map<String,String> users = new HashMap<String,String>();
17     static{
18         users.put("bill", "1234");
19         users.put("mike", "4321");
20     }
21     /**
22      * login
23      * @return result after deal the logic
24      */
25     public String login(){
26         if(user.getNickname().equals("hello")){
27             return SUCCESS;
28         }else{
29             return INPUT;
30         }
31     }
32     public User getUser() {
33         return user;
34     }
35     public void setUser(User user) {
36         this.user = user;
37     }
38 }
  • 新建实体类
 1 package entity;
 2 
 3 public class User {
 4     private String nickname;
 5     private String password;
 6     public String getNickname() {
 7         return nickname;
 8     }
 9     public void setNickname(String nickname) {
10         this.nickname = nickname;
11     }
12     public String getPassword() {
13         return password;
14     }
15     public void setPassword(String password) {
16         this.password = password;
17     }
18 }

4、在src下创建struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <constant name="struts.devMode" value="true" />
 8     <constant name="struts.custom.i18n.resources" value="message" />
 9     <package name="default" namespace="/" extends="struts-default">
10         <default-action-ref name="index" />
11         <action name="index">
12             <result>/WEB-INF/pages/login.jsp</result>
13         </action>
14 
15      </package>
16      <package name="user" namespace="/user" extends="struts-default">
17         <!-- =========================================================login -->
18         <action name="Login_*" method="{1}" class="action.LoginAction">
19             <result name="success">/WEB-INF/pages/main.jsp</result>
20             <result name="input">/WEB-INF/pages/login.jsp</result>
21         </action>
22         <!-- =========================================================main -->
23         <action name="Main_*">
24             <result>/WEB-INF/pages/{1}.jsp</result>
25         </action>
26     </package>
27 
28 </struts>

5、创建jsp文件

  • login.jsp
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title>login</title>
 9 </head>
10 <body>
11 <s:form action="Login_login" method="post" theme="simple" namespace="/user">
12     nickname<s:textfield name="user.nickname"/>
13     <b style="color: red">
14         <s:property value="fieldErrors['user.nickname'][0]"/>
15     </b><br/>
16     password<s:password name="user.password"/>
17     <b style="color: red">
18         <s:property value="fieldErrors['user.password'][0]"/>
19     </b><br/>
20     <s:submit value="%{getText('button.login')}" cssStyle="width: 85px; height: 30px;"/>
21 </s:form>
22 </body>
23 </html>
  • main.jsp
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>main</title>
 8 </head>
 9 <body>
10  welcome ${user.nickname},come in  <br>
11 <a href="Main_login">logout</a>&nbsp;&nbsp;&nbsp;<a href="Main_register">register</a>
12 </body>
13 </html>

6、加入国际化资源

  • 在struts.xml中加入<constant name="struts.custom.i18n.resources" value="message" />
  • 在src下创建message.properties
1 # -- button --
2 button.login=login
3 # -- errors --
4 error.nickname.required=nickname is required
5 error.password.required=Password is required

 

7、加入验证

  • 在LoginAction.java文件目录下创建LoginAction-validation.xml
 1 <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
 2 
 3 <validators>
 4 
 5     <field name="user.nickname">
 6         <field-validator type="requiredstring">
 7             <param name="trim">true</param>
 8             <message key="error.nickname.required"/>
 9         </field-validator>
10     </field>
11 
12     <field name="user.password">
13         <field-validator type="requiredstring">
14             <message key="error.password.required"/>
15         </field-validator>
16     </field>
17 
18 </validators>

 

 

 

posted @ 2014-07-04 09:44  gowork  Views(214)  Comments(0Edit  收藏  举报