Struts2開發環境搭建,及一個簡單登錄功能實例


首先是搭建Struts2環境。

第一步 下載Struts2
去Struts官網 http://struts.apache.org/ 下載Struts2組件。
截至目前,struts2最新版本為2.3.1.3,下載struts-2.3.16.3-all.zip,解壓,放着。

第二步 新建Web Project並導入jar包
在MyEclispe中新建Web Project,然后找到解壓的Struts2包,在里面apps文件夾下找到struts2-blank.war,解壓這個WAR文件,將里面WEB-INF\lib目錄下的jar文件全部復制到新建的Web Project的WebRoot\WEB-INF\lib目錄下。

第三步 配置web.xml
在項目的WebRoot\WEB-INF\目錄下找到web.xml文件,沒有就新建一個web.xml文件,在里面添加如下代碼:

<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.xml文件的示例:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>web1</display-name>
    <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>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

第四步 配置struts.xml
在項目的src目錄下找到struts.xml文件,沒有就新建一個,里面代碼如下:

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

<struts>
    <package name="main" extends="struts-default">
        <!-- 在這里面配置action -->
    </package>
</struts>

到此,Struts2開發環境搭建完成。

下面演示一個登錄頁面實例。

第一步 修改Index.jsp
修改Index.jsp,做出登錄界面。
下面是index.jsp的代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
  <head>
    <title>登錄</title>
  </head>
  
  <body>
  <form action="login" method="post">
    登錄<br />
    賬號:<input type="text" name="username" /><br />
    密碼:<input type="password" name="password" /><br />
    <input type="submit" value="登錄" />
    </form>
  </body>
</html>

下面是Index.jsp在瀏覽器中的效果:

第二步 編寫驗證賬戶和密碼的類
新建LogAction類,讓其繼承com.opensymphony.xwork2.ActionSupport類,注意到index.jsp中兩個輸入框的name屬性分別是username和password,所以LogAction類必須包含下面兩個屬性:
private String username
private String password
而且必須寫出他們的get、set方法。

然后,編寫execute方法,在execute方法中驗證賬號和密碼並返回String類型的結果,execute方法會在該Action類被調用的時候自動執行。

下面是LogAction.java的完整代碼:

package com.lidi.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class LogAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private String username;//賬號
    private String password;//密碼
    
    //getters & setters
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    /**
     * execute方法會在該Action類被調用的時候自動執行,
     * 如果 賬號="admin"並且密碼="123456",就返回SUCCESS
     * 否則返回ERROR
     */
    public String execute(){
        if(username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("123456")){
            return SUCCESS;
        }
        else
            return ERROR;
    }
}

上面的返回SUCCESS和返回ERROR什么意思呢,后面再講。

第三步 配置struts.xml
第二步編寫了Action類,第三步將該Action配置到struts.xml中,打開struts.xml,在package標簽中添加如下代碼:

<action name="login" class="com.lidi.struts.action.LogAction">
    <result name="success">success.jsp</result>
    <result name="error">error.jsp</result>
</action>

action標簽的name屬性為login,這個必須與index.jsp中form標簽的action屬性一致,class屬性填寫LogAction類的全稱。

<result name="success">success.jsp</result> 這個標簽的意思是當LogAction類的execute方法返回SUCCESS時,頁面跳轉到success.jsp;同理,<result name="success">success.jsp</result> 這個標簽的意思是當LogAction類的execute方法返回ERROR時,頁面跳轉到error.jsp。

完整的struts.xml代碼如下:

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

<struts>
    <package name="main" extends="struts-default">
        <action name="login" class="com.lidi.struts.action.LogAction">
            <result name="success">success.jsp</result>
            <result name="error">error.jsp</result>
        </action>
    </package>

</struts>

這里用到了success.jsp和error.jsp,在項目中新建這兩個文件,success.jsp表示登錄成功后的頁面,上面顯示登錄用的賬戶和密碼,error.jsp表示登錄失敗后的頁面,上面顯示錯誤提示就就好了,他們的代碼分別如下:
success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML>
<html>
  <head>
    <title>登陸成功</title>
  </head>
  
  <body>
    歡迎<s:property value="username" />,登錄成功!<br />
  </body>
</html>

<%@ taglib prefix="s" uri="/struts-tags"%>表示引用struts標簽庫
<s:property value="username" />是struts標簽,用以顯示登錄頁面傳遞過來的賬號。

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
  <head>
    <title>登錄失敗</title>
  </head>
  
  <body>
    登錄失敗!用戶名或密碼錯誤!
  </body>
</html>

第四步 運行
配置struts.xml后要重啟下服務器,然后在瀏覽器中查看效果。
分別輸入賬號和密碼並登錄,如果賬號和密碼分別為admin和123456,頁面就會顯示
歡迎admin,登錄成功!
否則就會顯示
登錄失敗!用戶名或密碼錯誤!

第五步 程序運行原理淺析
用戶填寫賬號密碼並點擊登錄后,瀏覽器會請求form標簽action屬性里面的鏈接,也就是login,
服務器中,過濾器攔截到login這個請求,就會查找struts.xml中name=login的action,再找到這個action的class屬性對應的類,也就是com.lidi.struts.action.LogAction,然后實例化一個LogAction對象,並且把參數username和password分別賦給這個對象的username和passwrod屬性(這就是為什么LogAction類的兩個屬性名稱必須和index.jsp中兩個文本框的name屬性分別相同,並且必須添加他們的get和set方法),然后執行這個對象的execute方法,並返回一個字符串,如果返回SUCCESS字符串,就查找struts.xml中對應action的<result>標簽中name屬性等於success的<result>標簽,並將頁面轉到該標簽里面配置的頁面success.jsp上。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM