java - 如何在 spring 3.2 mvc 中接收复杂对象?

标签 java spring-mvc data-binding

spring 3.2 mvc如何接收复杂对象?

在下面的简单示例中,我有两个模型类,具有多对一关系。添加新的 Employee 对象时,我想使用 html 选择来选择它的部门。

当我发帖添加新员工时,出现以下错误:

Failed to convert property value of type java.lang.String to required type hu.pikk.model.Department for property department; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [hu.pikk.model.Department] for property department: no matching editors or conversion strategy found

我应该如何实现编辑器或转换策略?是否存在应注意的最佳实践或陷阱?

我已经阅读了 spring mvc 文档、一些文章和 stackoverflow 问题,但老实说,我发现它们有点令人困惑,而且很多时候太短、太草率了。

模型:

@Entity
public class Employee {
    @Id
    @GeneratedValue
    private int employeeId;
    @NotEmpty
    private String name;

    @ManyToOne
    @JoinColumn(name="department_id")
    private Department department;
    //getters,setters
}

@Entity
public class Department {
    @Id
    @GeneratedValue
    private int departmentId;
    @Column
    private String departmentName;

    @OneToMany
    @JoinColumn(name = "department_id")
    private List<Employee> employees;
    //getters,setters
}

在我的 Controller 类中:

@RequestMapping(value = "/add", method = RequestMethod.GET)
private String addNew(ModelMap model) {
    Employee newEmployee = new Employee();
    model.addAttribute("employee", newEmployee);
    model.addAttribute("departments", departmentDao.getAllDepartments());
    return "employee/add";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
private String addNewHandle(@Valid Employee employee, BindingResult bindingResult, ModelMap model, RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("departments", departmentDao.getAllDepartments());
        return "employee/add";
    }
    employeeDao.persist(employee);
    redirectAttributes.addFlashAttribute("added_employee", employee.getName());
    redirectAttributes.addFlashAttribute("message", "employee added...");
    return "redirect:list";     
}

在 add.jsp 中:

<f:form commandName="employee" action="${pageContext.request.contextPath}/employee/add" method="POST">
    <table>
        <tr>
            <td><f:label path="name">Name:</f:label></td>
            <td><f:input path="name" /></td>
            <td><f:errors path="name" class="error" /></td>
        </tr>
        <tr>
            <td><f:label path="department">Department:</f:label></td>
        <td><f:select path="department">
                <f:option value="${null}" label="NO DEPARTMENT" />
                <f:options items="${departments}" itemLabel="departmentName" itemValue="departmentId" />
            </f:select></td>
            <td><f:errors path="department" class="error" /></td>
        </tr>
    </table>
    <input type="submit" value="Add Employee">
</f:form>

最佳答案

问题是,当 Controller 收到 POST 时,它不知道如何将 id 字符串转换为 Department 对象。 我发现基本上有三种方法可以解决这个问题:

  1. 不使用 spring form jSTL,而是使用自定义名称的简单 html 选择,并在 Controller 中使用 @RequestParam 读取它,访问数据库并填充它。
  2. 实现 Converter 接口(interface),并将其注册为 bean。
  3. 实现 PropertyEditor 接口(interface)。通过扩展 PropertyEditorSupport 类可以更轻松地做到这一点。

我选择了第三个选项。 (稍后当我有时间时,我将使用探索的前两个选项编辑此答案。)

2。实现 Converter 接口(interface)

@Component 
public class DepartmentConverter implements Converter<String,Department>{
    @Autowired
    private DepartmentDao departmentDao;
    @Override
    public Department convert(String id){
        Department department = null;
        try {
            Integer id = Integer.parseInt(text);
            department = departmentDao.getById(id);
            System.out.println("Department name:" + department.getDepartmentName());
        } catch (NumberFormatException ex) {
            System.out.println("Department will be null");
        }
        return department;
    }
}

在spring beans配置文件中:

<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService"
  class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="package.DepartmentConverter"/>
        </list>
    </property>
</bean>

3。扩展 PropertyEditorSupport 类

public class SimpleDepartmentEditor extends PropertyEditorSupport {

    private DepartmentDao departmentDao;

    public SimpleDepartmentEditor(DepartmentDao departmentDao){
        this.departmentDao = departmentDao;
    }
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        Department department = null;
        try {
            Integer id = Integer.parseInt(text);
            department = departmentDao.getById(id);
            System.out.println("Department name:" + department.getDepartmentName());
        } catch (NumberFormatException ex) {
            System.out.println("Department will be null");
        }
        setValue(department);
    }
}

在 Controller 内部,我需要添加一个@InitBinder:

    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Department.class, new SimpleDepartmentEditor(departmentDao));
    }

关于java - 如何在 spring 3.2 mvc 中接收复杂对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19957760/

相关文章:

java - 如何限制ListView中的复选框被选中?

java - Spring MVC 是否在启动时初始化所有 Controller ?

spring - 无法解析为赋值表达式 : "${attrs ?: defaultAttrs}"

android - 通过绑定(bind)设置可见性 - dimens.xml

Java Swing : JInternalFrame: need a dialog popup

java - CXF代码优先服务,WSDL生成; SOAP :address changes?

java - Spring MVC JPA 惰性异常

javascript - 如何在 AngularJS 自定义指令中指定模型?

c# - 复选框绑定(bind) - 什么是更好的解决方案?

java - 是否可以在不附加任何监听器的情况下读取 Firebase 数据?