【发布时间】:2016-10-02 02:31:18
【问题描述】:
我有
Class Shape {
//Implementation
}
Class Round extends Shape{
//Implementation
}
控制器 我有
@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**
}
}
在 Jsp 中
<form:form action="/submit" commandName="shape" method="post">
//form tags
</form:form>
当我提交带有 Round 对象的表单时。在控制器端 ModelAttribute 没有给出 Round 的实例。它仅给出形状的实例。如何做到这一点
【问题讨论】:
-
无论你做什么,静态类型都是Shape。这与 Spring 无关;这是静态/动态类型的问题。您可以强制转换或创建工厂/虚拟构造函数。
-
为什么不能从表单提交
Round -
@Priyamal 每个形状都有更多的形状和相同的形状。
标签: java spring jsp spring-mvc spring-form