The value-passing method of Struts2 project construction (2)

illustrate

  • This article introduces the method and code of passing values ​​from the background to the foreground

Ready to work

Please configure and learn Logger4j by yourself. The following MyLog4J class inherits the properties of Logger4j and prints out the log.

struts.xml

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <include file="struts-default.xml"/>
    <package name="login.action.LoginAction" extends="struts-default" namespace="/">
        <action name="login" class="login.action.LoginAction">
            <result name="success" type="dispatcher">/login/login.jsp</result>
            <result name="login" type="dispatcher">/index.jsp</result>
        </action>
    </package>
</struts>

login.jsp

<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
    <title>登入成功界面</title>
</head>
<body>
登入成功
</body>
</html>

Struts2 pass value method:

1. Use domain objects
2. Use ModelDriven action
3. Use Action properties

1. Using Domain Objects

LoginAction code section

public class LoginAction extends ActionSupport {
    private LoginEntity loginEntity;

    @Override
    public String execute() throws Exception {
        if (loginEntity != null && loginEntity.getUserName().equals("erciyuan") && loginEntity.getUserPassword().equals("12345678")) {
            return SUCCESS;
        } else {
            if (loginEntity != null)
                loginEntity.setIsLogin("用户名或密码错误");
            return LOGIN;
        }
    }

    public LoginEntity getLoginEntity() {
        return loginEntity;
    }

    public void setLoginEntity(LoginEntity loginEntity) {
        this.loginEntity = loginEntity;
    }
}

index.jsp code section

<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
    <title>登入界面</title>
</head>
<body>
<form action="login.action" name="login.LoginAction">
    <input type="text" id="login_user_name" name="loginEntity.userName" placeholder="请输入名称"/>
    <input type="password" id="login_user_password" name="loginEntity.userPassword" placeholder="请输入密码"/>
    <div id="login_is_userName">${loginEntity.isLogin}</div>
    <input type="submit" id="login" value="登入"/>
</form>
</body>
</html>
Startup project
Enter the wrong password once
return result

2. Use ModelDriven action

LoginAction code section

public class LoginAction extends ActionSupport implements ModelDriven<LoginEntity> {
    private LoginEntity loginEntity = new LoginEntity();

    @Override
    public String execute() throws Exception {
        if (loginEntity != null && loginEntity.getUserName().equals("erciyuan") && loginEntity.getUserPassword().equals("12345678")) {
            return SUCCESS;
        } else {
            if (loginEntity != null)
                loginEntity.setIsLogin("用户名或密码错误");
            return LOGIN;
        }
    }

    public LoginEntity getModel() {
        return loginEntity;
    }
}

index.jsp code section

<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
    <title>登入界面</title>
</head>
<body>
<form action="login.action" name="login.LoginAction">
    <input type="text" id="login_user_name" name="userName" placeholder="请输入名称"/>
    <input type="password" id="login_user_password" name="userPassword" placeholder="请输入密码"/>
    <div id="login_is_userName">${isLogin}</div>
    <input type="submit" id="login" value="登入"/>
</form>
</body>
</html>
Startup project
Enter the wrong password once
return result

3. Use the properties of Action

LoginAction code section

public class LoginAction extends ActionSupport implements Action {
    private String userName;//用户名
    private String userPassword;//密码
    private String isLogin;//判断登入情况

    @Override
    public String execute() throws Exception {
        if (userName != null && userPassword != null && userName.equals("erciyuan") && userPassword.equals("12345678")) {
            return SUCCESS;
        } else {
            setIsLogin("用户名或密码错误");
            return LOGIN;
        }
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getIsLogin() {
        return isLogin;
    }

    public void setIsLogin(String isLogin) {
        this.isLogin = isLogin;
    }
}

index.jsp code section

<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
    <title>登入界面</title>
</head>
<body>
<form action="login.action" name="login.LoginAction">
    <input type="text" id="login_user_name" name="userName" placeholder="请输入名称"/>
    <input type="password" id="login_user_password" name="userPassword" placeholder="请输入密码"/>
    <div id="login_is_userName">${isLogin}</div>
    <input type="submit" id="login" value="登入"/>
</form>
</body>
</html>
Startup project
Enter the wrong password once
return result












I am a programmer. If you think it's well organized, please pay attention to your personal WeChat public account (scan it):

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326005626&siteId=291194637