1.首先準備Struts2的必備jar包css
2.Struts2攔截用戶請求html
在web.xml文件中配置Struts2的核心控制器,用來攔截客戶端的請求,並將請求轉發到相應的Action類中來處理,web.xml文件以下:java
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>struts2Demo</display-name> <!-- Struts2核心控制器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3.建立視圖頁面login.jsp及index.jspweb
login.jsp能夠使用Struts2標籤庫實現一個表單,登錄成功進入index.jsp界面,登錄失敗返回login.jsp界面apache
login.jsp界面代碼以下:服務器
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 3 <!-- 引入Struts2標籤庫 --> 4 <%@ taglib prefix="s" uri="/struts-tags" %> 5 6 <!DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="UTF-8" /> 10 11 <style type="text/css">*{font-size:12px;}</style> 12 13 <title> 登錄頁面 </title> 14 </head> 15 16 <body> 17 <div style="margin:0 auto;"> 18 <div style="font-size:14px;font-weight:bold;">用戶登錄</div> 19 <div> 20 <s:form action="checkLogin" namespace="/login"> 21 <s:textfield name="username" style="font-size:12px;width:120px;" label="登錄名稱" /> 22 <s:password name="password" style="font-size:12px;width:120px;" label="登錄密碼" /> 23 <s:submit value="登錄" /> 24 </s:form> 25 </div> 26 </div> 27 </body> 28 </html>
index.jsp界面代碼以下:app
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8" /> 7 <title> 主頁 </title> 8 </head> 9 10 <body> 11 <h1>登錄成功,歡迎您!</h1> 12 </body> 13 </html>
4.建立業務控制器LoginActionjsp
LoginAction類做爲邏輯控制器組件,定義checkLogin方法處理登錄驗證邏輯,代碼以下:this
1 package action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 /** 6 * @author Sere 7 * @date:2015年7月18日 上午9:04:18 8 * 類說明 9 */ 10 public class LoginAction extends ActionSupport{ 11 12 private static final long serialVersionUID = 7922979648150320921L; 13 14 private String username; 15 private String password; 16 17 /** 18 * 處理客戶端請求的method,對應struts.xml文件 19 * @author Sere 20 * @date:2015年7月18日 上午9:06:22 21 * */ 22 public String checkLogin(){ 23 if(this.username.endsWith("sere")&&this.password.equals("123")){ 24 return SUCCESS; //登錄成功返回SUCCESS 25 }else{ 26 return LOGIN; //登錄失敗返回LOGIN 27 } 28 } 29 30 31 public String getUsername() { 32 return username; 33 } 34 public void setUsername(String username) { 35 this.username = username; 36 } 37 public String getPassword() { 38 return password; 39 } 40 public void setPassword(String password) { 41 this.password = password; 42 } 43 }
5.配置LoginActionurl
當Action處理完請求後返回一個字符串,每一個字符串對應一個視圖。在Struts.xml文件中配置Action時,name定義該Action的名稱,class定義這個Action實際實現類,method表示這個Action中的實際實現方法。
在Struts.xml文件中,每一個Action文件都指定了result元素,每一個result元素都定義了一個邏輯視圖,其中的name屬性定義了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> <include file="struts-default.xml" /> <package name="struts2_login" extends="struts-default" namespace="/login"> <action name="checkLogin" class="action.LoginAction" method="checkLogin"> <result name="success">/index.jsp</result> <result name="login">/login.jsp</result> </action> </package> </struts>
6.部署
struts.xml文件放在WEB-INF\classes目錄下,jsp放在Webroot下,結構以下:
最後,部署到Tomcat服務器上打開login.jsp頁面,用戶名成功跳轉index.jsp,失敗跳轉login.jsp