2 hkinq HKINQ 于 2016.02.28 00:00 提问

关于Struts2 DomainDriven

为何Struts2一直没有自动实例化域
代码如下:

jsp

 <%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: 健勤
  Date: 2016/2/4
  Time: 15:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加员工</title>
</head>
<body>
    <s:form action="addEmpAction" method="POST">
      <s:textfield name="employee.name" label="员工名"/><br/>
      <s:textfield name="employee.password" label="员工密码"/><br/>
      <s:textfield name="employee.salary" label="工资"/><br/>
      <s:submit value="提交"/>
      <s:reset value="重置"/>
    </s:form>
</body>
</html>

Action

 package org.hrsystem.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.hrsystem.service.IMgrService;
import org.hrsystem.vo.Employee;

/**
 * Created by 健勤 on 2016/2/3.
 */
public class AddEmpAction extends ActionSupport {
    private IMgrService mgrService;
    private Employee employee;

    @Override
    public String execute() throws Exception {
        this.getActionMessages().clear();
        ActionContext actionContext = ActionContext.getContext();

        String mgrName = (String) actionContext.getSession().get("user");

        boolean flag = mgrService.addEmp(employee, mgrName);

        if(flag){
            addActionMessage("添加员工成功!");
        }
        else{
            addActionMessage("添加员工失败!");
        }

        return SUCCESS;
    }

    public IMgrService getMgrService() {
        return mgrService;
    }

    public void setMgrService(IMgrService mgrService) {
        this.mgrService = mgrService;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
}

domain

 package org.hrsystem.vo;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by 健勤 on 2016/2/2.
 */
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name = "employee_inf")
public class Employee {
    @Id
    @Column(name = "emp_id")
    @GenericGenerator(name = "employee_sequence",strategy = "sequence")
    @GeneratedValue(generator = "employee_sequence")
    private Integer id;
    @Column(name = "emp_name",nullable = false,unique = true, length = 30)
    private String name;
    @Column(name = "emp_password",nullable = false,length = 30)
    private String password;
    @Column(name = "emp_salary",nullable = false)
    private double salary;
    @OneToMany(targetEntity = Payment.class,mappedBy = "employee")
    private Set<Payment> payments = new HashSet<>();
    @ManyToOne(targetEntity = Manager.class)
    @JoinColumn(name = "mgr_id")
    private Manager manager;

    public Employee() {
    }

    public Employee(String name, String password, double salary, Set<Payment> payments, Manager manager) {
        this.name = name;
        this.password = password;
        this.salary = salary;
        this.payments = payments;
        this.manager = manager;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Set<Payment> getPayments() {
        return payments;
    }

    public void setPayments(Set<Payment> payments) {
        this.payments = payments;
    }

    public Manager getManager() {
        return manager;
    }

    public void setManager(Manager manager) {
        this.manager = manager;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Employee)) return false;

        Employee employee = (Employee) o;

        if (name != null ? !name.equals(employee.name) : employee.name != null) return false;
        return !(password != null ? !password.equals(employee.password) : employee.password != null);

    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (password != null ? password.hashCode() : 0);
        return result;
    }
}

2个回答

devmiao
devmiao   Ds   Rxr 2016.02.28 06:58
HKINQ
HKINQ   2016.02.28 10:11

已解决,是struts2配置文件没有使用defaultstack的拦截器,所以就没有办法实例化

Csdn user default icon
上传中...
上传图片
插入图片