Struts2 使用域模型給action傳遞引數以及DTO
阿新 • • 發佈:2019-01-22
域模型:
以使用者登入為例: 定義一個User類物件us,設定其中變數資訊,getter和setter。在LoginAction類中申明一個user類物件(不需要new,Struts2會自行new出一個對像),以及user的getter和setter。 在使用者登入的jsp頁面中,提交資訊為:us.XXX,XXX為user類中成員變數的名字:如us.userName. User類程式碼:package model; public class User { private String userName; private String userPassword; 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; } }
LoginAction程式碼:
package action; import com.opensymphony.xwork2.ActionSupport; import model.User; public class LoginAction extends ActionSupport{ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute(){ System.out.println(user.getUserName()); System.out.println(user.getUserPassword()); return this.SUCCESS; } }
Login.jsp程式碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title><s:text name="userlogin.title" /></title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <s:form action="login.action" method="post"> <table> <tr> <td> </td> <td> <s:textfield name="user.userName" label="使用者名稱"/> </td> </tr> <tr> <td> </td> <td> <s:password name="user.userPassword" label="密碼"/> </td> </tr> <tr> <td colspan="2" align="right"> <s:submit value="%{getText('userlogin.submit')}" /> </td> </tr> </table> </s:form> </center> </body> </html>
DTO:
當jsp傳遞引數數量與user的成員變數數量不一致時,struts2無法給user模型引數注入,會出現如下報錯資訊:
ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor - Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'loginname' on 'class com.opensymphony.xwork2.ActionSupport: Error setting expression 'loginname' with value ['admin', ]
此時需要使用dto 定義一個UserDTO類,其中包含的成員變數與jsp頁面傳遞的引數完全一致。在UserLogin中宣告一個UserDTO,以及其getter和setter。 再new一個User物件,將UserDTO中User物件需要的成員變數傳遞給User類物件。 User類程式碼:
package model;
public class User {
private int userId;
private String userName;
private String userPassword;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
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;
}
}
UserDTO類程式碼:
package dto;
public class UserDto {
private String userName;
private String userPassword;
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;
}
}
LoginAction程式碼:
package action;
import com.opensymphony.xwork2.ActionSupport;
import dto.UserDto;
import model.User;
public class LoginAction extends ActionSupport{
private User user= new User();
private UserDto udto;
public UserDto getUdto() {
return udto;
}
public void setUdto(UserDto udto) {
this.udto = udto;
}
public String execute(){
user.setUserName(udto.getUserName());
user.setUserPassword(udto.getUserPassword());
System.out.println(user.getUserName());
System.out.println(user.getUserPassword());
return this.SUCCESS;
}
}
Login.jsp程式碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title><s:text name="userlogin.title" /></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<s:form action="login.action" method="post">
<table>
<tr>
<td>
</td>
<td>
<s:textfield name="udto.userName" label="使用者名稱"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<s:password name="udto.userPassword" label="密碼"/>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<s:submit value="%{getText('userlogin.submit')}" />
</td>
</tr>
</table>
</s:form>
</center>
</body>
</html>