Summary of springMVC several page jump methods

  • 2020-06-12 08:57:54
  • OfStack

You've seen several ways to configure Controller

Today mainly write 1 response interface jump several ways

1. In the form of annotations

1.1 Direct output via API (no need to configure renderer)

The main code for the controller class


@Controller
public class RequestController{
 @RequestMapping("/resp")
  public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {

     resp.getWriter().println("hello HttpServletResponse");

  }

web. xml configuration


<?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>

dispatcher-servlet.xml main code


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

  <!-- Scans all classes that contain annotations under the specified package -->
  <context:component-scan base-package="com.jsu.mvc"/>

</beans>

1.2 Redirect to another view using HttpServletResponse (nothing else)


  @RequestMapping("/resp")
  public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    resp.sendRedirect("index.jsp");

  }
}

1.3 Forward using HttpServletRequest (index. jsp page under default access/is not affected by renderer)


@RequestMapping("/resp")
  public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    req.setAttribute("message","it's forword ");
    req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

1.4 Directly returns the name of the jsp page (no renderer)

Other configurations remain the same


 @RequestMapping("/nice")
  public String hello1(){
    // Way forward 1
    return "home.jsp";
    // Way forward 2
    return "forward:index.jsp";
    // Redirect mode 
    return "redirect:index.jsp";
  }

1.5 When there is a renderer specified


 @RequestMapping("/nice")
  public String hello1(){
    // Way forward 1
    return "home";
    // Way forward 2
    return "forward:index";
    // Redirect mode  hello Refers to the requsrmapping
    return "redirect:hello";
  }

2 use view

2.1 use modelandview

The view parser is required to specify jump pages


public class HelloController implements Controller {


  @Override
  public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,
                   javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {

    ModelAndView mv = new ModelAndView();
    // Encapsulates the data to display into the view 
    mv.addObject("msg","hello myfirst mvc");
    // View name 
    mv.setViewName("hello");
    return mv;

  }
}

[servlet-name]-servlet.xml


<!-- Configure renderer -->
  <!-- configuration hellocontroller The location of the page in -->

  <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"/>
  <!-- The prefix for the result view -->
  <property name="prefix" value="/WEB-INF/jsp/"/>
  <!-- The suffix for the result view -->
  <property name="suffix" value=".jsp"/>
</bean>
  <bean name="/hello.do" class="com.jsu.mvc.HelloController"></bean>

2.2 use modelview

The view parser cannot specify a jump page without a view parser


 // through modelmap way 
  @RequestMapping("/modelmap")
  public String modelHello(String name,ModelMap map){
    map.addAttribute("name",name);
    System.out.println(name);

    return "index.jsp";
  }


Related articles: