1.获取Action类中的属性值
- Action类:LoginTest.java
package test;
public class LoginTest {
private String userName = "";
private String passWord = "";
public String execute() {
if(this.userName .equals( this.passWord))
return "SUCCESS";
else return "ERROR";
}
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;
}
}
- 测试主页index.jsp
<body>
<s:form action="/logintest.action">
<s:textfield name="userName" label="UserName"></s:textfield>
/*此处name值要和Action类中的对应的属性名称相同,且必须有该属性的getter和setter方法*/
<s:password name="passWord" label="PassWord"></s:password>
<s:submit value="Click Here"></s:submit>
</s:form>
</body>
- 测试登陆成功页面test.jsp
<body>
<h1>Hello ,<s:property value="userName"/> </h1>
</body>
(引用标签:<%@ taglib prefix="s" uri="/struts-tags"%>
)
点击按钮跳转到test.jsp
2. pageContext, request, session、application四大作用域中获取值
- 在Action类中实现SessionAware 接口:
public class SessionTest1Action extends ActionSupport implements SessionAware {
private Map session;
public void setSession(Map session) {
this.session = session;
}
public String execute() {
this.session.put("username", "ABC");
return SUCCESS;
}
}
- jsp页面取值:
<s:property value="#session.username"/>