我有问题......
首先,我是Struts 2和Java 的新手,但是需要来了解我的工作。我正在尝试将 LoginUser 操作类传递给 UserBean Java Bean ... < / p>
当LoginUser Action执行时,通过标记从userBean调用 fullName 的bean值。
但是当我点击success.jsp中的更多信息链接时,以下页面还会通过标记调用bean值 fullName ,但这次是 null ,如果我返回success.jsp,它也是 null 。
我的问题 为什么bean没有像我们想要的那样保留信息。
技术说明:我正在开发使用Eclipse IDE作为Maven项目,并且只使用Apache Tomcat 7来测试/部署。
来自JAVA和JSP文件的代码:
LoginUser 操作
package test.Home1;
@SuppressWarnings("unused")
public class LoginUser {
private String username = "";
private String password = "";
private String userOK = "admin";
private String passOK = "admin";
// Password getters/setters
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
// Username getters/setters
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
// Bean is instantiated here
private UserBean userBean = new UserBean();
// userBean getters/setters
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}
public UserBean getUserBean() {
return userBean;
}
// Execute the following action when this class is called via struts action
public String execute() {
if (this.username.equalsIgnoreCase(userOK) && this.password.equals(passOK)) {
userBean.setAccessLevel("full");
userBean.setFullName("Administrator");
userBean.setUserName(username);
return "SUCCESS";
}
else {
return "FAIL";
}
}
}
UserBean BEAN
package Test.Home1;
public class UserBean {
private String userName;
private String password;
private String fullName;
private String accessLevel;
private boolean LoggedIn = false;
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 getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getAccessLevel() {
return accessLevel;
}
public void setAccessLevel(String accessLevel) {
this.accessLevel = accessLevel;
}
public boolean isLoggedIn() {
return LoggedIn;
}
public void setLoggedIn(boolean loggedIn) {
LoggedIn = loggedIn;
}
}
login.jsp JSP表单登录页面
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>
Hello World at Home (Struts 2 Tutorial)
</title>
</head>
<body style="font-family: Arial;">
<h1>Please Log in</h1>
<s:form action="LoginUser" autocomplete="off">
<s:textfield name="username" label="Username"/>
<s:password name="password" label="Password"/>
<s:submit/>
</s:form>
</body>
</html>
success.jsp JSP登录成功页面
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>
Hello World at Home (Struts 2 Tutorial) - LOGGED IN
</title>
</head>
<body style="font-family: Arial;">
<h1>Struts 2 Hello World Example</h1>
<h4>Hello <s:property value="userBean.fullName" />
</h4>
<br><br>
<s:a value="Info">More Info</s:a>
</body>
</html>
moreInfo.jsp * JSP FILE
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>
Hello World at Home (Struts 2 Tutorial) - LOGGED IN
</title>
</head>
<body style="font-family: Arial;">
<h1>Are you still there <s:property value="userBean.fullName" />?</h1>
<h4>Can you still see your name above?</h4>
<br><br>
<s:a value="Home">Home</s:a>
</body>
</html>
答案 0 :(得分:0)
动作bean仅在同一请求期间存在,但如果您按下该链接,则会调用另一个userBean
未创建 的请求。如果提交表单时,Struts会填充bean null
。
当OGNL尝试从包含参数名称的表达式填充bean时,默认行为为createIfNull==true
。由于您没有提供任何带有bean名称的参数,因此不会填充任何内容。
如果您添加一些参数,例如此代码
<s:a action="ShowUser"><s:param name="userBean.fullName" value="Administrator"/> More Info</s:a>
然后它会创建一个userBean
并使用参数的值初始化其属性fullName
。操作名称应该不同,它应该映射到另一个方法而不是execute()
,如果在操作配置中没有指定方法属性,则默认使用该方法。