【S2SH】怪问题!!大神指点一下。。
本帖最后由 WingBin 于 2012-12-15 22:19:33 编辑 S2SH配置没有问题,出现Action里面无法获取页面的值(不能自动装配),具体如下
实体类Empoyee代码:
package cn.jbit.jboa.entity;
/**
* 员工 实体类。
* @author
* @version
*/
public class Employee implements java.io.Serializable {
// Fields
private static final long serialVersionUID = 5106663630382037556L;
private String sn;
private Position position;
private Department department;
private String password;
private String name;
private String status;
// Constructors
/** default constructor */
public Employee() {
}
public Employee(String sn, Position position, Department department,
String password, String name, String status) {
super();
this.sn = sn;
this.position = position;
this.department = department;
this.password = password;
this.name = name;
this.status = status;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
login.jsp代码:
<s:form action="login">
<s:textfield name="employee.name" label="用户名" />
<s:textfield name="employee.sn" label="编码" value="12345"/>
<s:password name="employee.password" label="密码" />
<input type="hidden" name="employee.status" value="...stat..."/>
<s:submit value="登录"></s:submit>
</s:form>
UserAction类
package cn.jbit.jboa.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import cn.jbit.jboa.constants.Constants;
import cn.jbit.jboa.entity.ClaimVoucher;
import cn.jbit.jboa.entity.Department;
import cn.jbit.jboa.entity.Employee;
import cn.jbit.jboa.entity.Position;
import cn.jbit.jboa.service.EmployeeService;
import cn.jbit.jboa.utils.MD5;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户登录action
* @author
* @version 1.0
*/
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private final Log logger = LogFactory.getLog(getClass());
private Employee employee;
private EmployeeService employeeService;
/**
* 用户登录。
* @return
* @throws Exception
*/
public String login() throws Exception {
Employee newEmployee = null;
HttpServletRequest req = ServletActionContext.getRequest();
//直接获取
System.out.println("===name="+req.getParameter("employee.name"));
System.out.println("===password="+req.getParameter("employee.password"));
System.out.println("===status="+req.getParameter("employee.status"));
System.out.println("===sn="+req.getParameter("employee.sn"));
//测试组装后Bean的属性值
System.out.println("name="+employee.getName());
System.out.println("password="+employee.getName());
System.out.println("status="+employee.getStatus());
System.out.println("sn="+employee.getSn());
System.out.println("password="+employee.getName());
try {
employee.setPassword(new MD5(employee.getPassword()).compute());
newEmployee = employeeService.login(employee);
if (logger.isDebugEnabled()) {
logger.debug("---------------" + newEmployee.getName());
}
} catch (Exception e) {
logger.error(e.getMessage());
}
String ret = INPUT;
if (newEmployee == null) {
ret = INPUT;
} else {
ActionContext ac = ActionContext.getContext();
ac.getSession().put("employee", newEmployee);
//获取职员职务信息,并获得职务名称
String nameCn = newEmployee.getPosition().getNameCn();
if ("员工".equals(nameCn)) {
ret = "staff";
ClaimVoucher claimVoucher = new ClaimVoucher();
claimVoucher.setStatus(Constants.CLAIMVOUCHER_CREATED);
claimVoucher.setId(0);
ac.getSession().put("claimVoucher", claimVoucher);
} else if ("部门经理".equals(nameCn)) {
ret = "deptManager";
} else if ("总经理".equals(nameCn)) {
ret = "manager";
} else if ("财务".equals(nameCn)) {
ret = "cashier";
}
}
return ret;
}
/**
* 用户退出。
* @return
* @throws Exception
*/
public String logout() throws Exception {
ActionContext ac = ActionContext.getContext();
ac.getSession().remove("employee");
return SUCCESS;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
}
测试的输出如下:
===name=admin
===password=admin
===status=...stat...
===sn=12345
name=null
password=null
status=...stat...
sn=null
password=null
注意红色的只有status可以获取到值
大神请指点....
[解决办法]
我看你只设置了setter方法,getter也加进去试试。
[解决办法]
很明显是对象属性private Employee employee;没有生成get方法,,你只有set ,其实是值已经装配好了,不信你可以debug 看看 。struts2自动装配就是反射,反射赋值的时候只需要set方法即可 。