Spring MVC控制器HTTP GET查询参数 [英] Spring MVC controller HTTP GET query parameters
问题描述
如何在没有注释的情况下创建并连接将根据查询参数执行操作的控制器?
所以也许我有一个包含项目列表的页面,每个页面都是edititem.htm?id = 5之类的链接。当用户点击链接时,我希望控制器加载第5项并将其传递给我的编辑表单。
So maybe I've got a page with a list of items on it, and each one is a link like "edititem.htm?id=5". When the user clicks a link I want the controller to load up "item #5" and pass it to my edit form.
我很遗憾地问这么傻问题,但由于某种原因,我找不到任何在线这样做的例子。
I'm sorry to ask such a silly question, but for some reason I can't find any example of doing this online.
推荐答案
你应该有一个控制器来映射to edititem.htm。 (也许是 SimpleFormController )
You should have a Controller that maps to edititem.htm. (Maybe a SimpleFormController)
覆盖两个showForm方法中的一个,用项目填充模型:
Override one of the two showForm methods to populate the your model with the item:
protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors){
//get the id
int id = Integer.parseInt(request.getParameter("id"));
// get the object
Item item = dao.getItemById(id);
return new ModelAndView(getFormView(), "item", item);
}
此外,请参阅使用Spring的SimpleFormController 的不同视图
Also, see Different views with Spring's SimpleFormController
这篇关于Spring MVC控制器HTTP GET查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!