2009-09-02 42 views
1

这是在Spring MVC使用ThrowawayController的例子:Spring MVC中的ThrowawayController如何从请求中获取参数?

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

凭借其在XML文件中的声明:

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

这两个两段代码的不指定如何参数ID找到请求中的。任何人都可以告诉我它是如何工作的吗?

编辑: 我觉得逻辑可能会被使用getParameterNames()的getParameter()检索所有参数,并使用Java反射来获取相应的setter。

谢谢。

+1

如果你使用Spring 2.5,那么你不应该使用ThrowawayController,它将在Spring 3中被删除。 – skaffman

+0

你们都提到它。我会牢记这一点。谢谢。 –

回答

1

其实我不得不寻找这个类,所以,如果你还在使用它,要知道,它甚至没有出现在春季3

ThrowawayControllerHandlerAdapter的Javadoc

此实现绑定请求 参数给ThrowawayController 实例,然后对其执行调用。

所以,我会说处理程序适配器使用标准属性编辑器将命名的请求参数转换为setX方法中指定的类型。

您可以通过在上面的示例代码中传递一个无效的数字作为请求中的id来确认此问题,例如:foo?id = not-a-number,并查看抛出异常的位置。

+0

谢谢。似乎我们有同样的猜测。我会试一试,看看会发生什么。 –

1

也许界面“org.springframework.web.servlet.mvc.throwaway.ThrowawayController”是因为Spring3.1.x

你为什么不使用注释去掉?

相关问题