ssm*_*ssm 6 java spring jsp spring-mvc spring-form
我有
Class Shape {
//Implementation
}
Class Round extends Shape{
//Implementation
}
Run Code Online (Sandbox Code Playgroud)
控制器我有
@Requestmapping(value="/view/form")
public ModelAndView getForm(){
ModelAndView mav=new ModelAndView();
mav.addObject("shape",new Round());
}
@RequestMapping(value="/submit", method = RequestMethod.POST)
public ModelAndView submitForm(@ModelAttribute("shape") Shape shape){
if(shape instanceof Round){ //**Not giving positive result**
}
}
Run Code Online (Sandbox Code Playgroud)
在Jsp
<form:form action="/submit" commandName="shape" method="post">
//form tags
</form:form>
Run Code Online (Sandbox Code Playgroud)
当我用Round对象提交表单时.在控制器端,ModelAttribute没有给出Round的实例.它只给出形状的例子.这该怎么做
你不可以做这个。因为这是两个不同的请求生命周期。
@Requestmapping(value="/view/form")
public ModelAndView getForm(){
ModelAndView mav=new ModelAndView();
mav.addObject("shape",new Round());
}
Run Code Online (Sandbox Code Playgroud)
当上面的请求执行时,即使你Round在 中添加了对象mav,它也会被转换为 html 响应并发回客户端。
因此,当您提交表单时接下来出现以下请求时,这是一个完全独立的请求,并且 spring 无法识别为先前请求添加了哪种对象类型。
@RequestMapping(value="/submit", method = RequestMethod.POST)
public ModelAndView submitForm(@ModelAttribute("shape") Shape shape){
if(shape instanceof Round){ //**Not giving positive result**
}
}
Run Code Online (Sandbox Code Playgroud)
但是您可以尝试探索@SessionAttributes,使用它您可以在不同的请求中维护相同的对象