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
- 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代码:
[java] view plain copy
- 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代码:
[html] view plain copy
- <%@ 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模型参数注入,会出现如下报错信息:
[plain] view plain copy
- 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类代码:
[java] view plain copy
- 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类代码:
[java] view plain copy
- 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代码:
[java] view plain copy
- 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代码:
[html] view plain copy
- <%@ 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>