Struts2 使用域模型給action傳遞參數以及DTO

域模型:

以用戶登錄爲例:
定義一個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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章