Spring Web MVC,@ ModelAttribute和@RequestParam一起


问题内容

我有一个带有GET方法的控制器,如下所示:

@Controller
public class ThingController {

     @RequestMapping( value = "/Thing.html", method = RequestMethod.GET )
    public String editThing(@RequestParam( "thingId" ) String thingId, @ModelAttribute ThingBean thing, BindingResult result) {
        thing = <some service call using thingId>
        logger.debug("The thing to edit is {}", thingBean);
        return "thing/edit";
    }
}

该Bean是正确的(获取器和设置器),服务调用返回带有ThingId的正确ThingBean,并且显示了我在Thing / edit.jsp上的JSP页面。

JSP是:

<html>
<body>
    <h1 id="title" class="title">Edit Thing</h1>

<form:form id="thing" modelAttribute="thing">
    <form:input path="subject" id="subject" tabindex="1" />
    <form:textarea path="message" />

</form:form>

</body>
</html>

但是,JSP显示主题和消息的空白值。是的,这些属性上有吸气剂/吸气剂。

我有一个非常相似的Controller,它工作正常,只是GET映射方法的签名中没有@RequestParam。

我在Spring WebMVC文档中没有看到任何地方说我无法做到这一点-
为什么它不起作用?为什么不将更新后的ModelAttribute对象绑定到JSP表单中?

编辑:

该控制器以及用于GET调用的相同模式在许多不同的地方起作用-用@ModelAttribute注释的变量由方法填充,然后可用于JSP页面显示。为什么通过添加@RequestParam注释使其停止?

@RequestMapping( value = "/Things.html", method = RequestMethod.GET )
public String getThings(@ModelAttribute ThingForm thingForm, BindingResult result) {

    try {
        // need to get list of Things
        List<Thing> Things = <service call that gets list of Things>;
        thingForm.setThings(Things);
        logger.debug("Things are {}", Things);

    }
    catch (ResourceNotFoundException e) {
        return "error";
    }

    logger.debug("Thing list loading - end");

    // Go to jsp
    return "thing/list";
}

问题答案:

问题是您只分配了对事物的新引用,而该引用永远不会在Java中起作用。您必须将其放入模型中,否则您的视图将无法得知。

@RequestMapping( value = "/Thing.html", method = RequestMethod.GET )
public String editThing(@RequestParam( "thingId" ) String thingId, @ModelAttribute ThingBean thing, BindingResult result) {
    thing = <some service call using thingId> // This is only assigning a new reference not updating 
    logger.debug("The thing to edit is {}", thingBean);
    return "thing/edit";
}

所以这样做

@RequestMapping( value = "/Thing.html", method = RequestMethod.GET )
public String editThing(@RequestParam( "thingId" ) String thingId, @ModelAttribute ThingBean thing, BindingResult result, Model model) {
    thing = <some service call using thingId>
    model.addAttribute("thing", thing);
    logger.debug("The thing to edit is {}", thingBean);
    return "thing/edit";
}

这使我想知道为什么在此方法中甚至还具有模型属性?基本上是没有用的。

代替上面的我会做这样的事情

@ModelAttribute("thing")
public Thing prepareModel(@RequestParam("thingId") String thingId) {
    return thingSergice.findById(thingId);
}

现在,将在每个请求处理方法之前调用此方法。您可以在其中简单地引用它,而不必每次都查找它。(从代码中判断方法中的Thingbean模型属性非常没用,我将其重写为以下内容)

@RequestMapping( value = "/Thing.html", method = RequestMethod.GET )
public String editThing() {
    return "thing/edit";
}