Struts2開發環境搭建 附簡單登入功能執行個體_java

來源:互聯網
上載者:User

首先是搭建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上。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.