Learn the basic concepts and introductory cases of using Spring MVC in IDEZ

1. Basic concepts of Spring MVC

1.1 On Three-tier Architecture and MVC

1.1.1 Three-tier Architecture

The development of server-side programs, generally based on two forms, a C/S architecture program, a B/S architecture program. The use of Java language is basically the development of B/S architecture program, B/S architecture is divided into three layers of architecture:

Presentation layer: WEB layer, used for data interaction with clients. The design model of MVC is usually used in the presentation layer.
Business Layer: Processing the company's specific business logic
Persistence Layer: Used to manipulate databases

1.1.2. MVC Model

The full name of MVC is Model View Controller Model View Controller, each part of which has its own responsibilities.

Model: Data model, JavaBean class, used for data encapsulation.
View: Refers to JSP, HTML used to display data to users
Controller: Controller used to receive user requests, the entire process. Used for data validation, etc.

1.2 Summary of Spring MVC

1.2.1 What is Spring MVC

Spring MVC is a lightweight Web framework based on Java to implement MVC design model. It has become one of the most mainstream MVC frameworks. With the release of Spring 3.0, it surpasses Struts 2 and becomes the best MVC framework. It uses a set of annotations to make a simple Java class a controller for processing requests without implementing any interfaces. It also supports RESTful programming style requests.

1.2.2 Location of Spring MVC in the three-tier architecture

1.2.3 Advantages of Spring MVC

1. Clear role demarcation: Dispatcher Servlet

Request to Handler Mapping
Handler Adapter
ViewResolver
Processor or Page Controller
Validator
Command Object (Command Request Parameter Binding Object is called Command Object)
Form Object (the object that Form Object provides for form presentation and submission is called form object)

2. The division of labor is clear, and the extension point is quite flexible. It can be easily extended, although it is almost unnecessary.
3. Since the command object is a POJO, it can be used as a business object directly without inheriting the framework-specific API.
4. Seamless integration with other Spring frameworks is not available in other Web frameworks.
5. Adaptable. Handler Adapter can support any class as a processor.
6. Customizability, Handler Mapping, ViewResolver and so on can be very simple customization.
7. Powerful data validation, formatting and binding mechanism.
8. Using the Mock object provided by Spring, unit testing of Web layer can be carried out very simply.
9. The support of localization and theme analysis makes it easier for us to switch between internationalization and theme.
10. Powerful JSP tag library makes JSP writing easier.
... There are also RESTful style support, simple file upload, contractual programming support with conventions greater than configuration, zero configuration support based on annotations, and so on.

1.2.4 Preferential Analysis of Spring MVC and Struts 2

Common ground:
They are both presentation-level frameworks, which are based on the MVC model.

Their underlying layers are inseparable from the original Servlet API.

Their mechanism for processing requests is a core controller.

Difference:
The entry to Spring MVC is Servlet, while Struts 2 is Filter.

Spring MVC is method-based, while Struts 2 is class-based. Struts 2 creates an action class every time it executes. So Spring MVC is slightly faster than Struts 2.

Spring MVC is simpler to use and supports JSR303, which makes it easier to handle ajax requests. (JSR303 is a set of JavaBean parameter checking standards. It defines many common checking annotations. We can add these annotations directly to our JavaBean attributes, so we can check them when we need to check them. Yes. )

Struts 2's OGNL expression makes page development more efficient than Spring MVC, but its execution efficiency is not higher than that of JSTL, especially the form tag of struts 2, which is far less efficient than that of html.

2. Introduction to Spring MVC

2.1 Use maven to create projects

file → project→

Next Fill in the project name and click Next click the Add button to add the following configuration, then click to skip the download configuration.

name: archetypeCatalog
value: internal


Click Next finish to complete the creation

2.2 Completed Catalogue

Create two folders under main

2. Right-click java Mark Directory as Source Root, right-click resources Mark Directory as Source Root


2.3 Coordinate Introducing Dependency

Add Spring version locking to the pom.xml document

Adding coordinate dependencies to dependencies Tags

		<dependencies>
	    <dependency>
	      <groupId>org.springframework</groupId>
	      <artifactId>spring-context</artifactId>
	      <version>${spring.version}</version>
	    </dependency>
	
	    <dependency>
	      <groupId>org.springframework</groupId>
	      <artifactId>spring-web</artifactId>
	      <version>${spring.version}</version>
	    </dependency>
	
	    <dependency>
	      <groupId>org.springframework</groupId>
	      <artifactId>spring-webmvc</artifactId>
	      <version>${spring.version}</version>
	    </dependency>
	
	    <dependency>
	      <groupId>javax.servlet</groupId>
	      <artifactId>servlet-api</artifactId>
	      <version>2.5</version>
	      <scope>provided</scope>
	    </dependency>
	
	    <dependency>
	      <groupId>javax.servlet.jsp</groupId>
	      <artifactId>jsp-api</artifactId>
	      <version>2.0</version>
	      <scope>provided</scope>
	    </dependency>
	
	  </dependencies>

2.4 Configuring Front-end Controller in web.xml

2.5 Add Springmvc configuration file to resources

2.6 Configure tomcat

2.7 Introduction Case Code

Directory structure:

controller:

 package com.hejie.controller;    		
		
		import org.springframework.stereotype.Controller;
		import org.springframework.web.bind.annotation.RequestMapping;
		
		//Controller class
		@Controller
		public class helloController {
		
		    @RequestMapping(path="/hello")
		    public String sayHello(){
		
		        System.out.println("hello SpringMVC");
		
		        //The string "success" returned indicates that a string named success.jsp is returned.
		        //This jsp file is generally created in WEB-INFO/pages
		        return  "success";
		
		    }
		}

Springmvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Open Annotation Scan -->
    <context:component-scan base-package="com.hejie"/>

    <!-- View parser object
         class Provision of allocation, id Arbitrary starting, generally fixed
    -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- Configure the prefix, that is, the path -->
        <property name="prefix" value="/WEB-INF/pages/"/>
       <!--  Configure suffixes, which types of files-->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

success.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3>Successful introduction</h3>

</body>
</html>

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
    <!--
    servlet-class Fixed as DispatcherServlet
    servlet-name From any point of view, generally dispatcherServlet
    -->
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- servlet Create-time loading Springmvc.xml file-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:Springmvc.xml</param-value>
    </init-param>
    <!-- Load only once  -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <!--
      servlet-name Must be with the above servlet-name Agreement
      url-pattern in"/"Indicates that any request will be intercepted
    -->
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>  
</web-app>

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3>Initial procedures</h3>

    <a href="hello">Initial procedures</a>

</body>
</html>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.hejie</groupId>
  <artifactId>SpringMVC_Start</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SpringMVC_Start Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <!--  Version Locking for Easy Modification Spring Relevant configuration versions  -->
    <spring.version>5.0.2.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>

  </dependencies>

  <build>
    <finalName>SpringMVC_start</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Keywords: Spring Maven JSP xml

Added by ph3n0m on Sun, 21 Jul 2019 09:23:57 +0300