Spring MVC notes (crazy God)

Madness theory

1. Review MVC

1.1 what is MVC

  • MVC is the abbreviation of model, view and controller. It is a software design specification.
  • It is a method to organize code by separating business logic, data and display.
  • The main function of MVC is to reduce the two-way coupling between view and business logic.
  • MVC is not a design pattern, MVC is an architecture pattern. Of course, there are differences between different MVCs.

Model: the data model provides the data to be displayed, so it contains data and behavior. It can be considered as a domain model or JavaBean component (including data and behavior), but now it is generally separated: Value Object (data Dao) and Service layer (behavior Service). That is, the model provides functions such as model data query and model data status update, including data and business.

**View: * * is responsible for displaying the model, which is generally the user interface we see and what customers want to see.

**Controller: * * receives the user's request and delegates it to the model for processing (state change). After processing, the returned model data is returned to the view, which is responsible for displaying it. In other words, the controller does the work of a dispatcher.

The most typical MVC is the pattern of JSP + servlet + javabean.

1.2 Model1 Era

  • In the early development of web, model 1 is usually used.
  • Model1 is mainly divided into two layers: view layer and model layer.

Advantages of Model1: simple architecture, more suitable for small project development;

Model1 disadvantages: JSP responsibilities are not single, responsibilities are too heavy, and it is not easy to maintain;

1.3 Model2 Era

Model 2 divides a project into three parts, including view, control and model.

  1. User sends request
  2. The Servlet receives the request data and calls the corresponding business logic method
  3. After the business is processed, the updated data is returned to the servlet
  4. servlet turns to JSP, which renders the page
  5. Respond to the front-end updated page

Responsibility analysis:

Controller: controller

  1. Get form data
  2. Call business logic
  3. Go to the specified page

Model: Model

  1. Business logic
  2. Status of saved data

View: View

  1. Display page

This not only improves the reuse rate of the code and the scalability of the project, but also greatly reduces the maintenance cost of the project. The implementation of Model 1 mode is relatively simple and suitable for rapid development of small-scale projects. The JSP page in Model 1 plays both the roles of View and Controller, mixing the control logic and presentation logic, resulting in very low code reusability and increasing the scalability of the application and the difficulty of maintenance. Model 2 eliminates the disadvantages of Model 1.

1.4 review Servlet

  1. Create a Maven project as the parent project! pom dependency!
<dependencies>
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.12</version>
   </dependency>
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
       <version>5.1.9.RELEASE</version>
   </dependency>
   <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <version>2.5</version>
   </dependency>
   <dependency>
       <groupId>javax.servlet.jsp</groupId>
       <artifactId>jsp-api</artifactId>
       <version>2.2</version>
   </dependency>
   <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>jstl</artifactId>
       <version>1.2</version>
   </dependency>
</dependencies>
  1. Establish a Moudle: springmvc-01-servlet and add Web app support!

  2. Import jar dependencies for servlet s and JSPS

    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <version>2.5</version>
    </dependency>
    <dependency>
       <groupId>javax.servlet.jsp</groupId>
       <artifactId>jsp-api</artifactId>
       <version>2.2</version>
    </dependency>
    
  3. Write a Servlet class to handle user requests

    package com.th.servlet;
    
    //Implement Servlet interface
    public class HelloServlet extends HttpServlet {
       @Override
       protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           //Get parameters
           String method = req.getParameter("method");
           if (method.equals("add")){
               req.getSession().setAttribute("msg","Yes add method");
          }
           if (method.equals("delete")){
               req.getSession().setAttribute("msg","Yes delete method");
          }
           //Business logic
           //View jump
           req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
      }
    
       @Override
       protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           doGet(req,resp);
      }
    }
    
  4. Write hello.jsp, create a JSP folder under the WEB-INF directory, and create hello.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
       <title>thshen</title>
    </head>
    <body>
    ${msg}
    </body>
    </html>
    
  5. Registering servlets 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">
       <servlet>
           <servlet-name>HelloServlet</servlet-name>
           <servlet-class>com.th.servlet.HelloServlet</servlet-class>
       </servlet>
       <servlet-mapping>
           <servlet-name>HelloServlet</servlet-name>
           <url-pattern>/user</url-pattern>
       </servlet-mapping>
    
    </web-app>
    
  6. Configure Tomcat and start the test

    • localhost:8080/user?method=add
    • localhost:8080/user?method=delete

What does the MVC framework do

  1. Mapping URLs to java classes or methods of java classes
  2. Encapsulates data submitted by users
  3. Processing request - calling related business processing - encapsulating response data
  4. Render the response data. jsp / html and other presentation layer data

explain:

Common server-side MVC frameworks include Struts, Spring MVC, ASP.NET MVC, Zend Framework and JSF; Common front-end MVC frameworks: vue, angularjs, react, backbone; Other patterns evolved from MVC, such as MVP, MVVM and so on

2. What is spring MVC

2.1 overview

Spring MVC is a part of the Spring Framework and a lightweight Web framework based on Java to implement MVC.

View official documents: https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/web.html#spring-web

Why should we learn spring MVC?

Features of Spring MVC:

  1. Lightweight, easy to learn
  2. Efficient, request response based MVC framework
  3. Good compatibility with Spring and seamless combination
  4. Convention over configuration
  5. Powerful functions: RESTful, data validation, formatting, localization, theme, etc
  6. Concise and flexible

Spring's web framework is designed around the dispatcher Servlet.

The dispatcher servlet is used to distribute requests to different processors. Starting from Spring 2.5, users using Java 5 or above can develop based on annotations, which is very concise;

Because Spring MVC is good, simple, convenient and easy to learn, it is naturally seamlessly integrated with Spring (using SpringIoC and Aop), and the use convention is better than configuration. It can carry out simple junit testing. It supports Restful style. Exception handling, localization, internationalization, data validation, type conversion, interceptors, etc. so we need to learn

The most important point is that there are many people and companies

2.2 central controller

  • Spring's web framework is designed around dispatcher servlet. The dispatcher servlet is used to distribute requests to different processors. Starting from Spring 2.5, users using Java 5 or above can adopt annotation based controller declaration.

Like many other MVC frameworks, the Spring MVC framework is request driven. It distributes requests and provides other functions around a central Servlet. The dispatcher Servlet is an actual Servlet (it inherits from the HttpServlet base class).

  • The principle of spring MVC is shown in the following figure:

    When a request is initiated, the front controller intercepts the request, generates a proxy request according to the request parameters, finds the actual controller corresponding to the request, processes the request, creates a data model, accesses the database, and responds to the model to the central controller. The controller renders the view results using the model and view, and returns the results to the central controller, The result is then returned to the requester.

    Original drawing

    Chinese map

2.3 implementation principle of spring MVC

The figure shows a relatively complete flow chart of spring MVC. The solid line indicates the technology provided by the spring MVC framework, which does not need to be implemented by developers, and the dotted line indicates that it needs to be implemented by developers.

Briefly analyze the implementation process

  1. Dispatcher servlet represents the front controller and is the control center of the whole spring MVC. When the user sends a request, the dispatcher servlet receives the request and intercepts the request.
    • We assume that the requested url is: http://localhost:8080/SpringMVC/hello
    • As above, the url is divided into three parts:
    • http://localhost:8080 ------>Server domain name
    • Spring MVC ----- -- > web sites deployed on servers
    • Hello ---- -- > indicates the controller
    • Through analysis, the above url is expressed as: request the hello controller of the spring MVC site located on the server localhost:8080.
  2. HandlerMapping is processor mapping. DispatcherServlet calls HandlerMapping, which looks up the Handler according to the request url.
  3. HandlerExecution refers to a specific Handler. Its main function is to find the controller according to the url. The controller found by the url above is: hello.
  4. HandlerExecution passes the parsed information to DispatcherServlet, such as parsing controller mapping.
  5. The HandlerAdapter represents a processor adapter that executes the Handler according to specific rules.
  6. The Handler lets the specific Controller execute.
  7. The Controller returns the specific execution information to the HandlerAdapter, such as ModelAndView.
  8. The HandlerAdapter passes the view logical name or model to the dispatcher servlet.
  9. DispatcherServlet calls the view resolver to resolve the logical view name passed by the HandlerAdapter.
  10. The view parser passes the parsed logical view name to the dispatcher servlet.
  11. DispatcherServlet calls a specific view according to the view result parsed by the view parser.
  12. The final view is presented to the user.

3. The first spring MVC program

3.1 configuration version

  1. Create a new Moudle, springmvc-02-hello, and add web support!

  2. Confirm that the dependency of spring MVC is imported!

  3. Configure web.xml and register dispatcher Servlet

    <?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">
    
       <!--1.register DispatcherServlet-->
       <servlet>
           <servlet-name>springmvc</servlet-name>
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <!--Associate a springmvc Configuration file for:[servlet-name]-servlet.xml-->
           <init-param>
               <param-name>contextConfigLocation</param-name>
               <param-value>classpath:springmvc-servlet.xml</param-value>
           </init-param>
           <!--Startup level-1-->
           <load-on-startup>1</load-on-startup>
       </servlet>
    
       <!--/ Match all requests; (excluding.jsp)-->
       <!--/* Match all requests; (including.jsp)-->
       <servlet-mapping>
           <servlet-name>springmvc</servlet-name>
           <url-pattern>/</url-pattern>
       </servlet-mapping>
    
    </web-app>
    
  4. Write the spring MVC configuration file! Name: SpringMVC-servlet.xml: [servletname] - servlet.xml

    Note that the name requirements here are in accordance with the official

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
          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">
    
    </beans>
    
  5. Add process mapper (can be omitted)

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    
  6. Add processor adapter (can be omitted)

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
  7. Add view parser

    <!--view resolver :DispatcherServlet Give it to him ModelAndView--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">   <!--prefix-->   <property name="prefix" value="/WEB-INF/jsp/"/>   <!--suffix-->   <property name="suffix" value=".jsp"/></bean>
    
  8. Write the business Controller that we want to operate, either implement the Controller interface or add annotations; we need to return a ModelAndView, load data and seal the view;

    package com.th.controller;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//Note: Here we first import the Controller interface public class hellocontroller implements Controller {public ModelAndView handlerequest (HttpServletRequest, httpservletresponse) throws exception {/ / ModelAndView model and view ModelAndView mv = new ModelAndView() ; / / encapsulate the object and put it in ModelAndView. Model mv.addobject ("MSG", "hellosppringmvc!"); / / encapsulate the view to jump to and put it in ModelAndView mv.setviewname ("hello"); / /: / WEB-INF / JSP / hello.jsp return MV;}}
    
  9. Give your class to the spring IOC container and register the bean

    <!--Handler--><bean id="/hello" class="nuc.ss.controller.HelloController"/>
    
  10. Write the jsp page to jump to, display the data stored in ModelandView, and our normal page;

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>   <title>He is at ease</title></head><body>${msg}</body></html>
    
  11. Configure Tomcat startup test!

Possible problems: visit 404, troubleshooting steps:

  1. Check the console output to see if there is any missing jar package.
  2. If the jar package exists and the display cannot be output, add lib dependency in the project release of IDEA!
  3. Restart Tomcat to solve the problem!

Summary: let's look at an annotated version implementation, which is the essence of spring MVC.

3.2 annotation Version (key, configuration code)

1. Create a new Moudle, springmvc-03-hello-annotation. Add web support!

2. Since Maven may have the problem of resource filtering, we will improve the configuration

<build>   <resources>       <resource>           <directory>src/main/java</directory>           <includes>               <include>**/*.properties</include>               <include>**/*.xml</include>           </includes>           <filtering>false</filtering>       </resource>       <resource>           <directory>src/main/resources</directory>           <includes>               <include>**/*.properties</include>               <include>**/*.xml</include>           </includes>           <filtering>false</filtering>       </resource>   </resources></build>

3. Introduce relevant dependencies in pom.xml file: mainly Spring framework core library, Spring MVC, servlet, JSTL, etc. we have introduced them in parent dependencies!

4. Configure 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 " ><! -- register servlet -- > < servlet > < servlet name > SpringMVC < / servlet name > < servlet class > org. Springframework. Web. Servlet. Dispatcherservlet < / servlet class > <! -- location of initialization Spring configuration file -- > < init param > < param name > contextconfiglocation < / param name > < param value > classpath: SpringMVC ser Vlet.xml < / param value > < / init param > <! -- start sequence. The smaller the number, the earlier the start -- > < load on startup > 1 < / load on startup > < / servlet > <! -- all requests will be intercepted by Spring MVC -- > < servlet mapping > < servlet name > Spring MVC < / servlet name > < URL pattern > / < / url pattern > < / servlet mapping > < / web app > <-- This is a fixed configuration when developing with annotations -- >

Note:

/Difference between and / *:

< URL pattern > / < / url pattern > will not match. jsp, only for the requests we write; that is,. jsp will not enter the DispatcherServlet class of spring.

< URL pattern > / * < / url pattern > will match *. jsp. When returning the jsp view, you will enter the DispatcherServlet class of spring again, resulting in no corresponding controller, so a 404 error is reported.

    • Pay attention to the version of web.xml. The latest version! 4.0
    • Register DispatcherServlet
    • Associated spring MVC configuration file
    • The startup level is 1
    • The mapping path is / [do not use / *, it will 404]

5. Add Spring MVC configuration file

Add the springmvc-servlet.xml configuration file in the resource directory. The configuration form is basically similar to the Spring container configuration. In order to support annotation based IOC, the function of automatic package scanning is set. The specific configuration information is as follows:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "       xmlns:context=" http://www.springframework.org/schema/context "       xmlns:mvc=" http://www.springframework.org/schema/mvc "       xsi:schemaLocation=" http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        https://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/mvc        https://www.springframework.org/schema/mvc/spring-mvc.xsd ">    <!--  Automatically scan the package to make the annotations under the specified package effective, which is uniformly managed by the IOC container -- > < context: component scan base package = "com. Th. Controller" / > <-- Let Spring MVC not process static resources -- > < MVC: default servlet handler / > <-- Support MVC annotation driven. In spring, the @ RequestMapping annotation is generally used to complete the mapping relationship. To make the @ RequestMapping annotation effective, you must register DefaultAnnotationHandlerMapping and an AnnotationMethodHandlerAdapter instance in the context. These two instances are processed at the class level and method level respectively. The annotation driven configuration helps us automatically complete the injection of the above two instances. -- >< mvc:annotation-driven />    <!--  View parser -- > < bean class = "org. Springframework. Web. Servlet. View. Internalresourceviewresolver" id = "internalresourceviewresolver" > <-- Prefix -- > < property name = "prefix" value = "/ Web inf / JSP / / > <-- Suffix -- > < property name = "suffix" value = ". JSP" / > < / bean > < / beans > <-- This is also a fixed configuration. Only the package that needs to be injected with IOC needs to be modified -- >

In the view parser, we store all views in the / WEB-INF / directory, which can ensure the view security, because the files in this directory cannot be accessed directly by the client.

  • Make IOC comments effective
  • Static resource filtering: HTML. JS. CSS. Pictures, videos
  • Annotation driven MVC
  • Configure view parser

6. Create Controller

Write a Java control class: com.th.controller.HelloController. Pay attention to the coding specification

package com.th.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/HelloController")public class HelloController {    //When the @ RequestMapping method is registered on the class, it will automatically move down. Actual address: project name / hellocontroller / Hello @ RequestMapping ("/ Hello") public string sayhello (model model) {/ / add the attribute msg and value to the model. You can take out and render the model. AddAttribute ("msg", "Hello, springmvcannotatiom!") in the JSP page ;                //web-inf/jsp/hello.jsp        return "hello";    }}
  • @The Controller is used to automatically scan the Spring IOC container during initialization;
  • @RequestMapping is to map the request path. Here, because there are mappings on classes and methods, the access should be / HelloController/hello;
  • The purpose of declaring Model type parameters in the method is to bring the data in the Action to the view;
  • The result returned by the method is the name of the view Hello, plus the prefix and suffix in the configuration file to become WEB-INF/jsp/hello.jsp.

7. Create view layer

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>   <title>SpringMVC</title></head><body>${msg}</body></html>

Create hello.jsp in the WEB-INF/ jsp directory. The view can directly take out and display the information brought back from the Controller;

The value or object stored in the Model can be retrieved through EL representation;

8. Configure Tomcat run

Configure Tomcat, start the server and access the corresponding request path!

OK, run successfully!

Summary

The implementation steps are actually very simple:

  1. Create a new web project
  2. Import related jar packages
  3. Write web.xml and register DispatcherServlet
  4. Writing spring MVC configuration files
  5. The next step is to create the corresponding control class, controller
  6. Finally, improve the correspondence between the front-end view and the controller
  7. Test run commissioning

Three major components that must be configured to use spring MVC:

Processor mapper, processor adapter, view parser

Usually, we only need to manually configure the view parser, while the processor mapper and processor adapter only need to turn on the annotation driver, eliminating a large section of xml configuration

Let's review the principle~

Now we are going to study the Controller and RestFul style!

4. Controller and RestFul style

In the last section, we wrote our first spring MVC program!

Now let's take a look at the specific contents of the controller and path request!

4.1 Controller

  • The complex controller provides the behavior of accessing the application program, which is usually implemented by interface definition or annotation definition.
  • The controller is responsible for parsing the user's request and transforming it into a model.
  • In Spring MVC, a controller class can contain multiple methods
  • In Spring MVC, there are many ways to configure the Controller

4.2 implementation of Controller interface

Controller is an interface. Under the org.springframework.web.servlet.mvc package, there is only one method in the interface;

//The class implementing the interface obtains the controller function public interface Controller {/ / processes the request and returns a model and view object ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws Exception;}

test

  1. Create a new Moudle, spring mvc-04-controller. Make a copy of 03 just now and let's operate!

    • Delete HelloController
    • mvc's configuration file leaves only the view parser!
  2. Write a Controller class, ControllerTest1

    //Define the Controller. / / Note: do not import the wrong package, implement the Controller interface, and rewrite the method; Public class controllertest1 implements Controller {public modelandview handlerequest (HttpServletRequest, HttpServletRequest, httpservletresponse, httpservletresponse) throws exception {/ / a model view object is returned. Modelandview MV = new modelandview(); mv.addobject ("MSG", "test1Controller"); mv.setviewname ("test") ;       return mv;  }}
    
  3. After writing, register the requested bean in the Spring configuration file; name corresponds to the request path, and class corresponds to the class that handles the request

    <bean name="/t1" class="com.th.controller.ControllerTest1"/>
    
  4. Write the front-end test.jsp. Note that it is written in the WEB-INF/jsp directory, corresponding to our view parser

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>   <title>thshen</title></head><body>${msg}</body></html>
    
  5. Configure Tomcat to run the test. I don't have a project release name here. I configure a /, so the request doesn't need to add a project name. OK!

explain:

  • It is an old method to implement the interface Controller and define the Controller
  • The disadvantages are: there is only one method in a Controller. If you want multiple methods, you need to define multiple controllers; The way of definition is troublesome;

4.3 using annotation @ Controller

  • @The Controller annotation type is used to declare that the instance of the Spring class is a Controller (another three annotations were mentioned when talking about IOC);

  • Spring can use the scanning mechanism to find all annotation based controller classes in the application. In order to ensure that spring can find your controller, you need to declare component scanning in the configuration file.

    <!-- Automatically scan the specified package, and submit all the following annotation classes to IOC Container management --><context:component-scan base-package="com.th.controller"/>
    
  • Add a ControllerTest2 class and implement it with annotations;

    //@The class annotated by the Controller will be automatically added to the Spring context @ Controllerpublic class ControllerTest2 {/ / map the access path @ requestmapping ("/ T2") public string index (Model) {/ / Spring MVC will automatically instantiate a Model object to pass the value model.addAttribute("msg", "ControllerTest2") to the view / / return the view location return "test";}}
    
  • Run tomcat test

It can be found that both of our requests can point to a view, but the results of the page results are different. It can be seen from here that the view is reused, and there is a weak coupling relationship between the controller and the view.

Annotation is the most commonly used method!

4.4 RequestMapping

@RequestMapping

  • @The RequestMapping annotation is used to map URLs to a controller class or a specific handler method. Can be used on classes or methods. Used on a class to indicate that all methods in the class that respond to requests take this address as the parent path.

  • In order to test the conclusion more accurately, we can add a project name to test myweb

  • Annotate only on Methods

    @Controllerpublic class TestController {   @RequestMapping("/h1")   public String test(){       return "test";  }}
    

    Access path: http://localhost:8080 /Project name / h1

  • Annotate classes and methods at the same time

    @Controller@RequestMapping("/admin")public class TestController {   @RequestMapping("/h1")   public String test(){       return "test";  }}
    

    Access path: http://localhost:8080 /Project name / admin /h1, you need to specify the path of the class first, and then the path of the method;

4.5RestFul style

concept

Restful is a style of resource location and resource operation. It's not a standard or agreement, it's just a style. The software designed based on this style can be more concise, more hierarchical, and easier to implement caching and other mechanisms.

function

Resources: all things on the Internet can be abstracted as resources

Resource operation: use POST, DELETE, PUT and GET to operate resources using different methods.

Add, delete, modify and query respectively.

Operate resources in the traditional way: achieve different effects through different parameters! Single method, post and get

​ http://127.0.0.1/item/queryItem.action?id=1 Query, GET

​ http://127.0.0.1/item/saveItem.action New, POST

​ http://127.0.0.1/item/updateItem.action Update, POST

​ http://127.0.0.1/item/deleteItem.action?id=1 Delete, GET or POST

Using RESTful operation resources: different effects can be achieved through different request methods! As follows: the request address is the same, but the function can be different!

​ http://127.0.0.1/item/1 Query, GET

​ http://127.0.0.1/item New, POST

​ http://127.0.0.1/item Update, PUT

​ http://127.0.0.1/item/1 DELETE

Learning test

  1. Create a new class RestFulController

    @Controllerpublic class RestFulController {}
    
  2. In Spring MVC, you can use the @ PathVariable annotation to bind the value of the method parameter to a URI template variable.

    @Controllerpublic class RestFulController {   //Mapping access path @ requestmapping ("/ commit / {P1} / {P2}") public string index (@ pathvariable int P1, @ pathvariable int P2, Model) {int result = P1 + P2; / / spring MVC will automatically instantiate a Model object to pass the value model.addAttribute("msg", "result:" + result) "to the view; / / return the view location return" test " ;         }   }
    
  3. Let's test the request and check it out

  4. Think: what are the benefits of using path variables?

    • Make the path more concise;

    • It is more convenient to obtain parameters, and the framework will automatically perform type conversion.

    • Access parameters can be constrained by the type of path variables. If the types are different, the corresponding request method cannot be accessed. For example, if the access path here is / commit/1/a, the path does not match the method, rather than parameter conversion failure.

  5. Let's modify the corresponding parameter type and test again

    //Mapping access path @ requestmapping ("/ commit / {P1} / {P2}") public string index (@ pathvariable int P1, @ pathvariable string P2, Model) {string result = P1 + P2; / / spring MVC will automatically instantiate a Model object to pass the value model.addAttribute("msg", "result:" + result) "to the view; / / return the view location return" test ";}
    

Use the method property to specify the request type

It is used to constrain the type of request and narrow the request range. Specify the type of request predicate, such as GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE, etc

Let's test:

  • Add a method

    //Mapping access path must be POST request @ requestmapping (value = "/ Hello", method = {requestmethod. POST}) public string index2 (model) {model. AddAttribute ("MSG", "Hello!"); return "test";}
    
  • We use the browser address bar to access. By default, it is a Get request, and an error 405 will be reported:

  • If you change POST to GET, it is normal;

    //The mapping access path must be a get request @ requestmapping (value = "/ Hello", method = {requestmethod. Get}) public string index2 (model) {model. AddAttribute ("MSG", "Hello!"); return "test";}
    

Summary:

The @ RequestMapping annotation of Spring MVC can handle HTTP requests, such as GET, PUT, POST, DELETE and PATCH.

All address bar requests will be of HTTP GET type by default.

Method level annotation variants are as follows: combined annotation

@GetMapping@PostMapping@PutMapping@DeleteMapping@PatchMapping

@GetMapping is a combined annotation, which is often used!

It acts as a shortcut to @ RequestMapping(method =RequestMethod.GET).

4.6 expansion: little yellow duck debugging method

Scenario 1: we all have the experience of asking questions and explaining programming problems to others (or even people who can't program at all), but many times we think of solutions to problems in the process of explanation, and then the other party looks at a loss.

Scene 2: your colleague comes to ask you a question, but when he finishes the question or is halfway there, he comes up with the answer and leaves, leaving you with a blank face.

In fact, the above two scene phenomena are the so-called rubber duck debugging, also known as rubber duck debugging, which is one of the most commonly used debugging methods in our software engineering.

This concept is said to come from a story in the book "the way of programmer cultivation". It is said that the program master carries a little yellow duck with him. When debugging the code, he will put the little yellow duck on the table, explain each line of code to the duck in detail, and then fix the problem quickly.

Now we are ready to study parameter acceptance and result jump!

In the previous section, we learned about controllers and Restful style operations

Now let's take a look at spring MVC parameter receiving processing and result jump processing!

5. Result jump mode

5.1,ModelAndView

Set the ModelAndView object and jump to the specified page according to the name of the view and the view parser

Page: {view parser prefix} + viewName + {view parser suffix}

<!-- view resolver  --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"     id="internalResourceViewResolver">   <!-- prefix -->   <property name="prefix" value="/WEB-INF/jsp/" />   <!-- suffix -->   <property name="suffix" value=".jsp" /></bean>

Corresponding controller class

public class ControllerTest1 implements Controller {   public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {       //Return a model view object modelandview MV = new modelandview(); mv.addObject("msg","ControllerTest1");        mv.setViewName("test");        return mv;  }}

5.2 ServletAPI

By setting the servlet API, no view parser is required

1. Output through HttpServletResponse

2. Redirection via HttpServletResponse

3. Forwarding through HttpServletResponse

@Controllerpublic class ResultGo {   @RequestMapping("/result/t1")   public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {       rsp.getWriter().println("Hello,Spring BY servlet API");  }   @RequestMapping("/result/t2")   public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {       rsp.sendRedirect("/index.jsp");  }   @RequestMapping("/result/t3")   public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {       //Forward req.setattribute ("MSG", "result / T3"); req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);  }}

5.3 SpringMVC

Forward and redirect through spring MVC - no view parser is required;

Before testing, you need to comment out the view parser

  • The default is forward forwarding (you can also add)
  • redirect forwarding needs to be specially added
@Controllerpublic class ResultSpringMVC {   @RequestMapping("/rsm/t1")   public String test1(){       //Forward return "/ index. JSP";}@ Requestmapping ("/ RSM / T2") public string test2() {/ / forward 2 return "forward: / index. JSP";} @ requestmapping ("/ RSM / T3") public string test3() {/ / redirect return "redirect:/index.jsp";}}

Forward and redirect through spring MVC - view parser;

Redirection does not require a view parser. Its essence is to re request a new place, so pay attention to the path problem

You can redirect to another request implementation

  • forward forwarding is the default (cannot be added)
  • redirect forwarding needs to be specially added
@Controllerpublic class ResultSpringMVC2 {   @RequestMapping("/rsm2/t1")   public String test1(){       //Forward return "test";}@ Requestmapping ("/ rsm2 / T2") public string test2() {/ / redirect return "redirect: / index. JSP"; / / return "redirect: Hello. Do"; / / Hello. Do is another request /}}

6. Data processing

6.1. Processing submitted data

1. The parameter names are consistent

Submit data: http://localhost:8080/hello?name=thshen

Treatment method:

@RequestMapping("/hello")public String hello(String name){   System.out.println(name);   return "hello";}

Background output: thshen

2. Inconsistent parameter name

The name of the submitted domain and the processing method

Submit data: http://localhost:8080/hello?username=thshen

Treatment method:

//@Requestparam ("username"): the name of the domain submitted by username. @ requestmapping ("/ Hello") public string hello (@ requestparam ("username") string name) {system.out.println (name); return "hello";}

Background output: thshen

3. Submitted is an object

It is required that the submitted form field and the attribute name of the object are consistent, and the parameter can use the object

1. Entity class

public class User {   private int id;   private String name;   private int age;   //Construct / / get / set / / tostring()}

2. Submit data: http://localhost:8080/mvc04/user?name=thshen&id=1&age=5

3. Treatment method:

@RequestMapping("/user")public String user(User user){   System.out.println(user);   return "hello";}

Background output: user {id = 1, name = 'thshen', age=15}

Note: if an object is used, the parameter name passed by the front end must be consistent with the object name, otherwise it is null.

6.2 data display to front end

First: through ModelAndView

We've always been like this before. There's no more explanation

public class ControllerTest1 implements Controller {   public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {       //Return a model view object modelandview MV = new modelandview(); mv.addObject("msg","ControllerTest1");        mv.setViewName("test");        return mv;  }}

The second is through ModelMap

ModelMap

@RequestMapping("/hello")public String hello(@RequestParam("username") String name, ModelMap model){   //Encapsulate the data to be displayed in the view / / equivalent to req.setattribute ("name", name); model.addAttribute("name",name);    System.out.println(name);    return "hello";}

Third: through Model

Model

@RequestMapping("/ct2/hello")public String hello(@RequestParam("username") String name, Model model){   //Encapsulate the data to be displayed in the view / / equivalent to req.setattribute ("name", name); model.addAttribute("msg",name);    System.out.println(name);    return "test";}

6.3 comparison

For novices, the simple difference is:

Model There are only a few methods that are only suitable for storing data, which simplifies novices' understanding of data Model Operation and understanding of objects; ModelMap Inherited LinkedMap ,In addition to implementing some of its own methods, the same inheritance LinkedMap Methods and characteristics of; ModelAndView While storing data, you can set the returned logical view to control the jump of the display layer.

Of course, more future development is more about performance and optimization, so it can't be limited to this understanding.

Please use 80% of your time to lay a solid foundation, the remaining 18% to study the framework and 2% to learn some English. The official document of the framework is always the best tutorial.

7. Garbled code problem

Test steps:

1. We can write a submission form on the home page

<form action="/e/t" method="post"> <input type="text" name="name"> <input type="submit"></form>

2. Write the corresponding processing class in the background

@Controllerpublic class Encoding {   @RequestMapping("/e/t")   public String test(Model model,String name){       model.addAttribute("msg",name); //Get the value submitted by the form return "test"// Jump to the test page and display the entered value}}

3. Input Chinese test, found garbled code

It has to be said that the problem of garbled code is very common in our development, and it is also a big problem for our program ape!

In the past, the problem of garbled code was solved through filters, and spring MVC provided us with a filter that can be configured in web.xml

The xml file has been modified. You need to restart the server!

<filter>   <filter-name>encoding</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></filter><filter-mapping>   <filter-name>encoding</filter-name>   <url-pattern>/*</url-pattern></filter-mapping>

However, we found that in some extreme cases, this filter does not support get well

Treatment method:

1. Modify tomcat configuration file: set code!

<Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1"          connectionTimeout="20000"          redirectPort="8443" />

Random code universal filtering

Customize the filter and configure it in web.xml

package com.th.filter;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Map;/*** Filter to solve all the garbled codes of get and post requests*/public class GenericEncodingFilter implements Filter {   @Override   public void destroy() {  }   @Override   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {       //Character encoding of processing response httpservletresponse myresponse = (httpservletresponse) response; myResponse.setContentType("text/html;charset=UTF-8");       //  Transform to protocol related object HttpServletRequest httpServletRequest = (HttpServletRequest) request// Enhance the request packaging HttpServletRequest myrequest = new myrequest (HttpServletRequest); chain.doFilter(myrequest, response);  }   @ Override public void init (filterconfig filterconfig) throws ServletException {} / / custom request object, HttpServletRequest wrapper class myrequest extensions httpservletrequestwrapper {private HttpServletRequest request; / / whether to encode the tag private boolean hasEncode; / / define a constructor that can be passed into the HttpServletRequest object to decorate it. Public myrequest (HttpServletRequest) {super (request); / / super must write this.request = request;} //Overwrite the method to be enhanced @ override public map getparametermap() {/ / obtain the request method first string method = request. Getmethod(); if (method. Equalsignorecase ("post")) {/ / post request try {/ / process post garbled request.setcharacterencoding ("UTF-8") ; return request. Getparametermap();} catch (UnsupportedEncodingException E) {e.printstacktrace();}} else if (method. Equalsignorecase ("get") {/ / get request map < string, string [] > parametermap = request. Getparametermap(); if (! Hasencode) {/ / ensure that the get manual encoding logic runs only once for (string parametername: parametermap. Keyset()) {string [] values = parametermap.get (parametername); if (values! = null) {for (int i = 0; I < values. Length; I + +) {try {/ / processing get garbled values [i] = new string (values [i]. GetBytes ("iso-8859-1"), "UTF-8");} catch (UnsupportedEncodingException E) {e.printstacktrace();} }}} hasencode = true;} return parametermap;} return super. Getparametermap();} / / take a value @ override public string getparameter (string name) {map < string, string [] > parametermap = getparametermap(); string []] Values = parametermap.get (name); if (values = = null) {return null;} return values [0]; / / retrieve the first value of the parameter} / / get all values @ override public string [] getparametervalues (string name) {map < string, string [] > parametermap = getparametermap(); string [] values = parametermap.get (name) ;       return values;  }}

This is also written by some great gods I found on the Internet. Generally, the default garbled code processing of spring MVC can be well solved!

Then configure this filter in web.xml!

The problem of garbled code needs more attention at ordinary times. The unified coding UTF-8 should be set wherever possible!

With this knowledge, we can integrate SSM immediately!

8. Jason interactive processing (Jackson and fastjson)

8.1. What is JSON?

  • JSON (JavaScript object notation) is a lightweight data exchange format, which is widely used at present.
  • Data is stored and represented in a text format completely independent of the programming language.
  • The concise and clear hierarchy makes JSON an ideal data exchange language.
  • It is easy for people to read and write, but also easy for machine analysis and generation, and effectively improves the network transmission efficiency.

In the JavaScript language, everything is an object. Therefore, any type supported by JavaScript can be represented by JSON, such as string, number, object, array, etc. see its requirements and syntax format:

  • Objects are represented as key value pairs, and data is separated by commas
  • Curly braces save objects
  • Square brackets hold the array

JSON key value pairs are a way to save JavaScript objects, and the writing method is similar to that of JavaScript objects. The key name in the key / value pair combination is written in front and wrapped in double quotation marks "", separated by colon: and then followed by the value:

{"name": "QinJiang"}{"age": "3"}{"sex": "male"}

Many people don't know the relationship between JSON and JavaScript objects, or even who they are. In fact, it can be understood as follows:

JSON is a string representation of JavaScript objects. It uses text to represent the information of a JS object, which is essentially a string.

var obj = {a: 'Hello', b: 'World'}; //This is an object. Note that the key name can also be wrapped in quotation marks. var json = '{"a": "Hello", "b": "World"}'; / / this is a JSON string, which is essentially a string

JSON and JavaScript objects interoperate

To convert from a JSON string to a JavaScript object, use the JSON.parse() method:

var obj = JSON.parse('{"a": "Hello", "b": "World"}');//The result is {a: 'Hello', b: 'World'}

To convert from a JavaScript object to a JSON string, use the JSON.stringify() method:

var json = JSON.stringify({a: 'Hello', b: 'World'});//The result is' {"a": "Hello", "b": "World"} '

Code test

1. Create a new module, spring mvc-05-json, and add web support

2. Create a new json-1.html in the web directory and write the test content

<!DOCTYPE html><html lang="en"><head>   <meta charset="UTF-8">   <title>JSON_Qin Jiang</title></head><body><script type="text/javascript">   //Write a js object var user = {Name: "Qinjiang", age: 3, sex: "male"}; / / convert the js object to JSON string var STR = json.stringify (user); console.log (STR); / / convert the JSON string to js object var user2 = json.parse (STR); console.log (user2.age, user2.name, user2. Sex); < / script ></body></html>

3. In IDEA, open it with a browser and view the console output!

JSON data returned by Controller

Jackson should be a better json parsing tool at present

Of course, there are more than one tool, such as Alibaba's fastjson and so on.

We use Jackson here. To use Jackson, we need to import its jar package;

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --><dependency>   <groupId>com.fasterxml.jackson.core</groupId>   <artifactId>jackson-databind</artifactId>   <version>2.9.8</version></dependency>

Configuration required to configure spring MVC

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 ">   <!-- 1. Register servlet -- > < servlet > < servlet name > spring MVC < / servlet name > < servlet class > org. Springframework. Web. Servlet. Dispatcher servlet < / servlet class > <-- Specify the location of the spring MVC configuration file through the initialization parameters to associate -- > < init param > < param name > contextconfiglocation < / param name > < param value > classpath: springmvc servlet. XML < / param value > < / init param > <-- Startup sequence: the smaller the number, the earlier the startup -- > < load on startup > 1 < / load on startup > < / servlet > <-- All requests will be intercepted by springmvc -- > < servlet mapping > < servlet name > springmvc < / servlet name > < URL pattern > / < / url pattern > < / servlet mapping > < Filter > < filter name > encoding < / filter name > < filter class > org.springframework.web.filter.characterencoding filter < / filter class > < init param > < param name > enc oding</param-name>           <param-value>utf-8</param-value>       </init-param>   </filter>   <filter-mapping>       <filter-name>encoding</filter-name>       <url-pattern>/</url-pattern>   </filter-mapping></web-app>

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "      xmlns:context=" http://www.springframework.org/schema/context "      xmlns:mvc=" http://www.springframework.org/schema/mvc "      xsi:schemaLocation=" http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        https://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/mvc        https://www.springframework.org/schema/mvc/spring-mvc.xsd ">   <!--  Automatically scan the specified package, and submit all the following annotation classes to IOC container management -- > < context: component scan base package = "com. Th. Controller" / > <-- View parser -- > < bean class = "org. Springframework. Web. Servlet. View. Internalresourceviewresolver" id = "internalresourceviewresolver" > <-- Prefix -- > < property name = "prefix" value = "/ Web inf / JSP / / > <-- Suffix -- > < property name = "suffix" value = ". JSP" / > < / bean > < / beans >

We randomly write a User entity class, and then we write our test Controller;

package com.th.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;//Import required lombok@Data @ AllArgsConstructor@NoArgsConstructorpublic  class User {   private String name;   private int age;   private String sex;   }

Here we need two new things, one is @ ResponseBody and the other is ObjectMapper object. Let's see the specific usage

Write a Controller;

@Controllerpublic class UserController {   @RequestMapping("/json1")   @ResponseBody   public String json1() throws JsonProcessingException {       //Create an object mapper of jackson to parse data objectmapper = new objectmapper()// Create an object user = new user ("Qinjiang No. 1", 3, "male")// Parse our object into json format String str = mapper.writeValueAsString(user)// Due to the @ ResponseBody annotation, STR will be converted to json format and returned here; Very convenient return str;}}

Configure Tomcat and start the test!

http://localhost:8080/json1

It is found that there is a garbled code problem. We need to set its encoding format to utf-8 and its return type;

This is implemented through the @ requestmapping products attribute. Modify the following code

//Products: specify the response body return type and code @ requestmapping (value = "/ json1", products = "application / JSON; charset = UTF-8")

Test again, http://localhost:8080/json1 , garbled code problem OK!

[Note: remember to deal with garbled code when using json]

8.2 garbled code (solution)

Unified solution of garbled code

The previous method is troublesome. If there are many requests in the project, each one should be added, which can be specified uniformly through Spring configuration, so you don't have to deal with them every time!

We can add a message StringHttpMessageConverter conversion configuration to the configuration file of spring MVC!

<mvc:annotation-driven>   <mvc:message-converters register-defaults="true">       <bean class="org.springframework.http.converter.StringHttpMessageConverter">           <constructor-arg value="UTF-8"/>       </bean>       <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">           <property name="objectMapper">               <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">                   <property name="failOnEmptyBeans" value="false"/>               </bean>           </property>       </bean>   </mvc:message-converters></mvc:annotation-driven>

Unified solution for returning json strings

Use @ RestController directly on the class. In this way, all the methods in it will only return json strings. There is no need to add @ ResponseBody to each one! We generally use @ RestController in front and back-end separation development, which is very convenient!

@RestControllerpublic class UserController {   //Products: specify the return type and code of the response body @ requestmapping (value = "/ json1") public string json1() throws jsonprocessingexception {/ / create a jackson object mapper to parse the data ObjectMapper mapper = new ObjectMapper(); / / create an object user = new user ("Qinjiang No. 1", 3, "male") ; / / parse our object into JSON format String str = mapper.writeValueAsString(user); / / due to the @ ResponseBody annotation, STR will be converted into JSON format and returned here; very convenient return str;}}

Start the tomcat test, and the results are output normally!

8.3 test set output

Add a new method

@RequestMapping("/json2")public String json2() throws JsonProcessingException {   //Create an object mapper of jackson to parse data objectmapper = new objectmapper()// Create an object User user1 = new User("Qinjiang No. 1", 3, "male"); User user2 = new User("Qinjiang No. 2", 3, "male"); User user3 = new User("Qinjiang No. 3", 3, "male"); User user4 = new User("Qinjiang No. 4", 3, "male"); List<User> list = new ArrayList<User>();    list.add(user1);    list.add(user2);    list.add(user3);    list.add(user4);   // Parse our object into json format string STR = mapper.writevalueasstring (list); return str;}

Running result: perfect, no problem!

8.4 output time object

Add a new method

@RequestMapping("/json3")public String json3() throws JsonProcessingException {   ObjectMapper mapper = new ObjectMapper();   //Create a time object, Java. Util. Date = new date()// Parse our object into json format string STR = mapper.writevalueasstring (date); return str;}

Operation results:

  • The default date format will become a number, which is the number of milliseconds from January 1, 1970 to the current date!
  • Jackson will convert the time into timestamps by default

Solution: cancel the timestamps format and customize the time format

@RequestMapping("/json4")public String json4() throws JsonProcessingException {   ObjectMapper mapper = new ObjectMapper();   //Mapper.configure (serializationfeature. Write_dates_as_timestamp, false) without timestamp// Custom date format object simpledateformat SDF = new simpledateformat ("yyyy MM DD HH: mm: SS")// Specify the date format mapper. SetDateFormat (SDF); Date date = new Date();    String str = mapper.writeValueAsString(date);    return str;}

Running result: the time was successfully output!

8.5 extraction as tools

If you want to use it frequently, it is troublesome. We can encapsulate these codes into a tool class; Let's write it

package com.th.utils;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import java.text.SimpleDateFormat;public class JsonUtils {      public static String getJson(Object object) {       return getJson(object,"yyyy-MM-dd HH:mm:ss");  }   public static String getJson(Object object,String dateFormat) {       ObjectMapper mapper = new ObjectMapper();       //mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) without using time difference// User defined date format object SimpleDateFormat sdf = new SimpleDateFormat(dateFormat)// Specify the date format mapper. SetDateFormat (SDF); try {           return mapper.writeValueAsString(object);      } catch (JsonProcessingException e) {           e.printStackTrace();      }       return null;  }}

When we use tool classes, the code is more concise!

@RequestMapping("/json5")public String json5() throws JsonProcessingException {   Date date = new Date();   String json = JsonUtils.getJson(date);   return json;}

be accomplished! Perfect!

8.6 fastjason (better)

fastjson.jar is a package developed by Alibaba for Java development. It can easily realize the conversion between json objects and JavaBean objects, between JavaBean objects and json strings, and between json objects and json strings. There are many conversion methods to implement json, and the final implementation results are the same.

fastjson's pom dependency!

<dependency>   <groupId>com.alibaba</groupId>   <artifactId>fastjson</artifactId>   <version>1.2.60</version></dependency>

fastjson has three main classes:

JSONObject represents a json object

  • JSONObject implements the Map interface. I guess the underlying operation of JSONObject is implemented by Map.
  • JSONObject corresponds to the json object. You can obtain the data in the json object through various forms of get() methods. You can also use methods such as size(), isEmpty() to obtain the number of "key: value" pairs and judge whether they are empty. Its essence is accomplished by implementing the Map interface and calling the methods in the interface.

JSONArray represents an array of json objects

  • Internally, there are methods in the List interface to complete the operation.

JSON represents the conversion of JSONObject and JSONArray

  • Analysis and use of JSON class source code
  • Carefully observe these methods, mainly to realize the mutual transformation between json objects, json object arrays, javabean objects and json strings.

For code testing, we create a new FastJsonDemo class

package com.th.controller;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.th.pojo.User;import java.util.ArrayList;import java.util.List;public class FastJsonDemo {   public static void main(String[] args) {       //Create an object User user1 = new User("Qinjiang No. 1", 3, "male"); User user2 = new User("Qinjiang No. 2", 3, "male"); User user3 = new User("Qinjiang No. 3", 3, "male"); User user4 = new User("Qinjiang No. 4", 3, "male"); List<User> list = new ArrayList<User>();        list.add(user1);        list.add(user2);        list.add(user3);        list.add(user4);        System. Out. Println ("******* Java object to JSON string ******"); String str1 = JSON.toJSONString(list);        System.out.println("JSON.toJSONString(list)==>"+str1);        String str2 = JSON.toJSONString(user1);        System.out.println("JSON.toJSONString(user1)==>"+str2);        System. Out. Println ("\ n ****** JSON string to Java object *******"); User jp_ user1=JSON.parseObject(str2,User.class);        System.out.println("JSON.parseObject(str2,User.class)==>"+jp_user1);        System. Out. Println ("\ n ******* Java object to JSON object *****************************************************; JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);        System.out.println("(JSONObject) JSON.toJSON(user2)==>"+jsonObject1.getString("name"));        System. Out. Println ("\ n ******** JSON object to Java object ****************************************************; User to_ java_ user = JSON.toJavaObject(jsonObject1, User.class);        System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user);  }}

We only need to master the use of this tool class, and find the corresponding implementation according to the specific business when using it. Like the previous commons IO toolkit, just use it!

Jason is very important in our data transmission. We must learn to use it!

9. Ajax research

9.1 introduction

  • AJAX = Asynchronous JavaScript and XML.
  • AJAX is a technology that can update some web pages without reloading the whole web page.
  • Ajax is not a new programming language, but a technology for creating better, faster and more interactive Web applications.
  • In 2005, Google made AJAX popular through its Google suggest. Google Suggest can automatically help you search for words.
  • Google Suggest uses AJAX to create a highly dynamic web interface: when you enter keywords in Google's search box, JavaScript will send these characters to the server, and then the server will return a list of search suggestions.
  • Just like the domestic Baidu search box!
  • Traditional web pages (that is, web pages without ajax Technology) need to reload the whole web page if they want to update the content or submit a form.
  • Web pages using ajax technology can realize asynchronous local update through a small amount of data exchange in the background server.
  • With Ajax, users can create a direct, highly available, richer and more dynamic Web user interface close to local desktop applications.

9.2. Fake Ajax

We can use a tag on the front end to fake an ajax look. iframe tag

1. Create a new module: sspring mvc-06-ajax and import web support!

2. Write an ajax-frame.html, use iframe test, and feel the effect

<!DOCTYPE html><html><head lang="en">   <meta charset="UTF-8">   <title>thshen</title></head><body><script type="text/javascript">   window.onload = function(){       var myDate = new Date();       document.getElementById('currentTime').innerText = myDate.getTime();  };   function LoadPage(){       var targetUrl =  document.getElementById('url').value;       console.log(targetUrl);       document.getElementById("iframePosition").src = targetUrl;  }</script><div>   <p>Please enter the address to load:<span id="currentTime"></span></p>   <p>       <input id="url" type="text" value="https://Www.baidu. COM / "/ > < input type =" button "value =" submit "onclick =" loadpage() "> < / P > < / div > < div > < H3 > Load page location: < / H3 > < iframe id =" iframeposition "style =" width: 100%; height: 500px; "></iframe></div></body></html>

3. Use IDEA to open the browser to test!

With AJAX, you can:

  • When registering, enter the user name to automatically detect whether the user already exists.
  • When logging in, you will be prompted that the user name and password are incorrect
  • When deleting a data row, the row ID is sent to the background, and the background deletes it in the database. After the database is deleted successfully, the data row is also deleted in the page DOM.
  • ... wait

9.3 jQuery.ajax

We will not explain the pure JS native implementation of Ajax here, but directly use the one provided by jquery to facilitate learning and use and avoid repeated wheel building. Interested students can learn about JS native XMLHttpRequest!

The core of Ajax is the XMLHttpRequest object (XHR). XHR provides an interface for sending requests to the server and parsing server responses. The ability to obtain new data from the server asynchronously.

jQuery provides several AJAX related methods.

Through the jQuery AJAX method, you can use HTTP Get and HTTP Post to request text, HTML, XML or JSON from a remote server - and you can load these external data directly into the selected elements of the web page.

jQuery is not a producer, but a nature porter.

The essence of jQuery Ajax is XMLHttpRequest, which is encapsulated and easy to call!

jQuery.ajax(...)      Some parameters:            url: Request address            type: Request method, GET,POST(1.9.0 Later use method)        headers: Request header            data: Data to send    contentType: The content encoding type of the message to be sent to the server(default: "application/x-www-form-urlencoded; charset=UTF-8")          async: Asynchronous        timeout: Set request timeout (MS)      beforeSend: Function executed before sending the request(overall situation)        complete: Callback function executed after completion(overall situation)        success: Callback function executed after success(overall situation)          error: Callback function executed after failure(overall situation)        accepts: Send the request to the server and tell the server the data type acceptable to the current client        dataType: Converts the data returned by the server to the specified type          "xml": Convert the content returned by the server into xml format          "text": Convert the content returned by the server to normal text format          "html": Convert the content returned by the server into normal text format and insert DOM If it contains JavaScript Tag, it will try to execute.        "script": Try to treat the return value as JavaScript To execute, and then convert the content returned by the server into normal text format          "json": Convert the content returned by the server into the corresponding JavaScript object        "jsonp": JSONP Format use JSONP When calling a function as "myurl?callback=?" jQuery Will automatically replace ? Give the correct function name to execute the callback function

Let's make a simple test, using the most primitive HttpServletResponse processing, which is the simplest and most common

1. Configure the configuration files of web.xml and spring MVC, and copy the above case [remember static resource filtering and annotation driven configuration]

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "      xmlns:context=" http://www.springframework.org/schema/context "      xmlns:mvc=" http://www.springframework.org/schema/mvc "      xsi:schemaLocation=" http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        https://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/mvc        https://www.springframework.org/schema/mvc/spring-mvc.xsd ">   <!--  Automatically scan the specified package, and submit all the following annotation classes to IOC container management -- > < context: component scan base package = "com. Th. Controller" / > < MVC: default servlet handler / > < MVC: annotation driven / > <-- View parser -- > < bean class = "org. Springframework. Web. Servlet. View. Internalresourceviewresolver" id = "internalresourceviewresolver" > <-- Prefix -- > < property name = "prefix" value = "/ Web inf / JSP / / > <-- Suffix -- > < property name = "suffix" value = ". JSP" / > < / bean > < / beans >

2. Write an Ajax controller

@Controllerpublic class AjaxController {   @RequestMapping("/a1")   public void ajax1(String name , HttpServletResponse response) throws IOException {       if ("admin".equals(name)){           response.getWriter().print("true");      }else{           response.getWriter().print("false");      }  }}

3. To import jquery, you can use the online CDN or download the import

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script><script src="${pageContext.request.contextPath}/statics/js/jquery-3.1.1.min.js"></script>

4. Write index.jsp test

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html> <head>   <title>$Title$</title>  <%--<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>--%>   <script src="${pageContext.request.contextPath}/statics/js/jquery-3.1.1.min.js"></script>   <script>       function a1(){           $.post({               url:"${pageContext.request.contextPath}/a1",               data:{'name':$("#txtName").val()},               success:function (data,status)  {alert (data); alert (status);}});} < / script > < / head > < body > <% -- onblur: loss of focus trigger event --% > User Name: < input type = "text" id = "txtname" onblur = "a1()" / ></body></html>

5. Start the tomcat test! Open the browser console. When our mouse leaves the input box, we can see that an ajax request has been issued! It is the result returned to us in the background! The test is successful!

9.4 spring MVC implementation

Entity class user

@Data@AllArgsConstructor@NoArgsConstructorpublic class User {   private String name;   private int age;   private String sex;}

Let's get a collection object and show it to the front page

@RequestMapping("/a2")public List<User> ajax2(){   List<User> list = new ArrayList<User>();   list.add(new User("Qinjiang 1",3,"male"));   list.add(new User("Qinjiang 2",3,"male"));   list.add(new User("Qinjiang 3",3,"male"));   return list; //Due to the @ RestController annotation, convert the list into json format and return}

Front page

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Title</title>    <script src="${pageContext.request.contextPath}/static/js/jquery-3.4.1.js"></script>    <script>        $(function () {            $("#btn").click(function () {                // console.log('click event succeeded ') / / $. post(url,param, [can be omitted], success) $. Post ("${pagecontext. Request. Contextpath} / A2", function (data) {console.log (data [0]. Name); let HTML = ""; for (let I = 0; I < data. Length; I + +) {                        html += `<tr>                                    <td>${"${data[i].name}"}</td>                                    <td>${"${data[i].age}"}</td>                                    <td>${"${data[i].sex}"}</td>                                </tr>`                    }                    $("#content").html(html) Console. Log (HTML)})}) < / script > < / head > < body > < input type = "button" value = "load data" id = "BTN" > < Table > < thead > < tr > < td > name < / td > < td > age < / TD > < td > gender < / td > < / TR > < / thead > < tbody id = "content "> <% -- data: Background --% > < / tbody > < / Table ></body></html>

Successfully realized the data echo! You can experience the benefits of Ajax!

9.5 registration prompt effect

Let's test a small Demo and think about how to do the real-time prompt behind the input box when we usually register; How to optimize

Let's write a Controller

@RequestMapping("/a3")public String ajax3(String name,String pwd){   String msg = "";   //There is data in the simulation database if (name! = null) {if ("admin". Equals (name)) {MSG = "OK";} else {MSG = "user name input error";}} if (PWD! = null) {if ("123456". Equals (PWD)) {MSG = "OK";} else {MSG = "password input error";}} return msg// Due to the @ RestController annotation, convert msg to json format and return}

Front end page login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>   <title>ajax</title>   <script src="${pageContext.request.contextPath}/statics/js/jquery-3.1.1.min.js"></script>   <script>       function a1(){           $.post({               url:"${pageContext.request.contextPath}/a3",               data:{'name':$("#name").val()},               success:function (data) {                   if (data.toString()=='OK'){                       $("#userInfo").css("color","green");                  }else {                       $("#userInfo").css("color","red");                  }                   $("#userInfo").html(data);              }          });      }       function a2(){           $.post({               url:"${pageContext.request.contextPath}/a3",               data:{'pwd':$("#pwd").val()},               success:function (data) {                   if (data.toString()=='OK'){                       $("#pwdInfo").css("color","green");                  }else {                       $("#pwdInfo").css("color","red");                  }                   $("#Pwdinfo "). HTML (data);}});} < / script > < / head > < body > < p > User Name: < input type =" text "id =" name "onblur =" a1() "/ > < span id =" userinfo "> < / span > < / P > < p > password: < input type =" text "id =" PWD "onblur =" a2() "/ > < span id =" pwdinfo "> < / span ></p></body></html>

[remember to deal with json garbled code]

<!--JSON Garbled problem configuration-->    <mvc:annotation-driven>        <mvc:message-converters register-defaults="true">            <bean class="org.springframework.http.converter.StringHttpMessageConverter">                <constructor-arg value="UTF-8"/>            </bean>            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                <property name="objectMapper">                    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">                        <property name="failOnEmptyBeans" value="false"/>                    </bean>                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>

Test the effect, dynamic request response, local refresh, that's it!

9.6. Get baidu interface Demo

JSONP.html

<!DOCTYPE HTML><html><head>   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">   <title>JSONP Baidu search</title>   <style>       #q{           width: 500px;           height: 30px;           border:1px solid #ddd;           line-height: 30px;           display: block;           margin: 0 auto;           padding: 0 10px;           font-size: 14px;      }       #ul{           width: 520px;           list-style: none;           margin: 0 auto;           padding: 0;           border:1px solid #ddd;           margin-top: -1px;           display: none;      }       #ul li{           line-height: 30px;           padding: 0 10px;      }       #ul li:hover{           background-color: #f60;           color: #fff;      }   </ Style > < script > / / 2. Step 2 / / define demo function (analyze interface and data) function demo (data) {var UL = document. Getelementbyid ('ul '); VAR HTML =' '; / / if the search data exists, add the content if (data.s.length) {/ / the hidden UL is displayed. Ul.style.display = 'block'; / / the searched data loop is appended to Li for (VaR I = 0; I < data. S.length; I + +) {HTML + = '< li >' + data. S [i] + '< / Li >';} / / the looped Li is written to UL. InnerHTML = HTML;}} //1. Step 1 window.onload = function() {/ / get the input box and UL var q = document. Getelementbyid ('q '); VAR UL = document. Getelementbyid ('ul'); / / event Q.onkeyup = function() {/ / if the input box is not equal to empty if (this.value! = '') {/ /☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ / / create tag var script = document.createElement('script ') ; / / assign the cross domain address to src. / / here is the cross domain address to be requested. I wrote the cross domain address of Baidu search script. src = ' https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd= '+ this. Value +' & CB = demo '; / / append the combined script tag with src to the body              document.body.appendChild(script);              }          }      }   </script></head><body><input type="text" id="q" /><ul id="ul"></ul></body></html>

test

Ajax is very important in our development. We must learn to use it!

10. Interceptor

10.1 overview

Spring MVC's processor interceptor is similar to the Filter in Servlet development, which is used to preprocess and post process the processor. Developers can define some interceptors to implement specific functions.

**Difference between filter and Interceptor: * * interceptor is a specific application of AOP idea.

filter

  • Part of the servlet specification that can be used by any java web project
  • After / * is configured in URL pattern, all resources to be accessed can be intercepted

Interceptor

  • The interceptor is the spring MVC framework's own. It can only be used by projects that use the spring MVC framework
  • The interceptor will only intercept the accessed controller methods. If the accessed is jsp/html/css/image/js, it will not intercept

10.2 custom interceptors

How to implement the interceptor?

To customize the interceptor, you must implement the HandlerInterceptor interface.

1. Create a Moudule, springmvc-07-Interceptor, and add web support

2. Configure the web.xml and springmvc-servlet.xml files

3. Write an interceptor

package com.th.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyInterceptor implements HandlerInterceptor {   //Execute before the request processing method. / / if true is returned, execute the next interceptor. / / if false is returned, do not execute the next interceptor public Boolean prehandle (HttpServletRequest, HttpServletRequest, httpservletresponse, object o) throws exception {system. Out. Println ("------------ before processing ------------") ; return true;} / / after the request processing method is executed, execute public void posthandle (HttpServletRequest, HttpServletRequest, httpservletresponse, httpservletresponse, object o, modelandview, modelandview) throws exception {system.out.println ("------------ after processing ----------------");} //Execute after dispatcherServlet processing and clean up. Public void aftercompletion (HttpServletRequest, HttpServletRequest, httpservletresponse, httpservletresponse, object o, exception E) throws exception {system.out.println ("------------ cleaning ----------------");}}

4. Configure the interceptor in the configuration file of spring MVC

<!--About interceptor configuration--><mvc:interceptors>   <mvc:interceptor>       <!--/** Include paths and their sub paths-->       <!--/admin/* Intercepting is/admin/add Wait, this , /admin/add/user Will not be intercepted-->       <!--/admin/** Intercepting is/admin/All under-->       <mvc:mapping path="/**"/>       <!--bean The interceptor is configured-->       <bean class="com.th.interceptor.MyInterceptor"/>   </mvc:interceptor></mvc:interceptors>

5. Write a Controller to receive requests

package com.th.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;//Test the interceptor's controller @ controllerpublic class interceptorcontroller {@ requestmapping ("/ interceptor") @ ResponseBody public string testfunction() {system.out.println ("the method in the controller is executed"); return "hello";}}

6. Front end index.jsp

<a href="${pageContext.request.contextPath}/interceptor">Interceptor test</a>

7. Start tomcat test!

10.3 verify whether the user logs in (authenticated user)

Realization idea

1. There is a login page. You need to write a controller access page.

2. The login page has an action to submit a form. It needs to be processed in the controller. Judge whether the user name and password are correct. If they are correct, write the user information to the session. The login success is returned.

3. Intercept the user's request and judge whether the user logs in. If the user has logged in. Release. If the user has not logged in, jump to the login page

Test:

1. Write a login page login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>   <title>Title</title></head><h1>Login page</h1><hr><body><form action="${pageContext.request.contextPath}/user/login">  user name:<input type="text" name="username"> <br>  password:<input type="password" name="pwd"> <br>   <input type="submit" value="Submit"></form></body></html>

2. Write a Controller to process the request

package com.th.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpSession;@Controller@RequestMapping("/user")public class UserController {   //Jump to the login page @ requestmapping ("/ jumplogin") public string jumplogin() throws exception {return "login";} / / jump to the success page @ requestmapping ("/ jumpsuccess") public string jumpsuccess() throws exception {return "success";} / / submit @ requestmapping ("/ login") public string login (httpsession, string username, string PWD) throws exception {/ / record the user identity information to the session. System.out.println("receive front end = = =" + username); session. SetAttribute ("user", username); return "success";} / / log out @ requestmapping ("logout") public string logout (httpsession) throws exception {/ / session expires session. Invalidate(); return "login";}}

3. Write a successful login page success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>   <title>Title</title></head><body><h1>Login success page</h1><hr>${user}<a href="${pageContext.request.contextPath}/user/logout">cancellation</a></body></html>

4. Test jump on the index page! Start Tomcat test, and you can enter the home page without logging in!

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html> <head>   <title>$Title$</title> </head> <body> <h1>index</h1> <hr><%--Sign in--%> <a href="${pageContext.request.contextPath}/user/jumplogin">Sign in</a> <a href="${pageContext.request.contextPath}/user/jumpSuccess">Success page</a> </body></html>

5. Write user login interceptor

package com.th.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;public class LoginInterceptor implements HandlerInterceptor {   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {       // If it is a login page, release system. Out. Println ("URI:" + request. Getrequesturi()); if (request.getRequestURI().contains("login")) {           return true;      }       HttpSession session = request.getSession();       //  If (session. Getattribute ("user")! = null) {return true;} / / if the user has logged in, jump to the login page request.getrequestdispatcher ("/ WEB-INF / JSP / login. JSP"). Forward (request, response); return false;  }    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {  }      public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {  }}

6. Register the interceptor in the spring MVC configuration file

<!--About interceptor configuration--><mvc:interceptors>   <mvc:interceptor>       <mvc:mapping path="/**"/>       <bean id="loginInterceptor" class="com.th.interceptor.LoginInterceptor"/>   </mvc:interceptor></mvc:interceptors>

7. Restart Tomcat test again!

OK, test the login interception function

OK, test the login interception function

11. File upload and download

11.1 preparation

File upload is one of the most common functions in project development. Spring MVC can well support file upload, but MultipartResolver is not installed in the spring MVC context by default, so it cannot handle file upload by default. If you want to use spring's file upload function, you need to configure MultipartResolver in the context.

***Front end form requirements: in order to upload files, the method of the form must be set to POST and the enctype must be set to multipart / form data*** Only in this case, the browser will send the file selected by the user to the server as binary data;

Give a detailed description of the enctype attribute in the form:

  • Application / x-www = form urlencoded: by default, only the value attribute value in the form field is processed. Forms with this encoding method will process the value in the form field into URL encoding.
  • Multipart / form data: this encoding method will process form data in the form of binary stream. This encoding method will also encapsulate the contents of the file specified in the file field into the request parameters, and will not encode characters.
  • text/plain: except for converting spaces into "+" signs, other characters are not encoded. This method is applicable to sending mail directly through forms.
<form action="" enctype="multipart/form-data" method="post">   <input type="file" name="file"/>   <input type="submit"></form>

Once the enctype is set to multipart / form data, the browser will process the form data in the form of binary stream, and the processing of file upload involves parsing the original HTTP response on the server side. In 2003, the Apache Software Foundation released the open source Commons FileUpload component, which soon became the best choice for Servlet/JSP programmers to upload files.

  • The Servlet 3.0 specification already provides methods to handle file uploads, but such uploads need to be done in the Servlet.
  • Spring MVC provides a simpler encapsulation.
  • Spring MVC provides direct support for file upload, which is implemented with the plug and play MultipartResolver.
  • Spring MVC implements a MultipartResolver implementation class using Apache Commons FileUpload Technology:
  • CommonsMultipartResolver. Therefore, the file upload of spring MVC also depends on the component of Apache Commons FileUpload.

11.2 file upload

1. Import the jar package uploaded by the file, commons file upload, and Maven will automatically help us import his dependent package commons IO package;

<!--File upload--><dependency>   <groupId>commons-fileupload</groupId>   <artifactId>commons-fileupload</artifactId>   <version>1.3.3</version></dependency><!--servlet-api Import a later version of--><dependency>   <groupId>javax.servlet</groupId>   <artifactId>javax.servlet-api</artifactId>   <version>4.0.1</version></dependency>

2. Configuration bean: multipartResolver

[note!!! The id of this bena must be: multipartResolver, otherwise an error of 400 will be reported when uploading the file! I've planted a pit here, and I'll teach you a lesson!]

<!--File upload configuration--><bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">   <!-- The encoding format of the request must be and jSP of pageEncoding Property is consistent so that the contents of the form can be read correctly. The default is ISO-8859-1 -->   <property name="defaultEncoding" value="utf-8"/>   <!-- Maximum upload file size, in bytes (10485760)=10M) -->   <property name="maxUploadSize" value="10485760"/>   <property name="maxInMemorySize" value="40960"/></bean>

Common methods of CommonsMultipartFile:

  • String getOriginalFilename(): get the original name of the uploaded file
  • InputStream getInputStream(): get file stream
  • void transferTo(File dest): save the uploaded file to a directory file

Let's actually test it

3. Write front page

<form action="/upload" enctype="multipart/form-data" method="post"> <input type="file" name="file"/> <input type="submit" value="upload"></form>

4,Controller

FA Yi

package com.th.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.commons.CommonsMultipartFile;import javax.servlet.http.HttpServletRequest;import java.io.*;@Controllerpublic class FileController {   //@RequestParam("file") encapsulates the file obtained from the name=file control into a CommonsMultipartFile object. / / batch upload CommonsMultipartFile is an array, @ requestmapping ("/ upload") public string fileUpload (@ RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {/ / get the file name: file. Getoriginalfilename(); string uploadfilename = file. Getoriginalfilename(); / / if the file name is empty, go directly to the home page! If ("". Equals (uploadFileName)) {return "redirect: / index. JSP";} system.out.println ("upload file name:" + uploadFileName) ; / / upload path save settings String path = request.getServletContext().getRealPath("/upload"); / / if the path does not exist, create a file realpath = new file (path); if (! Realpath. Exists()) {realpath. Mkdir();} system.out.println ("upload file save address:" + realpath ") ; InputStream is = file. Getinputstream(); / / file input stream OutputStream os = new FileOutputStream(new File(realPath,uploadFileName)); / / file output stream / / read and write int len = 0; byte [] buffer = new byte [1024]; while ((len = is. Read (buffer))! = - 1) {os.write (buffer, 0, len); OS. Flush();}        os.close();       is.close();       return "redirect:/index.jsp";  }}

5. Test upload file, OK!

Use file.Transto to save the uploaded file

1. Write Controller

Method II

/** Use file.Transto to save the uploaded file*/@RequestMapping("/upload2")public String  fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {   //Upload path saving settings string path = request. Getservletcontext(). Getrealpath ("/ upload"); file realpath = new file (path); if (! Realpath. Exists()) {realpath. Mkdir();} / / upload file address System.out.println("upload file saving address:" + realPath); / / write the file directly through CommonsMultipartFile (note this time)    file.transferTo(new File(realPath +"/"+ file.getOriginalFilename()));   return "redirect:/index.jsp";}

2. Front end form submission address modification

3. Visit submit test, OK!

11.3 file download

File download steps:

1. Set response header

2. Read file – InputStream

3. Write out file – OutputStream

4. Perform operation

5. Close flow (first on then off)

Code implementation:

@RequestMapping(value="/download")public String downloads(HttpServletResponse response ,HttpServletRequest request) throws Exception{   //Address of the picture to be downloaded string path = request. Getservletcontext(). Getrealpath ("/ upload"); String filename = "basic syntax. JPG"// 1. Set the response header response.reset()// Set the page not to be cached, and clear buffer response.setcharacterencoding ("UTF-8")// Character encoding: response.setcontenttype ("multipart / form data")// Binary data transfer / / set the response header response.setheader ("content disposition", "attachment; filename =" + urlencoder.encode (filename, "UTF-8"); File file = new File(path,fileName);   // 2. Read file -- input stream InputStream input = new FileInputStream (file)// 3. Write out the file -- output stream OutputStream out = response. Getoutputstream(); byte[] buff =new byte[1024];    int index=0;   // 4. Execute the write operation while ((index = input. Read (buff))! = - 1) {out. Write (buff, 0, index); out. Flush();} out. Close(); input.close();    return null;}

front end

<a href="/download">Click download</a>

Test, file download OK, you can compare with the Java Web native way we learned before, and you can know that this is much more convenient!

Interceptors and file operations are very important in our development. We must learn to use them!

Tags: Spring MVC mvc

Posted on Wed, 08 Sep 2021 14:50:31 -0400 by dgudema