SpringMvc 页面跳转

----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";


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值