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
Get data from HttpServletRequest with spring 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 @Controller
handler methods to make attributes available to a JSP.
有两种方法可以从@Controller
处理程序方法使属性对 JSP 可用。
- Make your method accept an
HttpServletRequest
parameter and set the target object as a request attribute directly. - Make your method accept a
Model
,ModelMap
,ModelAndView
, orMap
parameter and set the target object as a request attribute on thatModel
argument. You can also make your method return any of the above.
- 让您的方法接受一个
HttpServletRequest
参数并将目标对象直接设置为请求属性。 - 让你的方法接受
Model
,ModelMap
,ModelAndView
,或Map
参数和设置目标对象对请求属性Model
的参数。您还可以让您的方法返回上述任何一项。
For 2.
, Spring will take the elements you added to the Model
and put them into the HttpServletRequest
attributes. They will then be available while rendering the JSP.
对于2.
,Spring 会将您添加到 的元素Model
放入HttpServletRequest
属性中。然后它们将在呈现 JSP 时可用。
Let's have some examples:
让我们举一些例子:
Return a ModelAndView
with one attribute
返回一个ModelAndView
只有一个属性
public ModelAndView inputRequiredInfo() {
Map map = newRequest.loadAllUserDomainType();
return new ModelAndView("request/selectDomainUser","mylist", map);
}
Return a ModelAndView
with 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 String
view name but add attributes to a Model
passed 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
.
在上面的例子中,你可能已经过去了Model
,ModelMap
或者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 ModelAndView
and 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 控制器传递到另一个控制器?