大佬教程收集整理的这篇文章主要介绍了Spring Web MVC,@ ModelAttribute和@RequestParam一起,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
问题是您只分配了对事物的新引用,而该引用永远不会在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"; }
@H_616_7@所以这样做
@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"; }
@H_616_7@这使我想知道为什么在此方法中甚至还具有模型属性?基本上是没有用的。
代替上面的我会做这样的事情
@modelattribute("thing") public Thing prepareModel(@requestParam("thingID") String thingID) { return thingSergice.findByID(thingID); }
@H_616_7@现在,将在每个请求处理方法之前调用此方法。您可以在其中简单地引用它,而不必每次都查找它。(从代码中判断方法中的Thingbean模型属性非常没用,我将其重写为以下内容)
@requestMapPing( value = "/Thing.HTML", method = requestMethod.GET ) public String editThing() { return "thing/edit"; }
@H_616_7@解决方法
我有一个带有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"; } }
@H_616_7@该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>
@H_616_7@但是,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"; }
@H_616_7@大佬总结
以上是大佬教程为你收集整理的Spring Web MVC,@ ModelAttribute和@RequestParam一起全部内容,希望文章能够帮你解决Spring Web MVC,@ ModelAttribute和@RequestParam一起所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。