Struts 2.0支持更高級的POJO訪問,先定義一個User.java類。把username和password屬性,從Login類中分離出來,它可以使系統結構更清晰,對開發多層系統尤其有用。
User.java
package com.bulktree.struts2;
public class User {
private String username;
private String password;
public String getUsername() {
System.out.println("******getUsername()******");
return username;
}
public void setUsername(String username) {
System.out.println("*******setUsername()********");
this.username = username;
}
public String getPassword() {
System.out.println("******getPassword()*******");
return password;
}
public void setPassword(String password) {
System.out.println("******setPassword()*********");
this.password = password;
}
}
login2.jsp
<s:form action="login2" method="post">
<s:textfield name="user.username" label="USERNAME" tooltip="Enter Your UserName"></s:textfield>
<s:password name= "user.password" label="PASSWORD" tooltip="Enter Your PassWord"></s:password>
<s:submit label="login"></s:submit>
</s:form>struts.xml文件中action配置:
<action name="login2" class="com.bulktree.struts2.LoginAction2">
<result name="success">/welcome.jsp</result>
<result name="input">/login2.jsp</result>
</action>
LoginAction2.java:
package com.bulktree.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction2 extends ActionSupport {
private User user;
private String message;
public void setUser(User user) {
System.out.println("********setUser()***********");
this.user = user;
}
public User getUser() {
System.out.println("*********getUser()*************");
return user;
}
public String getMessage() {
System.out.println("************getMessage()********");
return message;
}
@Override
public String execute() throws Exception {
System.out.println("*****execute()******");
if("bulktree".equals(user.getUsername()) && "oakertree".equals(user.getPassword())) {
message = "Welcome, " + user.getUsername();
} else {
message = "Error!";
}
return ActionSupport.SUCCESS;
}
@Override
public void validate() {
System.out.println("******validate()******");
if(null == user.getUsername() || user.getUsername().length() < 5) {
this.addFieldError("username","USERNAME ERROR");
}
if(null == user.getPassword() || user.getPassword().length() < 5) {
this.addFieldError("password","PASSWORD ERROR");
}
}
}
welcome.jsp:
YOUR USERNAME:<FONT size="5" color="red"><s:property value="user.username"/></FONT><br>
YOUR PASSWORD:<FONT size="5" color="red"><s:property value="user.password"/></FONT><br>控制台輸出及分析:
*********getUser()************* //得到User類的實例
********setUser()*********** //執行setUser(),准備賦值操作
******setPassword()********* //set賦值操作,此時是真正的賦值
*********getUser()************* //得到User類的實例
*******setUsername()******** //set賦值操作,此時是真正的賦值
以上有struts2框架自動執行
******validate()****** //表單驗證
******getUsername()****** //表單驗證時調用get方法(判斷username)
******getUsername()****** //表單驗證時第二次調用get方法(判斷username)
******getPassword()******* //表單驗證時調用get方法(判斷password)
******getPassword()******* //表單驗證時第二次調用get方法(判斷password)
*****execute()****** //業務邏輯
******getUsername()****** //調用get方法
******getPassword()******* //調用get方法
一下由Struts2框架自動執行
*********getUser()************* //得到User類的實例
******getUsername()****** //自動執行get方法
********getUser()************* //得到User類的實例
******getPassword()******* //自動執行get方法
我們可以把類的屬性分離出來實現系統架構清晰、明了。