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

   

 

posted @ 2015-07-18 10:08  奈河桥  阅读(7602)  评论(0编辑  收藏  举报