考虑以下三个类,C是B的子类。B是A的字段。如何将包含C类实例的A类实例作为其字段,从JSP页面传递给Spring Controller方法?
class A{
private B b;
public B getB(){return b;}
public void setB( B b){ this.b = b;}
}
class B{
private int id;
public int getId(){return id;}
public void setId(int id){this.id = id;}
}
class C extends B{
private name;
public String getName(){return name;}
public void setName(String name){this.name = name;}
}
示例 Spring 控制器:
@Controller
public class Handler{
@RequestMapping("/work")
public String work(@RequestParam( "objA" ) A objA ){
if( C.getClass().isInstance( objA.getB() ) ){
System.out.println("It works.");
}
}
}
假定的 JSP 页面但不起作用:
<form method="post" action="work" commandName="objA" >
<input type="submit" value="submit" /><br/>
<input type="hidden" name="b.name" value="name" /><br/>
<input type="hidden" name="b.id" value="1" /><br/>
</form>