Struts2实现简单登录功能

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

   

相关文章
相关标签/搜索