Spring MVC ModelAndView转发无效。以下代码不会将请求转发给Name.jsp。非常感谢你的帮助。
package main.java.com.mkyong.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class indexController {@RequestMapping(value= {"/index"})
public ModelAndView forward() {
ModelAndView model = new ModelAndView( "forward:/pages/Name.jsp");
model.addObject("msg","Forward Handled");
return model;
}}
答案 0 :(得分:3)
你需要将返回类型替换为String并返回" forward:/ some / where /"所以,你请求处理程序看起来像这样:
package main.java.com.mkyong.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class indexController {@RequestMapping(value= {"/index"})
public String forward() {
model.addObject("msg","Forward Handled");
return "forward:/some/where/to/go";
}}
答案 1 :(得分:1)
尝试使用此
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView forward(Model model) {
model.addAttribute("msg","Forward Handled");
return new ModelAndView("/pages/Name.jsp");
}
答案 2 :(得分:0)
请注意,根据您的用例,可接受的答案可能不正确。
要使用ModelAndView转发(有时您不能返回字符串):
return new ModelAndView("forward:/error");