SpringMVC開發中兩種頁面跳轉方式

      Spring MVC屬于SpringFrameWork的后續產品, 已經融合在Spring Web Flow里面 。 Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊 。 使用 Spring 可插入的 MVC 架構, 從而在使用Spring進行WEB開發時, 可以選擇使用Spring的SpringMVC框架或集成其他MVC開發框架, 如Struts1(現在一般不用), Struts2等 。
工具/原料電腦
SpringMvc
第一種:通過注解的方式進行跳轉1第一種方法:通過HttpServletResponse的API直接輸出
步驟:
        1.控制層:controller類的編寫
@Controller
public class RequestController{
 @RequestMapping("/request")  //映射地址注入
  public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
     rp.getWriter().println("request");
  }

SpringMVC開發中兩種頁面跳轉方式



2       2.web.xml文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

SpringMVC開發中兩種頁面跳轉方式



3        3.dispatcher-servlet.xml文件的編寫
<beans xmlns="http://www.springframework.org/schema/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開發中兩種頁面跳轉方式



4第二種方法:使用HttpServletResponse 類進行重定向跳轉視圖
  @RequestMapping("/Response")
  public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
    rp.sRedirect(url);  //跳轉的指定頁面 。
  }
}

SpringMVC開發中兩種頁面跳轉方式



5第三種:使用HttpServletRequest 轉發   
  @RequestMapping("/request")
  public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
    req.setAttribute("屬性名","屬性值");  //賦值
    req.getRequestDispatcher(url).forward(rq,rp); //跳轉

猜你喜歡