SpringMVC 作为控制层,发现想要跳转页面的地址总是与请求方法的路径相关。
用ModelAndView 和视图解析器都是一样的结果。
例如ModelAndView mv = new ModelAndView(“forward:success.jsp”);
目的是指向: http://localhost:端口号/项目名/success.jsp
却在实际运行时指向:http://localhost:端口号/项目名/queryProductById/success.jsp
有查到说是因为ModelAndView 该类导包导错,不是import org.springframework.web.portlet.ModelAndView; 而应是import org.springframework.web.servlet.ModelAndView;
但是在我的项目中 ModelAndView 只能导第二个本应正确的包,所以不是这个问题。
最后发现是因为请求路径应该加/,表示绝对路径,若不加/则会被SpringMVC自动加上 请求方法前缀。
@Controller
public class ProductController {
@RequestMapping("queryProductById")
public ModelAndView queryProductById(int product_id)
{
ModelAndView mv = new ModelAndView("forward:success.jsp");//应为"forward:/success.jsp"
return mv;
}
}
@Controller
public class ProductController {
@RequestMapping("queryProductById")
public StringqueryProductById(int product_id)
{
return "success";//应为"/success.jsp"
}
}