简体   繁体   English

Spring MVC控制器HTTP GET查询参数

[英]Spring MVC controller HTTP GET query parameters

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". 所以也许我有一个页面上有一个项目列表,每个页面都是“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. 当用户点击链接时,我希望控制器加载“第5项”并将其传递给我的编辑表单。

I'm sorry to ask such a silly question, but for some reason I can't find any example of doing this online. 我很抱歉问这么愚蠢的问题,但由于某种原因,我找不到任何在线这样做的例子。

You should have a Controller that maps to edititem.htm. 您应该有一个映射到edititem.htm的Controller。 (Maybe a SimpleFormController ) (也许是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的不同视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM