Struts2 基于注釋的登陸與數據驗證示例

openkk 14年前發布 | 61K 次閱讀 Struts2 Web框架

    Struts2是個優秀的MVC框架。有人說java的框架是把簡單的東西搞得復雜,而我不這樣認為。學習框架之前可能會這樣認為,學了之后就會感覺框架當然是在簡化問題,否則也不會有這么多人用了。本文介紹如何用eclipse如何創建一個Struts2基于注解的登陸驗證動態網頁工程。通俗的說來,我們用瀏覽器發送一個請求到服務器,服務器驗證通過則進入歡迎頁,否則給出錯誤提示。

     在eclipse中新建名為Struts2Demo項目,要在項目中使用Struts2框架,先要下載Struts2的相關jar包,在本站可以很容易搜索到。然后把需要的jar包添加到項目的lib目錄中。項目各文件結構如下圖:

Struts2基于注釋的登陸與數據驗證示例Struts2基于注釋的登陸與數據驗證示例

    為了使項目支持Struts2框架,配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>

    再配置struts.xml文件,此文件一定要位于src文件目錄下,否則配置無效。我做此項目時由于此文件沒放在src目錄下,而是放在了action所在的包中,導致一直運行不出來,浪費的半天時間。 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>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.i18n.reload" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    <constant name="struts.action.extension" value="do,action,," />
</struts>

再看用戶的請求頁面:

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Welcome User</title>
<s:head />
</head>
<body>
<s:form action="welcome">
    <s:textfield name="userName" label="User Name" />
    <s:submit />
</s:form>
<s:debug></s:debug>
</body>
</html>

Struts2基于注釋的登陸與數據驗證示例

上面的頁面中form的action為welcome意思是頁面提交到名為welcome頁面或action處理。產后的Url請求為http://localhost:8080/Struts2Demo/welcome。然后我們創建一個處理此請求的action。

package org.sunhing.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Action(value="/welcome",results={
        @Result(name = "success", location = "/welcome.jsp"),
        @Result(name = "input", location = "/index.jsp")}
)
public class Welcome extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private String userName;
    private String message;

    public String execute() {
        setMessage("Hello " + getUserName());
        S
        return SUCCESS;
    }

    @Override
    public void validate() {
        if("".equals(userName)){
            addFieldError("userName", "用戶名不能為空!");
        }
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getUserName() {
        return userName;
    }

    public String getMessage() {
        return message;
    }
}

     以上代碼中的注解:
@Action(value="/welcome",results={ @Result(name = "success", location = "/welcome.jsp"), @Result(name = "input", location = "/index.jsp")} )

    意思即為當我們的請求為/weclome時此請求交給此action處理。當上面的validate函數中校驗有錯誤時會返回"input",此時Struts2找到要跳轉的jsp頁面index.jsp,如下圖:

Struts2基于注釋的登陸與數據驗證示例Struts2基于注釋的登陸與數據驗證示例

若校驗通過則跳轉到welcome.jsp。下面是welcome.jsp頁面代碼:

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<s:property value="message"></s:property>
<s:debug></s:debug>
</body>
</html>

    總結一下,Struts2為我們封裝了很多常用的功能,避免了我們“重復造輪子”。

  •     請求參數的自動獲取。我們要做的是保證表單與要處理的action中的屬性名一致即可(準確的說只需保持與屬性對應getXXX()方法中的XXX一致即可)。
  •      頁面的自動跳轉。 ActionSupport類已經為我們封裝了大量常用的方法,繼承此類后我們可以直接調用此類的很多方法,完成我們需要的功能。
  •     提供標簽,大大的方便了我們的調試。

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!