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>


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值