2.0springmvc view

gh-xiaohe 2022-06-24 07:53:06 阅读数:965

2.0springmvcspringmvcview

author : gh-xiaohe
gh-xiaohe The blog of
If you think the blogger's article is good , I hope you'll make it three times in a row ( Focus on , give the thumbs-up , Comment on ), Give me more support !!!

SpringMVC The view of

SpringMVC The view in is View Interface , The function of view Render data , Put the model Model The data in is displayed to the user

SpringMVC There are many kinds of views , The default is Forwarding view and Redirect view

  • When the project introduces jstl Dependence , Forwarding view Meeting Automatically Convert to JstlView

  • If the view technology used is Thymeleaf, stay SpringMVC Of The configuration file Configuration of the Thymeleaf View parser , thus After the view parser parses What you get is ThymeleafView

1、ThymeleafView

When the view name set in the controller method There is no prefix when , The view name will be SpringMVC Configured in the configuration file View parser parsing , The view name is the final path obtained by splicing the view prefix and view suffix , Will pass forward Jump by

@RequestMapping("/testHello")
public String testHello(){

return "hello";
}

Source code

DispatcherServlet
// All of them ModelAndView
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
// Handle ModelAndView Methods that encapsulate model data and view information process perform Dispatch forward Result result 
this.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException); // mv Would be right ModelAndView To deal with 
this.render(mv, request, response); // render Process execute rendering Deal with the end result 
// mv: "ModelAndView [view="success"; model={}]"viewName:"success" 
String viewName = mv.getViewName(); // Get the view name 

 Insert picture description here

2、 Forwarding view

SpringMVC The default forwarding view in is InternalResourceView

SpringMVC Create a forwarding view in :

When the view name set in the controller method is marked with "forward:" by Prefix when , establish InternalResourceView View , The view name at this time Can't By SpringMVC Configured in the configuration file View parser parsing , It's about putting Prefix "forward:" Get rid of , The rest is for The final path adopt forward Jump by

The browser sends a request , You can visit WEB-INIF Resources under , No cross domain

It will help us create two views

① analysis forward:/testHello route , establish InternalResourceView View

②testHello It is also a request After creating a view view establish ThymeleafView View

for example "forward:/",“forward:/employee”

@RequestMapping("/testForward")
public String testForward(){

return "forward:/testHello"; // Forwarding to testHello This request 
}

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-U79njSdA-1655816980803)(C:\Users\gh\Desktop\ Silicon Valley Maven+RabbitMq\ Silicon Valley SpringMVC\ note \img\img003.png)]

3、 Redirect view

SpringMVC The default redirection view in is RedirectView

When the view name set in the controller method is marked with "redirect:" When prefixed , establish RedirectView View , The view name at this time Can't By SpringMVC Configured in the configuration file View parser parsing , It's about putting Prefix "redirect:" Get rid of , The rest passes as the final path Redirect Jump by

The browser sends two requests , No access WEB-INIF Resources under , You can cross domains

for example "redirect:/",“redirect:/employee”

@RequestMapping("/testRedirect")
public String testRedirect(){

return "redirect:/testHello";
}

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-90Nme4Ge-1655816980804)(C:\Users\gh\Desktop\ Silicon Valley Maven+RabbitMq\ Silicon Valley SpringMVC\ note \img\img004.png)]

notes :

Redirected view when parsing , Will be will be redirect: The prefix removed , Then it will determine whether the rest is in / start , If yes, the context path will be spliced automatically

4、 View controller view-controller

When the controller method , It is only used to realize page Jump , That is, when you only need to set the view name , You can use the processor method view-controller Label representation

Use requirement :

No other request processing is required , Just write a request map , Corresponding to a request path , Return a view name in the corresponding controller method

 @RequestMapping("/")
public String index() {

return "index";
}
@RequestMapping("/test_view")
public String testView() {

return "test_view";
}
SpringMVC.xml Configure the view controller in
<!-- path: Set the address of the request to be processed view-name: Set the view name corresponding to the request address -->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<mvc:view-controller path="/test_view" view-name="test_view"></mvc:view-controller>

notes :

When SpringMVC in Set any view-controller when , Request mappings in other controllers will All the failure , here need stay SpringMVC In the core configuration file of Set on mvc Annotation driven The label of :

<mvc:annotation-driven />

5、SpringMVC How to access the jsp

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_4_0.xsd" version="4.0">
<!-- Configure coding filter -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- To configure springMVC Front end controller for DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

SpringMVC.xml

 <context:component-scan base-package="com.atguigu.mvc.controller"></context:component-scan>
<!--jsp view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/templates/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

Controller

@Controller
public class JspController {

@RequestMapping("/success")
public String success(){

return "success";
}
}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1> home page </h1>
<%--pageContext.request.contextPath Get context path --%>
<a href="${pageContext.request.contextPath}/success">success.jsp</a>
</body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1> home page </h1>
<%--pageContext.request.contextPath Get context path --%>
<a href="${pageContext.request.contextPath}/success">success.jsp</a>
</body>
</html>

summary :( Difference )

① The configured view parser is different

② The way to get the request address is different

<%--pageContext.request.contextPath Get context path --%>
jsp <a href="${pageContext.request.contextPath}/success">success.jsp</a>
<a th:href="@{/hello/testRequestMapping}"> test RequestMapping The position of the annotation </a><br>
copyright:author[gh-xiaohe],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240320543874.html