struts2简单登陆页面

首先,先看一下该项目的目录结构,主要看左侧的导航栏的各个文件所属的包以及文件名及后缀!!

 

下面我给出各个项的源码,再对这个登陆页面进行一些我的理解,希望我的讲解可以帮助你去理解这个工作的原理!

web.xml文件源码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

struts.xml源码:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="example.xml"/>
    <!-- Configuration for the default package. -->
    <package name="zzf" extends="struts-default">
        <action name="login" class="loginAction.LoginAction">
            <result name="error">/login/fail.jsp</result>
            <result name="success">/login/success.jsp</result>
        </action>
    </package>
</struts>

login.jsp文件源码:

<%-- 
    Document   : login
    Created on : 2020-3-25, 09:02:38
    Author     : 刘标!
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>基于Struts2的登陆系统应用</title>
    </head>
    <body>
        <form action="login" method="post">
            用户名:<input name="userName" type="text" size="26">
            <br>
            密 码 :<input name="passWord" type="password" size="26">
            <br>
            <input type="submit" value="登陆">
        </form>
        <hr>
    </body>
</html>

success.jsp文件源码:

<%-- 
    Document   : success
    Created on : 2020-3-10, 22:10:47
    Author     : 标哥!
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>登陆成功页面</title>
    </head>
    <body>
        <h1>你登陆成功,欢迎你!</h1>
    </body>
</html>

fail.jsp文件源码:

<%-- 
    Document   : fail
    Created on : 2020-3-26, 21:03:08
    Author     : 标哥!
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>登陆失败页面</title>
    </head>
    <body>
        <h1>登陆失败,用户名或密码错误!请返回,重新输入!</h1>
    </body>
</html>

LoginAction.java文件源码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package loginAction;

import loginBean.LoginBean;

/**
 * Document:LoginAction
 * Created on : 2020-3-25, 09:02:02
 * @author 刘标!
 */
public class LoginAction {
    private String userName;
    private String passWord;

    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;
    }
    public String execute() throws Exception {
        LoginBean lb = new LoginBean();
        if(lb.login(userName,passWord)){
            return "success";
        } else {
            return "error";
        }
    }
}

LoginBean.java文件源码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package loginBean;

/**
 * Document:LoginBean
 * Created on : 2020-3-25, 09:07:11
 * @author 刘标!
 */
public class LoginBean {
    private String userName;
    private String passWord;

    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;
    }
    public boolean login(String userName,String passWord){
        boolean b = false;
        if(userName.equals("QQ") && passWord.equals("123")){
            b = true;
            return b;
        }
        else{
            b = false;
            return b;
        }
    }
}

首先,我们使用tomcat服务器来运行这个项目文件中的登陆首页,也就是login.jsp,运行他之后,成功的话会自动跳转到浏览器页面,显示的内容即为该jsp所定义的内容。如果想要各项文件起作用,必须要求用户点击登陆按钮,在用户输入完用户名和密码后,点击登陆这个事件驱动,这时,由于form表单中的action属性为login,这时候struts.xml文件响应,这是因为在struts文件里面有定义相应的action,该action的属性名与login.jsp中action属性名一致,处理该action,同时,在struts文件中,还设置了class属性(class="loginAction.LoginAction"),他将执行loginAction包下的LoginAction这个文件,即为loginAction.java文件,在这个文件中,定义了两个私有类,包括他们两个的set、get方法外,仅额外增加了一个execute方法,该方法调用了另外一个java类文件,即loginBean文件下的LoginBean.java文件,将其实例化后进行调用它里面的login方法。而在LoginAction.java文件中,同样是定义了两个私有变量和set、get方法,其中的login方法类型为boolean型,由于该项目过于简单,没有引入数据库,就将用户名和密码设置为一个定量,如果用户输入的内容与其匹配则返回相应的布尔值,反之亦然。接着将布尔值返回给LoginAction.java中的调用者,这时,LoginAction.java再返回相应的字符串给调用者,即为struts.xml文件,这时相应用户的事件已经处理完成,再struts文件中的action标签下的result标签开始匹配返回的字符串,如果匹配的字符串为error则将页面跳转到login文件下的fail.jsp给用户,告知用户输入的结果,反之将页面跳转到success.jsp页面。这个登陆界面的基本过程就是这样的了,希望对你有所帮助。

posted @ 2020-03-26 21:37  恶魔岛  阅读(458)  评论(0编辑  收藏  举报