Spring mvc 如何从先前方法中包含的方法中获取模型值
我正在使用SpringMVC 我有办法Spring mvc 如何从先前方法中包含的方法中获取模型值,spring-mvc,servlets,requestdispatcher,Spring Mvc,Servlets,Requestdispatcher,我正在使用SpringMVC 我有办法 @RequestMapping(value = "/firstMethod/getDetails", method = RequestMethod.GET) @ResponseBody public Map<String, Object> getFirstMethodDetails(HttpServletRequest request, HttpServletResponse response) { Map<String
@RequestMapping(value = "/firstMethod/getDetails", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getFirstMethodDetails(HttpServletRequest request,
HttpServletResponse response) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("name", "Bryan");
request.getRequestDispatcher("/secondmethod/getDetails")
.include(request, response);
// want to access second methods model values here
return model;
}
@RequestMapping(value=“/firstMethod/getDetails”,method=RequestMethod.GET)
@应答器
公共映射getFirstMethodDetails(HttpServletRequest请求,
HttpServletResponse(响应){
映射模型=新的HashMap();
模特儿。把(“名字”、“布莱恩”);
request.getRequestDispatcher(“/secondmethod/getDetails”)
.包括(请求、响应);
//要在此处访问第二个方法模型值吗
收益模型;
}
我从中包括对另一个方法的请求
@RequestMapping(value = "/secondMethod/getDetails", method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getSecondMethodDetails(HttpServletRequest request,
HttpServletResponse response) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("age", 29);
return model;
}
@RequestMapping(value=“/secondMethod/getDetails”,method=RequestMethod.GET)
@应答器
公共映射getSecondMethodDetails(HttpServletRequest请求,
HttpServletResponse(响应){
映射模型=新的HashMap();
示范.put(“年龄”,29岁);
收益模型;
}
现在,如何从第一种方法访问第二种方法的模型???要在两个控制器之间以re direct方式传递数据,您可以在flash scope中使用自定义属性 在这里,您可以在flash scope中添加第一个方法中的模型,并在第二个方法中访问该模型
所以post-在转发时,我可以通过使用查询参数将数据从第一个方法传递到第二个方法,这不是问题。需求是如何获取第二种方法的模型属性,即转发的方法。从第一种方法开始,将模型添加为自定义闪存范围属性是此类场景中推荐的方法。这样,您就不需要为此项添加自定义查询参数。首先,我不能使用重定向,因为我有一些代码需要在调用第二个方法后执行。这不可能使用重定向,因为重定向字符串将是该方法的最后一行。我不是Spring专家,但简单的请求属性在这里不起作用吗?即,
request.setAttribute(“secondModel”,model)第二个方法中的code>和映射modelFromSecondMethod=(Map)request.getAttribute(“secondModel”)第一个方法中的code>(在调用forward
之后)?还有什么原因不能在从控制器1调用的业务层类中的控制器2(方法2)中执行该过程?你真的需要呼叫另一个控制器吗?
@RequestMapping(method = RequestMethod.POST)
public String handleFormSubmission(..., final RedirectAttributes redirectAttrs) {
...
redirectAttrs.addFlashAttribute("AttributeName", value);
return "redirect:to_some_url_handled_by_BController";
}