Java 如何使用Spring@MVC在一个请求中填充多个bean

Java 如何使用Spring@MVC在一个请求中填充多个bean,java,spring,spring-mvc,model-binding,Java,Spring,Spring Mvc,Model Binding,我正在使用Spring@MVC(带有MVC注释)开发一个项目 如果所有请求参数都应该填充到一个bean中,那么一切看起来都很好,但是多个POJO呢 我在网上搜索过并且知道表单支持对象,但是如何在@MVC(基于注释)中使用它们呢 另一个问题:我应该为每个表单构造一个bean吗?它看起来不是很像Struber的ActionForms吗?是否存在阻止创建这些对象的方法 有没有一种方法可以将所有bean放在地图中,并让SpringBinder填充它们?比如: map.put("department",

我正在使用Spring@MVC(带有MVC注释)开发一个项目

如果所有请求参数都应该填充到一个bean中,那么一切看起来都很好,但是多个POJO呢

我在网上搜索过并且知道表单支持对象,但是如何在@MVC(基于注释)中使用它们呢

另一个问题:我应该为每个表单构造一个bean吗?它看起来不是很像Struber的
ActionForm
s吗?是否存在阻止创建这些对象的方法

有没有一种方法可以将所有bean放在地图中,并让SpringBinder填充它们?比如:

map.put("department", new Department());
map.put("person", new Person());

所以
department.name
department.id
绑定到department bean中,
person.name
person.sex
和。。。在person bean中填充?(因此控制器方法接受
映射作为其参数)。

表单支持对象不是必需的,您可以使用
@RequestParam
注释直接获取表单值。看


我认为默认的SpringMVC类型转换器不支持Map,但是您可以注册一个自定义转换器。请参阅。

如果您向
人员
提供
部门的参考资料,那么就很容易了。在你的应用程序中,如果此人在某个部门工作,那么在你的person类中创建
Has-a
关系是合乎逻辑的,如下所示:

@Component
@Scope("prototype")
public class Person {
    private String firstName;

    private Department department;

    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>    
<!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=ISO-8859-1">
<title>Test</title>
</head>
<body>
    <sf:form commandName="person" action="/appname/handlePost.html" method="post">
        <sf:input path="firstName"/>
        <sf:input path="department.departmentName"/>
        <sf:button name="Submit">Submit</sf:button>
    </sf:form>
</body>
</html>
您可以创建一个控制器,从上下文中获取
Person
bean并呈现视图

@Controller
public class TestController implements ApplicationContextAware{

    private ApplicationContext appContext;

    @RequestMapping(value="/handleGet",method=RequestMethod.GET)
    public String handleGet(ModelMap map){
        map.addAttribute("person", appContext.getBean("person"));
        return "test";
    }
    @RequestMapping(value="/handlePost",method=RequestMethod.POST)
    public @ResponseBody String handlePost(@ModelAttribute("person") Person person){
        return person.getDepartment().getDepartmentName();
    }

    @Override
    public void setApplicationContext(ApplicationContext appContext)
            throws BeansException {
        this.appContext=appContext;
    }
}
然后在JSP视图中,您可以编写如下内容:

@Component
@Scope("prototype")
public class Person {
    private String firstName;

    private Department department;

    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>    
<!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=ISO-8859-1">
<title>Test</title>
</head>
<body>
    <sf:form commandName="person" action="/appname/handlePost.html" method="post">
        <sf:input path="firstName"/>
        <sf:input path="department.departmentName"/>
        <sf:button name="Submit">Submit</sf:button>
    </sf:form>
</body>
</html>

试验
提交

这一点在开箱即用的情况下并没有得到很好的支持,不。这些字符串是否真的在运行时需要使用映射,而不能仅仅创建一个bean?那么,我应该为每个请求类型创建一个bean吗?是的,当一个完整的表单映射到一个对象时,封装的活页夹堆栈是最令人高兴的。我不清楚用什么方式为每个请求定制地图会客观上更好:)使用
@RequestParam
字符串不是要追溯到石器时代吗?