Struts2實現簡單登陸功能


1.首先准備Struts2的必備jar包

2.Struts2攔截用戶請求

在web.xml文件中配置Struts2的核心控制器,用來攔截客戶端的請求,並將請求轉發到相應的Action類中來處理,web.xml文件如下:

<?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.jsp

login.jsp可以使用Struts2標簽庫實現一個表單,登陸成功進入index.jsp界面,登陸失敗返回login.jsp界面

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界面代碼如下:

 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.創建業務控制器LoginAction

LoginAction類作為邏輯控制器組件,定義checkLogin方法處理登陸驗證邏輯,代碼如下:

 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.配置LoginAction

當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

   

 


免責聲明!

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



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