wang zhihao
wang zhihao

Reputation: 201

How to use Spring MVC DataBinding to instantiate a SubClass

Consider the following three classes, C is a subclass of B. B is a field of A. How to pass An instance of class A which contains an instance of class C as its field, from JSP pages to Spring Controller method?

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;}
}

The sample Spring controller:

@Controller
public class Handler{
     @RequestMapping("/work")
     public String work(@RequestParam( "objA" ) A objA ){
         if( C.getClass().isInstance( objA.getB() ) ){
               System.out.println("It works.");
          }
     }
}

The supposed JSP page but doesn't work:

<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>

Upvotes: 2

Views: 1028

Answers (1)

wang zhihao
wang zhihao

Reputation: 201

I think we can use Jackson to bind data. Then we can treat it as Jackson Polymorphic Type Handling Problem Here is a link about how to use Jackson and ajax in Spring mvc context.

Upvotes: 1

Related Questions