Java Spring MVC 控制器 HTTP GET 查询参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/787291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:29:08  来源:igfitidea点击:

Spring MVC controller HTTP GET query parameters

javaspring-mvc

提问by Boden

How do I, without annotations, create and wire a controller that will perform an action based on a query parameter?

在没有注释的情况下,我如何创建和连接将基于查询参数执行操作的控制器?

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.

所以也许我有一个页面,上面有一个项目列表,每个页面都是一个链接,如“edititem.htm?id=5”。当用户单击链接时,我希望控制器加载“项目 #5”并将其传递给我的编辑表单。

I'm sorry to ask such a silly question, but for some reason I can't find any example of doing this online.

我很抱歉问这样一个愚蠢的问题,但由于某种原因,我在网上找不到任何这样做的例子。

采纳答案by Theo

You should have a Controller that maps to edititem.htm. (Maybe a SimpleFormController)

您应该有一个映射到 edititem.htm 的控制器。(也许是SimpleFormController

Override one of the two showForm methods to populate the your model with the item:

覆盖两个 showForm 方法之一以使用项目填充您的模型:

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

Also, see Different views with Spring's SimpleFormController

另外,请参阅Spring 的 SimpleFormController 的不同视图