Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringMVC中,我可以有一个带支持对象的有状态下拉列表吗?_Java_Spring Mvc - Fatal编程技术网

Java 在SpringMVC中,我可以有一个带支持对象的有状态下拉列表吗?

Java 在SpringMVC中,我可以有一个带支持对象的有状态下拉列表吗?,java,spring-mvc,Java,Spring Mvc,在SpringMVC中,我希望有一个带有html下拉列表的表单,该表单由域对象列表支持,但只显示对象中的一个字段。提交表单时,我希望能够检索整个对象。我可以这样做吗?如果我正确理解了你的意思,这显然是可能的 模型 控制器 这是在使用注释。如果您不明白这是在做什么,您可能应该查看Spring文档。@ModelAttribute(“fooResults”)将可供您的视图用于下拉元素。@modeldattribute(“命令”)Foo-Foo将自动“吸收”您在下拉列表中选择的任何内容 @Control

在SpringMVC中,我希望有一个带有html下拉列表的表单,该表单由域对象列表支持,但只显示对象中的一个字段。提交表单时,我希望能够检索整个对象。我可以这样做吗?

如果我正确理解了你的意思,这显然是可能的

模型

控制器

这是在使用注释。如果您不明白这是在做什么,您可能应该查看Spring文档。
@ModelAttribute(“fooResults”)
将可供您的视图用于下拉元素。
@modeldattribute(“命令”)Foo-Foo
将自动“吸收”您在下拉列表中选择的任何内容

@Controller
public class FooController() {

    @ModelAttribute("fooResults")
    public List<String> fooResults() {
        // return a list of string
    }

    @RequestMapping(method = RequestMethod.GET)
    public String get(@ModelAttribute("command") Foo foo) {
        return "fooView";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String post(@ModelAttribute("command") Foo foo) {
        // do something with foo
    }

这一切都假设你知道自己在做什么:)如果你不知道,请查看这篇文章准确地解释了你想做什么:。我花了很长时间寻找完全相同的问题,克里德拉岛上的AJ Angus给出了我在网上看到的最好的解释

总之,您必须告诉Spring如何将select标记上的字符串形式选项值转换回对象。这是通过将项值作为对象的ID来实现的: spring现在有了员工的ID,但是当用户单击submit时spring如何将ID更改回员工?这是通过PropertyEditor实现的,Spring文档没有很好地解释:

public class EmployeeNamePropertyEditor extends PropertyEditorSupport {
EmployeeDAO employeeDAO;

public EmployeeNamePropertyEditor(EmployeeDAO employeeDAO)   {
    this.employeeDAO = employeeDAO;
}

public void setAsText(String text)   {
    Employee employee = new Employee();
    employee = employeeDAO.getEmployee(Long.parseLong(text));
    setValue(employee);
}
}
然后使用initBinder让控制器知道propertyEditor存在:

@InitBinder
public void initBinder(WebDataBinder binder)    {
    binder.registerCustomEditor(Employee.class, new        
    EmployeeNamePropertyEditor(employeeDAO));
}

那你们都准备好了!请查看链接以获得更好更详细的解释。

至少在2.5版本中,这不起作用。我们需要定制属性编辑器,如下所述
public class EmployeeNamePropertyEditor extends PropertyEditorSupport {
EmployeeDAO employeeDAO;

public EmployeeNamePropertyEditor(EmployeeDAO employeeDAO)   {
    this.employeeDAO = employeeDAO;
}

public void setAsText(String text)   {
    Employee employee = new Employee();
    employee = employeeDAO.getEmployee(Long.parseLong(text));
    setValue(employee);
}
}
@InitBinder
public void initBinder(WebDataBinder binder)    {
    binder.registerCustomEditor(Employee.class, new        
    EmployeeNamePropertyEditor(employeeDAO));
}