views:

352

answers:

1

This is an example of using ThrowawayController in Spring MVC:

public class DisplayCourseController 
    implements ThrowawayController {
  private Integer id;
  public void setId(Integer id) { this.id = id; }   
  public ModelAndView execute() throws Exception {
    Course course = courseService.getCourse(id);                       
    return new ModelAndView("courseDetail", "course", course);   
  }
  private CourseService courseService;
  public void setCourseService(CourseService courseService) {
    this.courseService = courseService;
  }
}

With its declaration in the xml file:

<bean id="displayCourseController"
    class="com.spring.mvc.DisplayCourseController"
    singleton="false">
  <property name="courseService">
    <ref bean="courseService"/>
  </property>
</bean>

Both of these two pieces of code do not specify how the parameter id in a request is found identified. Can anybody tell me how it works?

EDITED: I think the logic would probably be using getParameterNames() and getParameter() to retrieve all the parameters, and using java reflection to get the corresponding setter.

Thanks.

+1  A: 

I actually had to search for this class, so if you're still using it, be aware that it's not even present in Spring 3.

From ThrowawayControllerHandlerAdapter Javadoc

This implementation binds request parameters to the ThrowawayController instance and then calls execute on it.

So, I'd say that the handler adapter uses standard property editors to convert named request parameters into the type specified in the setX methods.

You might be able to confirm this by passing, in your example code above, an invalid number as id in the request, eg: foo?id=not-a-number and see where the exception gets thrown.

ptomli
Thanks. Seems that we have the same speculation. I will give the experiment a try, and see what happens.
Winston Chen