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

2025-11-11 23:55:45

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.sendRedirect(url);  //跳转的指定页面。

  }

}

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

5、第三种:使用HttpServletRequest 转发   

  @RequestMapping("/request")

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

    req.setAttribute("属性名","属性值");  //赋值

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

    }

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

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>

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开发中两种页面跳转方式

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢