说明
- 本文介绍了后台传值到前台的方式和代码
准备工作
Logger4j请自行配置和学习,以下MyLog4J类就是继承了Logger4j的属性,打印输出日志
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传值方式:
1.使用领域对象
2.使用ModelDriven action
3.使用Action的属性
1.使用领域对象
LoginAction 代码部分
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代码部分
<%@ 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>
启动项目
输入一次错的密码
返回结果
2.使用ModelDriven action
LoginAction 代码部分
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代码部分
<%@ 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>
启动项目
输入一次错的密码
返回结果
3.使用Action的属性
LoginAction代码部分
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代码部分
<%@ 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>