gpt4 book ai didi

java - Spring MVC 中的 ThrowawayController 如何从请求中获取参数?

转载 作者:行者123 更新时间:2023-11-30 05:15:51 27 4
gpt4 key购买 nike

这是在 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。

谢谢。

最佳答案

我实际上必须搜索这个类,所以如果您仍在使用它,请注意它在 Spring 3 中甚至不存在。

来自ThrowawayControllerHandlerAdapter Javadoc

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

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

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

关于java - Spring MVC 中的 ThrowawayController 如何从请求中获取参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1366177/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com