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
  • 阅读 ( 764 )
  • 分类:其他类型

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
联系我们:uytrv@hotmail.com 问答工具