Java 使用 spring mvc 从 HttpServletRequest 获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18690916/
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-12 10:13:38  来源:igfitidea点击:

Get data from HttpServletRequest with spring mvc

javaservletsspring-mvc

提问by H.dev.guy

I am new to Spring MVC.

我是 Spring MVC 的新手。

What I want to achieve is to add a map of data into HttpServletRequest; Example:

我想要实现的是在HttpServletRequest中添加一张数据图;例子:

private NewRequestService newRequest = new NewRequestService();

public ModelAndView inputRequiredInfo(@ModelAttribute("requestForm") HttpServletRequest request) {
  request.setAttribute("mylist", newRequest.loadAllUserDomainType());

  return new ModelAndView("request/selectDomainUser","requestForm", request);

}

And then in the view jsp file, I want to get those data which pass into the request and put it into dropdown list.

然后在视图jsp文件中,我想获取传递到请求中的那些数据并将其放入下拉列表中。

采纳答案by Sotirios Delimanolis

Forget what you've done right now.

忘记你现在所做的。

There are two ways from a @Controllerhandler methods to make attributes available to a JSP.

有两种方法可以从@Controller处理程序方法使属性对 JSP 可用。

  1. Make your method accept an HttpServletRequestparameter and set the target object as a request attribute directly.
  2. Make your method accept a Model, ModelMap, ModelAndView, or Mapparameter and set the target object as a request attribute on that Modelargument. You can also make your method return any of the above.
  1. 让您的方法接受一个HttpServletRequest参数并将目标对象直接设置为请求属性。
  2. 让你的方法接受ModelModelMapModelAndView,或Map参数和设置目标对象对请求属性Model的参数。您还可以让您的方法返回上述任何一项。

For 2., Spring will take the elements you added to the Modeland put them into the HttpServletRequestattributes. They will then be available while rendering the JSP.

对于2.,Spring 会将您添加到 的元素Model放入HttpServletRequest属性中。然后它们将在呈现 JSP 时可用。

Let's have some examples:

让我们举一些例子:

Return a ModelAndViewwith one attribute

返回一个ModelAndView只有一个属性

public ModelAndView inputRequiredInfo() {
    Map map = newRequest.loadAllUserDomainType();

    return new ModelAndView("request/selectDomainUser","mylist", map);
}

Return a ModelAndViewwith no attributes, but add the attribute directly to HttpServletRequest

返回一个ModelAndView没有属性的,而是直接将属性添加到HttpServletRequest

public ModelAndView inputRequiredInfo(HttpServletRequest request) {
    Map map = newRequest.loadAllUserDomainType();
    request.setAttribute("mylist", map);
    return new ModelAndView("request/selectDomainUser");
}

Return a Stringview name but add attributes to a Modelpassed as argument

返回String视图名称但将属性添加到Model作为参数传递

public String inputRequiredInfo(Model model) {
    Map map = newRequest.loadAllUserDomainType();
    model.addAttribute("mylist", map);
    return "request/selectDomainUser";
}

In the above example, you could have passed Model, ModelMap, or java.util.Map.

在上面的例子中,你可能已经过去了ModelModelMap或者java.util.Map

Same but with HttpServletRequest

相同但与 HttpServletRequest

public String inputRequiredInfo(HttpServletRequest request) {
    Map map = newRequest.loadAllUserDomainType();
    request.setAttribute("mylist", map);
    return "request/selectDomainUser";
}

For a more complete list of accepted method arguments, see section 17.3.3 of the official documentation.While you are at it, also read the supported return types to understand the difference between returning a ModelAndViewand returning a String.

有关可接受的方法参数的更完整列表,请参阅官方文档的第 17.3.3 节。在此过程中,还请阅读支持的返回类型以了解返回 aModelAndView和返回 a之间的区别String

回答by dharam

This is not the correct way though.

但这不是正确的方法。

You can use something like a model attribute.

您可以使用诸如模型属性之类的东西。

Check the links below for a lot of answers on this:

检查下面的链接以获得很多关于此的答案:

How to pass model attributes from one Spring MVC controller to another controller?

如何将模型属性从一个 Spring MVC 控制器传递到另一个控制器?

Passing parameters from JSP to Controller in Spring MVC

在 Spring MVC 中将参数从 JSP 传递到控制器