当前位置:网站首页 > 创业 > 正文

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

0 张子豪 张子豪 2025-10-12 17:49 1

      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";

     }

注意事项

  • 希望对读者有所帮助~~~喜欢的请给小编点个大拇指

来源:百闻(微信/QQ号:9397569),转载请保留出处和链接!


本文链接:https://www.ibaiwen.com/web/246561.html

张子豪

张子豪

TA很懒,啥都没写...

@百闻娱乐 本站部分内容转自互联网,若有侵权等问题请及时与本站联系,我们将在第一时间删除处理。 | 粤ICP备2024343649号 | (地图