Spring MVC framework is based on Spring framework, which can make it more convenient for us to develop the Web and realize front-end and back-end separation.
Ideas and principles
We defined a custom MVC framework just like Spring MVC, and both ideas are the same.
It is suggested to combine two articles for learning.
JSP Learning Notes (6) - Customized MVC Framework
First, we provide a Dispatch Servlet to intercept url requests. Then, according to url requests, we jump to the Controller layer, perform operations, and then return data.
Introduction
My demo uses the maven framework
1. Create maven projects
Configure as shown below
2. Adding dependencies
Modify pom.xml to add dependencies
At the beginning, the latest version (5.x.x) was used, and then a strange error was found. After a long time, no method was found, so the 4.x.x version was used. As a matter of fact, there was no problem, and the new version was useless.
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--Journal--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.9.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.9.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.9.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.9.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.3.9.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.9.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.release</version> </dependency> <!--AOP--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.9.release</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>RELEASE</version> </dependency> <!-- springmvc Dependent json Library (if used@responsebody Annotation return json Data) --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <!-- jstl 1.2.5 version libarary --> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-spec</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.5</version> </dependency> <!-- oracle driver --> <dependency> <groupId>com.github.noraui</groupId> <artifactId>ojdbc8</artifactId> <version>12.2.0.1</version> </dependency> <!-- mybatis orm frame --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!-- spring integration mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> <!-- File upload and download --> <!-- <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> -->
3. Project structure optimization
Since the new project is based on the maven template, some folders need to be added to the project structure
Create a new java folder under main folder
Set the java folder to source directory
Create your own package name under the java folder, and then create a new controller folder and model folder. Incidentally, create a new resource folder. Same steps as above, set it to resources directory
In addition, you need to create a new views folder under the webapp folder
4. Setting up Tomcat configuration
I have set it up here. If not, there is no drop-down menu, but there is an option to add configuration.
Select the settings for tomcat, select local, and if there is no option for Tomcat, click show more at the bottom of the option.
After clicking, add the constructor
Choose the exploded option
Set url
After running the Web program, you can access the homepage of the Web project by visiting http://localhost:8080/springmvcdemo.
5. New Spring MVC configuration file
Spring MVC configuration file is the same as the previous spring file, which is used to configure the related bean s. Because it is a resource file, we put it in the resources folder according to the rules.
springmvc-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- Yes web All classes in the package are scanned to complete Bean Functions for creating and automating dependency injection--> <!-- Mark it. controller and requestmapping Annotated classes and methods are saved and then invoked by reflection --> <context:component-scan base-package="com.wan.controller"/> <!--Support spring3.0+ New mvc annotation,It's not possible without some annotations, such as json Converted@ResponseBody <context:annotation-config/> //Implicitly register with Spring container 1. AutowiredAnnotationBeanPostProcessor Solving automatic assembly of data or components 2. CommonAnnotationBeanPostProcessor Solution compatibility JSR250 Notes to the Norms:@Resource,@PostConstruct,... 3. PersistenceAnnotationBeanPostProcessor Resolving persistent annotation processing 4. RequiredAnnotationBeanPostProcessor //These four BeanPost Processors. enable-matrix-variables="true": Characteristics of Opening Matrix Variables for Data Acquisition --> <mvc:annotation-driven enable-matrix-variables="true"> <mvc:async-support default-timeout="10"/><!--Subelements can specify asynchronous interceptors--> </mvc:annotation-driven> <!-- To configure*.js,*.css,*.jpg,*.html Please wait. DispatcherServlet Handling, but direct handing over tomcat Default of service Servlet To deal with. //Different servers have different default Servlet names, but tomcat default Servlet names are "default" --> <mvc:default-servlet-handler/> <!--Analysis of Model View Names by Adding Prefixes and Suffixes to Model View Names UserController.login(){ return "success"; //spring mvc is parsed into a corresponding JSP (view)/views/success.jsp } --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/views/" p:suffix=".jsp"> <!-- /views/[login].jsp --> <!-- And p:prefix, p:suffix equivalence <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> --> <!-- If used jstl If so, configure the following properties --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans>
There is a bean class in the configuration, which is the configuration view parser (that is, the last bean tag), and we use the Internal ResourceViewResolver.
This parser will process the return value of the request processing class (controller class) processing method according to the format of "prefix + method return value + suffix", and jump the processed return value as a path.
In addition, there are other parsers, which will be supplemented below.
6. Configure the web.xml file
Because we use maven's template to create a web project, the content in web.xml is not what we need, so we have to change the content.
<?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"> <display-name>SpringMVC Demo</display-name> <!-- springmvc Core controller, will springMVC Integrate into the project--> <servlet> <servlet-name>springmvc-DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- To configure spring mvc Component: Scanning controller, view resovle --> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <!-- Loading order at server startup --> <load-on-startup>1</load-on-startup> <!-- Asynchronous request processing support --> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>springmvc-DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
7. test
Let's use a simple example to understand how spring MVC works
We write a controller class to simulate the implementation of login operations, login success, jump to the successful page success.jsp
UserController.java
package com.wan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller public class UserController { @RequestMapping("/user/login") public String login() { //Here the return is followed by a prefix and a suffix. //It's equivalent to jumping to views/success.jsp page return "success"; } }
In success.jsp, there is only one simple "login success" text
In index.jsp, there is a link requesting url to be user/login
<a href="user/login">Sign in</a>
Then you can jump to the page.
PS: The above method returns a success, which is automatically prefixed and suffixed. Note that this is the request forwarding.
In addition, we can add forward or redirect prefix to forward or redirect requests.
However, if you use these two prefixes, then the view parser will not automatically add prefixes and suffixes. So, we have to specify the url address for the specific jump.
@RequestMapping("/user/login") public String login() { //Request forwarding return "forward:/views/success.jsp"; } @RequestMapping("/user/login") public String login() { //redirect return "redirect:/views/success.jsp"; }
Request Mapping Annotation
Advanced use
The Request Mapping annotation in the spring MVC framework is not as simple as the annotation in our previous custom MVC framework. It can also annotate a class.
For example:
package com.wan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login") public String login() { //The return here is equivalent to a page jumping to views/success.jsp return "success"; } }
The url request on our link is / user/login, not login.
Property description and use
attribute | Explain |
---|---|
value | The actual url address of the specified request is the default attribute, such as @RequestMapping("/login") equivalent to @RequestMapping (value="/login" |
method | Specify the method of the request, post or get |
params | Provide that the parameters in the request must satisfy certain conditions |
header | Provide that the header in the request must satisfy certain conditions |
1.method
package com.wan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login",method="RequestMethod.POST") public String login() { //The return here is equivalent to a page jumping to views/success.jsp return "success"; } }
Later, if the request is not post, a 405 error will occur.
2.params
Use this property to constrain the parameters of the request
Example:
package com.wan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login",params={"name","age!=23"}) public String login() { //The return here is equivalent to a page jumping to views/success.jsp return "success"; } }
The url request parameters constrained by the above example must contain name and age, and age cannot be equal to 23. If the condition is not met, 404 errors will occur.
Such as:
<!-- Satisfiable url request --> <a href="user/login?name=zhang&age=21">Sign in</a>
The following expressions can be received in params
Expression | Explain |
---|---|
paramName | The url request must contain the paramName parameter name |
!paramName | The url request cannot contain the paramName parameter name |
paramName!=xx | The url request must contain the paramName parameter name, and the parameter value is not equal to xx |
header is less used, so it's not added here.
Get the request url parameter value
To get the parameter value of the url request, we can use the RequestParam annotation
Using this annotation, you can assign the value of the url request parameter to the method parameter
Following is a description of common attributes for the @RequestParam annotation
attribute | Explain |
---|---|
value | The parameter name for the request to carry the parameter |
required | Identifying the url parameter of the request is that there must be a specific parameter, true (default): it must exist, if it does not exist, an exception will occur; false: it does not exist |
defaultValue | Give a default value to the method parameter, and use the default value if the request url does not exist |
package com.wan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login") public String login(@RequestParam(value="username") String name) { //The return here is equivalent to a page jumping to views/success.jsp return "success"; } }
The request url is user/login? User name = zhang, and then the request parameter is assigned to the method parameter name.
Like our custom mvc framework, in spring mvc framework, we can also directly use entity classes, session, request, response as method parameters.
@RequestMapping("/user/login") public login(Student student){ ... } @RequestMapping("/user/login") public login(HttpServletRequest request,HttpServletResponse response,HttpSession session){ ... }
Similar to RequestParam are these two RequestHeader s and CookieValue
I haven't used these two annotations very much now. Let's take a look at them for a while and leave them alone.
- RequestHeader annotation, which is mainly used to obtain the data of the request header
- CookieValee annotation, which is mainly used to get a cookieValue
Return json data
When we use @ResponseBody, springmvc automatically turns us into json data when the method returns to the entity class or collection.
Before use, you need to import these two jars, jackson-core.jar and jackson-databind.jar. The previous dependency already includes the following two jars
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.3</version> </dependency>
UserController.java
package com.wan.controller; import com.wan.model.Teacher; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller @RequestMapping("/user") public class UserController { @ResponseBody @RequestMapping("/login") public Teacher login() { return new Teacher("001", "Zhang San"); } }
Then use ajax asynchronous requests in jsp pages
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> <html> <head> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script type='text/javascript'> function getData() { $.getJSON("user/login", function(json){ console.log(json); }); } </script> </head> <body> <button onclick="getData()">Sign in</button> </body> </html>
You can see the printed json data in the console
Processing Model Data
In Spring MVC, M actually stands for Model, which is equivalent to data.
Suppose we want to query data from data: first we send url requests from the View, then the Controller gets the data from the database through Service/Dao, and processes the data so that the data can be sent back to the View and displayed.
If it's an asynchronous request, we can return a json data to the page. If it's not, we have to store the data in the scope of the request or session, and then the page (View) takes the data out of the scope and displays it.
Spring MVC provides four ways to handle views that need to be pulled out of scope for data display
- ModelAndView
- Map, ModelMap and Model
- @SessionAttributes
- @ModelAttribute
1.ModelAndView
This class is commonly used as the return value of a method to return a page (View) with data.
UserController.java
package com.wan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login") public ModelAndView login() { String view = "success"; ModelAndView mav = new ModelAndView(view); Teacher teacher = new Teacher("001","Zhang San"); //Equivalent to request.addAttribute("teacher",teacher) mav.addObject("teacher",teacher); return mav; } }
Remove data from success.jsp and display it
<body> ${requestScope.student.tno} </body>
The example above is the same as before, and will be prefixed and suffixed to get views/success.jsp
2.Map, ModelMap and Model
Map, ModelMap, and Model are commonly used as parameters of a method, and then data is stored in it through the put method.
package com.wan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login") public String login(Map<String,Object> map) { String view = "success"; Teacher teacher = new Teacher("001","Zhang San"); //Equivalent to request.addAttribute("teacher",teacher) map.put("teacher",teacher); return "success"; } }
ModelMap and Map are used the same way
You can also use Model
@RequestMapping("/login") public String login(Model model) { String view = "success"; Teacher teacher = new Teacher("001","Zhang San"); //Equivalent to request.addAttribute("teacher",teacher) model.addAttribute("teacher",teacher); return "success"; }
The Model class can also use addAllAttribute (Map < String,?> map) to add a map data.
3.@SessionAttributes
The first two methods are put in the request scope. If we want to put in the session scope, we can use the @SessionAttributes annotation, which is generally labeled on the class.
@ Session Attributes can add a specified object to the session scope, or a certain type of object to the session.
The following example specifies the key as the teacher object and adds it to the session scope
package com.wan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller @RequestMapping("/user") @SessionAttributes(value="teacher") public class UserController { @RequestMapping("/login") public String login(Map<String,Object> map) { String view = "success"; Teacher teacher = new Teacher("001","Zhang San"); //Adding session scope as well as request scope map.put("teacher",teacher); return "success"; } }
Add Teacher-type objects to session scope
package com.wan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author StarsOne * @date Create in 2019/9/28 0028 14:27 * @description */ @Controller @RequestMapping("/user") @SessionAttributes(type=Teacher.class) public class UserController { @RequestMapping("/login") public String login(Map<String,Object> map) { String view = "success"; Teacher teacher = new Teacher("001","Zhang San"); //Adding session scope as well as request scope map.put("teacher",teacher); return "success"; } }
4.@ModelAttribute
Usage:
We need to update the data, but we can only update one attribute of the data.
When we click on the edit, there is only one input box for us to enter to change that property. When we input the changed property value, we will find that the data in the controller and the incoming objects are null except the changed property value. We don't want this to happen, so we use this annotation.
This annotation is used to modify a method in the controller, and then executes before executing the @RequestMapping method in the controller, updates the data in the incoming object, and then performs the modification operation, only changes the attribute values that the object needs to modify, while the other attribute values remain unchanged (not null).
Personally, I think it's more troublesome. A better solution to the above situation is to use multiple input boxes instead of a single input box, to disable those input boxes that can't be changed, and then submit the form and successfully pass in other attribute values.
view resolver
Workflow
In spring MVC framework, after the request processing method (method in Controller) is executed, a ModelAndView object is finally returned.
Spring MVC uses ViewResolver to get the final View object. The final View can be JSP, Excel, JFreeChart and other forms of View.
The processor does not care about which view object is used to render the model data (that is, we often say to take the data out of the scope of request and display it on the page). The processor focuses on the production of model data, so as to achieve full decoupling of MVC.
For those processing methods that return String, View or ModeMap, Spring MVC also internally assembles them into a ModelAndView object, which contains the logical name and the view of the model object.
The following picture:
View
View in spring MVC is actually an interface. Here are the implementation classes of the common View interface
View type | Implementation class | Explain |
---|---|---|
URL view type | InternalResourceView | Encapsulate JSP or other resources into a view. It is used by default by the view parser Internal ResourceViewResolver. |
URL view type | JstlView | A subclass of InternalResourceView. If JSP uses JSTL's internationalized tags, you need to use the view class |
Document view | AbstractExcelView | The abstract class of Excel document view |
Document view | AbstractPdfView | The abstract class of PDF document view |
Report view | ConfigurableJasperReportsView | |
Report view | JasperReportsHtmlView | |
Report view | JasperReportsPdfView | |
Report view | JasperReportsXlsView | |
JSON view | MappingJackson2JsonView | Data is output in JSON mode through the ObjectMapper object of Jackson Framework |
ViewResolver and subclasses
ViewResolver, like View, is an interface
View parser type | Class name | Explain |
---|---|---|
Resolve to a mapping file | UrlBasedViewResolver | It simply implements the ViewResolver interface and accesses resources through logical view names without any mapping |
Resolve to a mapping file | InternalResourceViewResolver | Resolve the logical view name into a path |
Resolve to bean s | BeanNameViewResolver | Resolve the logical view name to the name attribute of the bean, so that the corresponding bean can be found according to the name attribute. |
Resolve to bean s | ResourceBundleResolver | Like BeanNameViewResolver, only the view-bean s defined are in a properties file, which is used to load the properties file. |
Resolve to bean s | XmlViewResolver | Just like ResourceBundle Resolver, the view-bean defined is used to load an xml file in an xml file. |
Resolve to template file | VelocityViewResolver | Support for Velocity Template Engine |
Resolve to template file | FreeMarkerViewResolver | Support for FreeMarker Template Engine |
Here, I will only introduce the first two types that we have used. For more information, please refer to our link below.
AbstractCachingViewResolver, which is an abstract class, implements the ViewResolver interface. The abstract class can only be inherited and cannot create instances.
UrlBasedViewResolver extends functionality by inheriting from AbstractCachingViewResolver.
AbstractCaching ViewResolver introduces:
This view parser saves the views it has parsed, and then looks in the cache every time it parses the view.
If the corresponding view is found, it will be returned directly. If not, a new view object will be created. Then it will be put into a map for caching, and then the new view will be returned.
Using this view caching approach can minimize the performance problems of parsing views.
UrlBasedViewResolver introduces:
AbstractCachingViewResolver is inherited from AbstractCachingViewResolver, which mainly provides a way to parse views by splicing URLs. It allows us to specify a specified prefix by prefix attribute, a specified suffix by suffix attribute, and then add the name of the returned logical view with the specified prefix and suffix to the specified view URL.
Introduced by International Resource View Resolver:
This class is inherited from UrlBasedViewResolver, which has both functions and its own features. In literal translation, the Internal ResourceViewResolver is the internal resource resolver.
The Internal ResourceViewResolver resolves the returned view names into the Internal ResourceView objects, and the Internal ResourceView stores the model attributes returned by the Controller processor methods into the corresponding request attributes, and then redirects the request for word to the target URL on the server side through the Request Dispatcher.
Reference link: Spring MVC - Start from scratch - view-ViewResolver
Static resource access
scene
If we want to access a picture, js file, video and other static resources in our project through a url, we will find a 404 error.
The reason is that we define a pre-Servlet that handles all url requests, but because we can't find the matching url on the RequestMapping annotation, a 404 error occurs.
Solution
Adding <mvc: default-servlet-handler/> and <mvc: annotation-driven> </mvc: annotation-driven> to the spring MVC configuration file can solve the problem.
In the springmvc configuration file given earlier, these two tags have actually been added. Here's a brief introduction of the role
<mvc: default-servlet-handler/> Function: In the context of Spring MVC, a Default Servlet HttpRequestHandler is defined, which checks requests processed by @Dispatcher Servlet. If a request is not processed by the corresponding @RequestMapping, the request will be handed over to the default Servlet of the Web server, and the default Servlet will be processed directly according to the U. RL to access the resource
Function of <mvc: annotation-driven> </mvc: annotation-driven>: While accessing static resources, the eye can normally access other non-static resources.
Both tags need to be added
Chinese garbled method (Supplement)
1. setting page encoding
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
2. Configuring filters
Configuration in 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"> <display-name>SpringMVC Demo</display-name> <!-- Chinese transcoding must be added to the core controller. --> <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>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!-- springmvc Core controller, will springMVC Integrate into the project--> <servlet> <servlet-name>springmvc-DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- To configure spring mvc Component: Scanning controller, view resovle --> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <!-- Loading order at server startup --> <load-on-startup>1</load-on-startup> <!-- Asynchronous request processing support --> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>springmvc-DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
For more information, see the reference link
Reference link: thoroughly solve springMVC Chinese garbled code