这是我的代码:
public ModelAndView login(@ModelAttribute("testVO") TestVO testVO){
//test the VO work theory
//testVO = new TestVO();
testVO.setTestStr("this is my test!");
return "index/index";
}
Run Code Online (Sandbox Code Playgroud)
当我使用new为testVO创建一个对象时.我无法在jsp页面中获取该值.如果我使用set方法,它的工作原理.
所以,我认为:对象testVo是由IOC容器创建的,因此JSP从容器中获取引用,而不是我自己创建的引用.
我对吗?提前致谢.
你猜对了.下面的文字来自spring docs:
An @ModelAttribute on a method argument indicates the argument should be
retrieved from the model. If not present in the model, the argument should be
instantiated first and then added to the model.
Run Code Online (Sandbox Code Playgroud)
如果你想自己创建它,你明确需要将它添加到模型中(如下所示),以便可以在你的jsp中使用
public String login(Model model){
TestVO testVO = new TestVO();
testVO.setTestStr("this is my test!");
model.addAttribute("testVO", testVO);
return "index/index";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3599 次 |
| 最近记录: |