Struts2用户登陆

任务需求

创建struts2项目,创建用户登陆实例,朴素版

代码

java类

LoginActon


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{
        /*
        System.out.println("execute is called");
        System.out.println("execute is called" +username);
        System.out.println("execute is called"+password);
        */
        if("root".equals(username)&&"123".equals(password)){
            return "success";
        }else{
            return "error";
        }
    }

}

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="struct2"  extends="struts-default">
        <action name="login" class="com.first.action.LoginAction">
            <result name="success">/login_success.jsp</result>
            <result name="error">/login_error.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">
    <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>
</web-app>

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: 长风
  Date: 2019/10/24
  Time: 21:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>用户登陆</title>
</head>
<body>
<form action="login.action">
    用户:<input type="text" name= "username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="登陆">
</form>
</body>
</html>

login_success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: 长风
  Date: 2019/10/24
  Time: 21:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>成功!</title>
</head>
<body>

<s:property value="username"/> 登陆成功
</body>
</html>

login_error.jsp

<%--
  Created by IntelliJ IDEA.
  User: 长风
  Date: 2019/10/24
  Time: 21:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>失败!</title>
</head>
<body>
登陆失败<a href="login.jsp">返回</a>
</body>
</html>

发布了33 篇原创文章 · 获赞 22 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/JY_WD/article/details/102787284
今日推荐