struts2.1.6 学习日志一

阿拉这人一般都是“后知后觉”,struts2出来这么长的时间了,到最近才关注它;可能最近要用它做点自己东西;
今天按照struts2自带的demo学习了一下,感觉不错,比1.x好用得多。
备份一下;
-------------------------------------------------------------------------------------

Struts2.1.6测试日志:

 

一、下载开发包http://struts.apache.org/download.cgi#struts216

 

二、将核心的 7 个开发包引入到工程中

1.         commons-logging-1.0.4.jar

2.         freemarker-2.3.13.jar

3.         ognl-2.6.11.jar

4.         struts2-core-2.1.6.jar

5.         xwork-2.1.2.jar

6.         commons-fileupload-1.2.1.jar

7.         commons-io-1.3.2.jar

 

三、在项目中建立一个名为resources目录的Source Folder

(方便管理)

 

四、配置web.xml

<!-- struts2 config begin -->

    <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>

<!-- struts2 config end -->

 

五、编写一个用户实体类

package test.entity;

public class UserEntity {

    private int userId;

    private String passWord;

    private String userName;

    private String userEmail;

    public int getUserId() {

       return userId;

    }

    public void setUserId(int userId) {

       this.userId = userId;

    }

    public String getPassWord() {

       return passWord;

    }

    public void setPassWord(String passWord) {

       this.passWord = passWord;

    }

    public String getUserName() {

       return userName;

    }

    public void setUserName(String userName) {

       this.userName = userName;

    }

    public String getUserEmail() {

       return userEmail;

    }

    public void setUserEmail(String userEmail) {

       this.userEmail = userEmail;

    }  

}

 

六、resources目录中建立struts配置文件: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>

    <include file="user.xml" />

    <!-- Add packages here -->

</struts>

 

七、resources目录中建立user.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="user" extends="struts-default" namespace="/UserCenter">      

       <action name="Index" class="test.struts.action.UserAction" >

           <result type="redirectAction">Login</result>

       </action>

       <action name="Login" class="test.struts.action.UserAction" method="Login">

           <result name="input">/struts/user/Login.jsp</result>

       </action>

    </package>

</struts>

 

八、建立一个用户登录页/struts/user/Login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title><s:property value="getText('Login.title')"/></title>

</head>

<body>

<s:form action="Login">

    <s:textfield key="user.userName"/>

    <s:password key="user.passWord" />

    <s:submit/>

</s:form> </body>

</html>

 

九、建立一个用户的基类UserSupport

package test.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserSupport extends ActionSupport{

    //因为是测试,所以没内容

}

 

十、添加一个类UserAction

package test.struts.action;

import test.entity.UserEntity;

public class UserAction extends UserSupport {

    private UserEntity user;

    public String execute() throws Exception {

       System.out.println("/n ==== GoTo Login ==== /n");

       return SUCCESS;

    }

    public String Login() {

       user = new UserEntity();

       if (isInvalid(user.getUserName())) return INPUT;

       if (isInvalid(user.getPassWord())) return INPUT;

       return SUCCESS;

    }

    private boolean isInvalid(String value) {

       return (value == null || value.length() == 0);

    }

    public UserEntity getUser() {

       return user;

    }

    public void setUser(UserEntity user) {

       this.user = user;

    }

}

 

十一、       验证文件resources/test/struts/action/UserAction-Login-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="user.userName">

        <field-validator type="requiredstring">

            <message key="requiredstring"/>

        </field-validator>

    </field>

    <field name="user.passWord">

        <field-validator type="requiredstring">

            <message key="requiredstring"/>

        </field-validator>

    </field>

</validators>

 

十二、       语言文件resources/test/struts/action/ package.properties

Login.title= Login - Test page

requiredstring = ${getText(fieldName)} is required.

user.passWord = Password

user.userName = User Name

 

十三、       项目文件结构

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值