ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【SpringMVC】学习笔记04-结果跳转方式

2022-07-12 23:31:59  阅读:164  来源: 互联网

标签:解析器 RequestMapping 04 SpringMVC 视图 jsp 跳转 model public


  ModelAndView

设置ModelAndView对象,根据view的名称,和视图解析器跳转到指定的页面。

页面:{视图解析器前缀}+viewName+{视图解析器后缀}

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

对应的conctroller类

public class HelloController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //ModelAndView 模型和视图
        ModelAndView mv=new ModelAndView();
        //调用业务层
        //封装对象,放到ModelAndView中。Model
        mv.addObject("msg","HelloSpringMVC!");
        //封装要跳转的视图,放在ModelAndView中
        mv.setViewName("hello");// /WEB-INF/jsp/hello.jsp

        return mv;
    }
}

 ServletAPI

通过设置ServletAPI , 不需要视图解析器 .

1、通过HttpServletResponse进行输出

2、通过HttpServletResponse实现重定向

3、通过HttpServletResponse实现转发

@Controller
public class ResultGo {

   @RequestMapping("/result/t1")
   public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
       rsp.getWriter().println("Hello,Spring BY servlet API");
  }

   @RequestMapping("/result/t2")
   public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
       rsp.sendRedirect("/index.jsp");
  }

   @RequestMapping("/result/t3")
   public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
       //转发
       req.setAttribute("msg","/result/t3");
       req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
  }

}

SpringMVC实现转发和重定向-无需视图解析器

//转发的话 url不会发生变化
@RequestMapping("/m1/t2")
public String test20(Model model){
model.addAttribute("msg","不用视图解析器实现抓发");
//转发方式一
return "/WEB-INF/jsp/hello.jsp";

}
@RequestMapping("/m1/t3")
public String test03(Model model){
model.addAttribute("msg","不适用视图解析器实现转发:forward");
return "forward:/WEB-INF/jsp/hello.jsp";
}
@RequestMapping("/m1/t4")
public String test04(Model model){
model.addAttribute("msg","不使用视图解析器实现重定向:redirect");
return "redirect:/index.jsp";
}

重定向,不需要视图解析器,本质上就是重新请求一个新地方,所以注意路径问题。

可以重定向到另一个请求实现。

 

通过SpringMVC来实现转发和重定向-有视图解析器

   @RequestMapping("/m1/t5")
    public String test05(Model model){
        //转发
        model.addAttribute("msg","使用视图解析器实现转发");
        return "hello";

    }
    @RequestMapping("/m1/t6")
    public String test06(Model model){
        //重定向
        model.addAttribute("msg","使用视图解析器实现重定向");
        return "redirect:/index.jsp";
    }

 

标签:解析器,RequestMapping,04,SpringMVC,视图,jsp,跳转,model,public
来源: https://www.cnblogs.com/WangGuangYuan/p/16468483.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有