SpringMVC开发中两种页面跳转方式

2024-11-08 08:17:57

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts2等。

工具/原料

电脑

SpringMvc

第一种:通过注解的方式进行跳转

1、第一种方法:通过HttpServletResponse的API直接输出步骤: 1.控制层:controller类的编写@Controllerpublic class RequestController{@RequestMapping("/request") //映射地址注入 public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception { rp.getWriter().println("request"); }

SpringMVC开发中两种页面跳转方式

3、 3.dispatcher-servlet.xml文件的编写争犸禀淫<beans xmlns="http://www.springframework.org/s艘早祓胂chema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!--扫描指定包下所有的注解的类--> <context:component-scan base-package="com.jsu.mvc"/></beans>

SpringMVC开发中两种页面跳转方式

5、第三种:使用HttpServletRequest 转发 @RequestMapping("/request") public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception { req.setAttribute("属性名","属性值"); //赋值 req.getRequestDispatcher(url).forward(rq,rp); //跳转 }

SpringMVC开发中两种页面跳转方式

2、第二种:使用modelview,它不需要配置视图解析器,但是不能指定跳转页面1.控制层controller的编写@RequestMapping("/modelmap")public String modelHello(String A,ModelMap B){ map.addAttribute("A",B); // 通过ModelMap键值对的方式设置传值 System.out.println(B); return "url";}

SpringMVC开发中两种页面跳转方式
猜你喜欢