Struts2-簡單登錄實現

一、效果圖

在這裏插入圖片描述
在這裏插入圖片描述

二、需要的Jar包

  • 這裏麪包含了部分Spring的jar包,如果只用struts,只導入struts的即可
    在這裏插入圖片描述
  • 我是用idea構建的項目,當然也可以手動下載jar包,Jar包下載地址
    在這裏插入圖片描述

三、項目結構及代碼

在這裏插入圖片描述

login.jsp

<%--
  Created by Leo.
  Date: 10/16/2019
  Time: 9:01 PM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
</head>
<body>
<form action="login.action" method="post">
    <label>
        用戶名
        <input type="text" name="username">
    </label><br>
    <label>
        密碼
        <input type="password" name="password">
    </label><br>
    <input type="submit" value="登錄">
    <input type="reset" value="重置">
</form>
</body>
</html>

index.jsp

<%--
  User: Leo
  Date: 10/16/2019
  Time: 7:30 PM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>主頁</title>
  </head>
  <body>
    <p1>歡迎---${param.username}---登錄成功!</p1>
  </body>
</html>

LoginAction

package action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: LoginAction
 * @Author: Leo
 * @Description:
 * @Date: 10/16/2019 8:39 PM
 */
public class LoginAction extends ActionSupport {
    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;
    }

    @Override
    public String execute() throws Exception {
        if ("admin".equals(username) && "123".equals(password)) {
            return "success";
        } else {
            return "input";
        }
    }
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

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

<struts>
    <package name="leo" namespace="/" extends="struts-default">
        <action name="login" class="action.LoginAction">
            <result name="success">
                index.jsp
            </result>

            <result name="input">
                login.jsp
            </result>
        </action>
    </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
         version="4.0">
    <!--Struts2 核心控制器-->
    <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>
    
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章