----1、使用HttpServletRequest request,HttpServletResponse response 进行页面跳转
// 转发页面带参数
@RequestMapping("/resp")
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
req.setAttribute("message","it's forword ");
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
// 重定向
@RequestMapping("/resp")
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
resp.sendRedirect("index.jsp");
}
----2、在springmvc中 在进行配置视图解析器后,
<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/page"></property>
<property name="suffix" value=".jsp"></property>
</bean>
1)、 return "index"; -----直接返回/page/index.jsp 页面
2)、 在方法上加@ResponseBody 或 使用@RestContorller , return "index"; 就会直接变成json字符串返回给浏览器
3)、转向 ,返回页面并带参数
--ModelMap
public String login(String name,String pass ,ModelMap model){
User user = userService.login(name,pwd);
model.addAttribute("user",user);
model.put("name",name);
return "success"; //success.jsp
}
--ModelAndView
public ModelAndView toAddUser(HttpServletRequest request,ModelAndView mv){
ModelAndView mv = new ModelAndView();
mv.setViewName("index"); //跳转页面
Map map = new HashMap();
mv.addObject("map", map);
return mv;
}
4)、重定向
1)、return "redirect:/ toList "; ----不带参数
2)、使用RedirectAttributes 带参数
public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr) throws Exception {
String code = service.save(form);
attr.addFlashAttribute("name", form.getName());
attr.addFlashAttribute("success", "添加成功!");
return "redirect:/index";