需求如下(我写了主要的东东供大家分析), 用户注册模块. 有用户信息UserProfile类:
public class UserProfile {
private String userName;
private String password;
....
}
Action类:
public class UserRegAction {
private UserProfile userProfile;
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
public String execute() {
// 处理userProfile对象
}
}
JSP页面:
<form method="POST" action="...do" >
用户名: <input name="userProfile.userName" type="text" />
密码: <input name="userProfile.password" type="password" />
</form>
其它配置文件略了, 问题是这样的, 我想通过页面提交后userProfile.userName,userProfile.password请求参数自动与UserRegAction 中 userProfile对象中的映射设值. 不过发现不能将请求参数设置对userProfile属性, 没有报错, 重复调了setUserProfile, 并没有按我想的把页面的值设置进去, 各位达人帮忙看下, 在struts2中能否实现该需求, 如果能实现应该如下写才正确. 先行谢过了.
public class UserProfile {
private String userName;
private String password;
....
}
Action类:
public class UserRegAction {
private UserProfile userProfile;
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
public String execute() {
// 处理userProfile对象
}
}
JSP页面:
<form method="POST" action="...do" >
用户名: <input name="userProfile.userName" type="text" />
密码: <input name="userProfile.password" type="password" />
</form>
其它配置文件略了, 问题是这样的, 我想通过页面提交后userProfile.userName,userProfile.password请求参数自动与UserRegAction 中 userProfile对象中的映射设值. 不过发现不能将请求参数设置对userProfile属性, 没有报错, 重复调了setUserProfile, 并没有按我想的把页面的值设置进去, 各位达人帮忙看下, 在struts2中能否实现该需求, 如果能实现应该如下写才正确. 先行谢过了.
package cn.edu.swu.lab.core.entity;import java.io.Serializable;/**
*
* @author Marco
*/
public class User implements Serializable { // --------------------------------------------------Instance variables private Long id; private String username; private String password; private String realname; private String email; private String phoneNumber; private Department department; private Role role; // -------------------------------------------------Getters and Setters public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getRealname() {
return realname;
} public void setRealname(String realname) {
this.realname = realname;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getPhoneNumber() {
return phoneNumber;
} public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
} public Department getDepartment() {
return department;
} public void setDepartment(Department department) {
this.department = department;
} public Role getRole() {
return role;
} public void setRole(Role role) {
this.role = role;
} // ---------------------------------------------------------Override methods @Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof cn.edu.swu.lab.core.entity.User)) {
return false;
} else {
cn.edu.swu.lab.core.entity.User user = (cn.edu.swu.lab.core.entity.User) obj;
if (null == this.getId() || null == user.getId()) {
return false;
} else {
return true;
}
}
} @Override
public int hashCode() {
if (Integer.MIN_VALUE == this.hashCode) {
if (null == this.getId())
return super.hashCode();
else {
String hashStr = this.getClass().getName() + ":"
+ this.getId().hashCode();
this.hashCode = hashStr.hashCode();
}
}
return this.hashCode;
} private int hashCode = Integer.MIN_VALUE; private static final long serialVersionUID = 8028493581640356208L;}package cn.edu.swu.lab.core.action;import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;import cn.edu.swu.lab.core.entity.Department;
import cn.edu.swu.lab.core.entity.Role;
import cn.edu.swu.lab.core.entity.User;
import cn.edu.swu.lab.core.service.DepartmentService;
import cn.edu.swu.lab.core.service.RoleService;
import cn.edu.swu.lab.core.service.UserService;
import cn.edu.swu.lab.core.util.Constants;@SuppressWarnings("serial")
public class UserAction extends BaseAction { private UserService userService; private DepartmentService departmentService; private RoleService roleService; private User user; private Department department; private Role role; public RoleService getRoleService() {
return roleService;
} public void setRoleService(RoleService roleService) {
this.roleService = roleService;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public Role getRole() {
return role;
} public void setRole(Role role) {
this.role = role;
} public Department getDepartment() {
return department;
} public void setDepartment(Department department) {
this.department = department;
} public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
} public DepartmentService getDepartmentService() {
return departmentService;
} public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
} public String login() throws Exception {
User u = this.userService.login(user.getUsername(), user.getPassword());
HttpServletRequest request = this.getRequest();
String validateCode = request.getParameter("validate");
HttpSession session = this.getSession();
String correctCode = (String) session
.getAttribute(Constants.RANDOM_VALIDATR_CODE);
session.setAttribute(Constants.RANDOM_VALIDATR_CODE, null);
if (u == null) {
System.out.println("用户或密码错误");
} else if (validateCode.equalsIgnoreCase(correctCode) == false) {
System.out.println("验证码错误");
System.out.println(validateCode);
System.out.println(correctCode);
} else {
session.setAttribute(Constants.USER_ENTITY, u);
return SUCCESS;
}
return INPUT;
} public String logout() {
this.getSession().setAttribute(Constants.USER_ENTITY, null);
return SUCCESS;
} public String register() throws Exception {
if (this.department != null) {
this.user.setDepartment(department);
}
if (role != null) {
this.user.setRole(role);
}
if (this.userService.register(user)) {
return SUCCESS;
}
return INPUT;
} public String registerLinks() throws Exception {
List<Department> departments = this.departmentService.findAll();
List<Role> roles = this.roleService.findAll();
if (departments.size() != 0) {
this.getSession().setAttribute(Constants.DEPARTMENT_LIST,
departments);
this.getSession().setAttribute(Constants.ROLE_LIST, roles);
return SUCCESS;
}
return INPUT;
} public String viewInformation() {
User u = this.userService.getUserById(user.getId()); System.out.println("*********************************************");
System.out.println(u.getRealname());
System.out.println(u.getDepartment() == null);
System.out.println(u.getRole().getName());
System.out.println("*********************************************");
if (u != null) {
this.getSession().setAttribute(Constants.USER_INFORMATION, u);
return SUCCESS;
}
return INPUT;
}
}<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body> <center>
<h3 style="color: red;">
您需要先登录才能进行设备管理操作
</h3> <hr> <form action="login" method="post">
<table>
<tr>
<td align="right" width="30%">
用户名
</td>
<td>
<input type="text" name="user.username" />
</td>
</tr>
<tr>
<td align="right" width="30%">
密码
</td>
<td>
<input type="password" name="user.password" />
</td>
</tr>
<tr>
<td align="right" width="30%">
验证码
</td>
<td>
<input type="text" name="validate" id="validate" size="5" />
<img src="validate.jpg" id="validateImg">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登录">
<input type="reset" value="清除">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
PS:抱怨下,linux下打汉字真费劲
你的UserProfile类里面也要有get/set方法就可以
public class UserRegAction {
private UserProfile userProfile;
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
public String execute() {
// 处理userProfile对象
}
public UserProfile getUserProfile() {
return msg;
}
}
如果没有getter方法就不会生效的, 仔细想想也对, 程序拿已有对象setter值拿不到了,当初为了代码干净就没加getter, 哎. 谢谢各位, 结贴给分quxiaoyong, 看了你的代码灵感就来了,哈哈.
public UserProfile getUserProfile() {
return userProfile;
}