• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

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

          Spring MVC屬于SpringFrameWork的后續產品,已經融合在Spring Web Flow里面。Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構,從而在使用Spring進行WEB開發時,可以選擇使用Spring的SpringMVC框架或集成其他MVC開發框架,如Struts1(現在一般不用),Struts2等。

    工具/原料

    • 電腦
    • SpringMvc

    第一種:通過注解的方式進行跳轉

    1. 1

      第一種方法:通過HttpServletResponse的API直接輸出

      步驟:

              1.控制層:controller類的編寫

      @Controller

      public class RequestController{

       @RequestMapping("/request")  //映射地址注入

        public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {

           rp.getWriter().println("request");

        }

    2. 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>

    3. 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>

    4. 4

      第二種方法:使用HttpServletResponse 類進行重定向跳轉視圖

        @RequestMapping("/Response")

        public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {

          rp.sendRedirect(url);  //跳轉的指定頁面。

        }

      }

    5. 5

      第三種:使用HttpServletRequest 轉發   

        @RequestMapping("/request")

        public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {

          req.setAttribute("屬性名","屬性值");  //賦值

          req.getRequestDispatcher(url).forward(rq,rp); //跳轉

          }

    第二種:使用Springvc提供的Model類

    1. 1

      第一種:使用modelandview進行跳轉。但是需要配置視圖解析器,而且能指定跳轉頁面。

      1.控制層controller的編寫

      public class HelloController implements Controller {

        @Override

        public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,

                         javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {

          ModelAndView view = new ModelAndView();

          view.addObject("A","B");   //A為屬性名,B為屬性值

          view.setViewName("index");  // 指定視圖的名稱

          return view;

        }

      }

      2.SpringMvc-servlet.xml的配置

      <!--配置渲染器-->

        <!--配置controller中頁面的位置-->

        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

        <bean id="viewResolver"

                 class="org.springframework.web.servlet.view.UrlBasedViewResolver">

        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

        <!--視圖的前綴-->

        <property name="prefix" value="/WEB-INF/jsp/"/>

        <!--視圖的后綴-->

        <property name="suffix" value=".jsp"/>  //指定跳轉的頁面為controller類設置的視圖名后面加上.jsp

      </bean>

        <bean name="/index.do" class="com.jsu.mvc.HelloController"></bean>

    2. 2

      第二種:使用modelview,它不需要配置視圖解析器,但是不能指定跳轉頁面

      1.控制層controller的編寫

       @RequestMapping("/modelmap")

       public String modelHello(String A,ModelMap B){

         map.addAttribute("A",B);   // 通過ModelMap鍵值對的方式設置傳值

         System.out.println(B);

         return "url";

       }

    注意事項

    • 希望對讀者有所幫助~~~喜歡的請給小編點個大拇指
    • 發表于 2018-01-29 00:00
    • 閱讀 ( 767 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆