struts2的domainmodel接受参数

domainmodel(域模型):

 

使用domainModel传参数时:可以自己new一个实例,也可以通过struts2来构建,struts2只在传入数值时才会new一个对象,但是要编写一个参数为空的构造方法。

 

以用户登录为例:

定义一个User类对象us,设置其中变量信息,getter和setter。在LoginAction类中申明一个user类对象(不需要new,Struts2会自行new出一个对像),以及user的getter和setter。

 

当Action被调用时,struts2框架会自动调用在Action中定义的User类的一个对象user的setName( )方法,将前台数据传递并赋值给user。因此在UserAction中一定要定义好User实体类的setter和getter方法,否则会报空指针错误。

 

在用户登录的jsp页面中,提交信息为:us.XXX,XXX为user类中成员变量的名字:如us.userName.

 

User类代码:

[java] view plain copy

  1. package model;  
  2.   
  3. public class User {  
  4.     private String userName;  
  5.     private String userPassword;  
  6.     public String getUserName() {  
  7.         return userName;  
  8.     }  
  9.     public void setUserName(String userName) {  
  10.         this.userName = userName;  
  11.     }  
  12.     public String getUserPassword() {  
  13.         return userPassword;  
  14.     }  
  15.     public void setUserPassword(String userPassword) {  
  16.         this.userPassword = userPassword;  
  17.     }  
  18. }  

 

LoginAction代码:

[java] view plain copy

  1. package action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. import model.User;  
  6.   
  7. public class LoginAction extends ActionSupport{  
  8.     private User user;  
  9.   
  10.       
  11.     public User getUser() {  
  12.         return user;  
  13.     }  
  14.   
  15.     public void setUser(User user) {  
  16.         this.user = user;  
  17.     }  
  18.   
  19.     public String execute(){  
  20.         System.out.println(user.getUserName());  
  21.         System.out.println(user.getUserPassword());  
  22.         return this.SUCCESS;  
  23.     }  
  24.   
  25. }  

 

Login.jsp代码:

[html] view plain copy

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title><s:text name="userlogin.title" /></title>  
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   </head>  
  23.     
  24.   <body>  
  25.     <center>  
  26.         <s:form action="login.action" method="post">  
  27.             <table>  
  28.                 <tr>  
  29.                     <td>  
  30.                     </td>  
  31.                     <td>  
  32.                         <s:textfield name="user.userName" label="用户名"/>  
  33.                     </td>  
  34.                 </tr>  
  35.                 <tr>  
  36.                     <td>  
  37.                     </td>  
  38.                     <td>  
  39.                         <s:password name="user.userPassword" label="密码"/>  
  40.                     </td>  
  41.                 </tr>  
  42.                 <tr>  
  43.                     <td colspan="2" align="right">  
  44.                         <s:submit value="%{getText('userlogin.submit')}" />  
  45.                     </td>  
  46.                 </tr>  
  47.             </table>  
  48.         </s:form>  
  49.     </center>  
  50.   </body>  
  51. </html>  


DTO:

当jsp传递参数数量与user的成员变量数量不一致时,struts2无法给user模型参数注入,会出现如下报错信息:

[plain] view plain copy

  1. ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor - Developer Notification (set struts.devMode to false to disable this message):  
  2. 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类代码:

[java] view plain copy

  1. package model;  
  2.   
  3. public class User {  
  4.     private int userId;  
  5.     private String userName;  
  6.     private String userPassword;  
  7.     public int getUserId() {  
  8.         return userId;  
  9.     }  
  10.     public void setUserId(int userId) {  
  11.         this.userId = userId;  
  12.     }  
  13.     public String getUserName() {  
  14.         return userName;  
  15.     }  
  16.     public void setUserName(String userName) {  
  17.         this.userName = userName;  
  18.     }  
  19.     public String getUserPassword() {  
  20.         return userPassword;  
  21.     }  
  22.     public void setUserPassword(String userPassword) {  
  23.         this.userPassword = userPassword;  
  24.     }  
  25. }  


UserDTO类代码:

[java] view plain copy

  1. package dto;  
  2.   
  3. public class UserDto {  
  4.     private String userName;  
  5.     private String userPassword;  
  6.     public String getUserName() {  
  7.         return userName;  
  8.     }  
  9.     public void setUserName(String userName) {  
  10.         this.userName = userName;  
  11.     }  
  12.     public String getUserPassword() {  
  13.         return userPassword;  
  14.     }  
  15.     public void setUserPassword(String userPassword) {  
  16.         this.userPassword = userPassword;  
  17.     }  
  18. }  


LoginAction代码:

[java] view plain copy

  1. package action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. import dto.UserDto;  
  6. import model.User;  
  7.   
  8. public class LoginAction extends ActionSupport{  
  9.     private User user= new User();  
  10.     private UserDto udto;  
  11.       
  12.     public UserDto getUdto() {  
  13.         return udto;  
  14.     }  
  15.   
  16.     public void setUdto(UserDto udto) {  
  17.         this.udto = udto;  
  18.     }  
  19.   
  20.     public String execute(){  
  21.         user.setUserName(udto.getUserName());  
  22.         user.setUserPassword(udto.getUserPassword());  
  23.         System.out.println(user.getUserName());  
  24.         System.out.println(user.getUserPassword());  
  25.         return this.SUCCESS;  
  26.     }  
  27.   
  28. }  


Login.jsp代码:

[html] view plain copy

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title><s:text name="userlogin.title" /></title>  
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   </head>  
  23.     
  24.   <body>  
  25.     <center>  
  26.         <s:form action="login.action" method="post">  
  27.             <table>  
  28.                 <tr>  
  29.                     <td>  
  30.                     </td>  
  31.                     <td>  
  32.                         <s:textfield name="udto.userName" label="用户名"/>  
  33.                     </td>  
  34.                 </tr>  
  35.                 <tr>  
  36.                     <td>  
  37.                     </td>  
  38.                     <td>  
  39.                         <s:password name="udto.userPassword" label="密码"/>  
  40.                     </td>  
  41.                 </tr>  
  42.                 <tr>  
  43.                     <td colspan="2" align="right">  
  44.                         <s:submit value="%{getText('userlogin.submit')}" />  
  45.                     </td>  
  46.                 </tr>  
  47.             </table>  
  48.         </s:form>  
  49.     </center>  
  50.   </body>  
  51. </html>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一位远方的诗人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值