J2EE Series of Spring MVC Learning Notes (1) --Introduction to Spring MVC

Keywords: Spring JSP xml Struts

The last blog talked about Spring's integration of Struts 2 and Hibernate (the S2SH framework). The function of Struts 2 in the framework of S2SH is to intercept front-end requests, complete the transfer of values between front-end pages and back-end logic processing codes. Hibernate's function is to complete database operations. Spring's function is to manage Struts 2 and Hibernate. This lecture tells about Spring MVC, which is similar to Struts 2 in S2SH.

Take a look at Baidu Encyclopedia's introduction to Spring MVC:

Spring MVC is a follow-up product of Spring FrameWork and has been integrated into Spring Web Flow.

The Spring framework provides a full-featured MVC module for building Web applications. Use Spring pluggable MVC architecture,

Therefore, Spring's Spring MVC framework or other MVC development frameworks can be used when developing WEB with Spring.

Such as Struts 1, Struts 2, etc.

First, the jar package of Spring MVC is in the libs folder of Spring downloaded. The jar packages needed to use Spring MVC alone include the following

(Spring version 4.1.2 is used here):

2. Create a new dynamic web project: Spring MVC01 and copy the required jar package to the lib folder of the project

Configuration Interception

Similar to Struts 2, Spring MVC also intercepts various requests from users. Struts2's interception request is configured in the web.xml file.

Spring MVC is also configured in this file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVC01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
   <servlet>
	<servlet-name>springmvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-mvc.xml</param-value>
	</init-param>
  </servlet>
  <servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

Strts2 configures the interceptor using < Filter > and < filter-mapping > tags, and here < servlet > and < servlet-mapping > tags configure the interceptor. Dispatcher Servlet in the <servlet-class> tag

Class is a forwarding class of Spring MVC, responsible for forwarding intercepted requests to the corresponding processing functions. The <init-param> tag defines other configuration parameters.

These configurations are placed in the spring-mvc.xml file. The following <servlet-mapping> tag configures the requests to be intercepted, where all requests are intercepted

Requests that end with ".do" (can also be defined as other requests, such as. html).

4. Write Spring MVC Profile: New spring-mvc.xml file under src folder

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- Packages that use annotations, including subsets -->
    <context:component-scan base-package="com.test"/>

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

</beans>

We manage all of them here based on annotations, so we first scan all the classes under the com.test package by configuring <context: component-scan base-package= "com.test"/> configuration (also used when integrating S2SH).

A view parser bean is defined below, which is similar to struts 2 (struts 2 configuration file is processed by < action > tag definition)

The various types of requests, and the configuration of the disposal of the returned results, such as jumping to a page if the returned result is success, are the views here

The parser is for returning results, <property The sentence name= "prefix" value="/WEB-INF/jsp/"/> is configured to jump to

/ WEB-INF/jsp/under this directory, <property The sentence name= "suffix" value=". jsp"> </property> is configured to jump to

A. JSP file. For example, if the handler returns a helloWorld string, the view parser jumps to / WEB-INF/jsp/.

helloWorld.jsp page.

Fifth, according to the configuration of view parser, first create a new jsp folder under the / WEB-INF folder:

6. Create a new com.test.controller package with request processing functions. In this package, create a new HelloWorldController class:

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

	@RequestMapping("/helloWorld")
	public String helloWorld(Model model){
		model.addAttribute("message", "SpringMVC Hello, Grandpa");
		return "helloWorld";
	}
}
This class uses @Controller to annotate the processing class. Spring MVC scans all classes under the com.test package according to configuration. When this annotation is scanned, the class is instantiated into a bean.

A method helloWorld is defined here. The parameter passed in by this method is the Model class object, which is different from Struts 2.

Spring MVC uses this object to transfer data between the foreground and the background (there are other methods of transmission, of course).

Use this one. This object passes data in the form of key-value pairs, where a "message" data is passed to the front desk and this is done.

Data can be added to the model object. Method returns the "helloWorld" string.

Which request does this process respond to? This is also achieved through annotations, which are added to this method.

@ RequestMapping("/helloWorld"), so that when the current desk requests helloWorld.do, the request forwarder forwards it

To this method of processing.

VII. Testing

New page index.html in WebContent (according to the configuration of the web.xml file, the default starting page of the project is index.html):

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="helloWorld.do">Hello SpringMVC</a>
</body>
</html>

Here, helloWorld.do is requested, which is intercepted and forwarded to the helloWorld method in the HelloWorld Controller class, which returns the string "helloWorld" according to

The configuration of the view parser in the spring-mvc.xml file will jump to the helloWorld.jsp page in the / WEB-INF/jsp/folder.

New helloWorld.jsp page under / WEB-INF/jsp/folder:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${message }
</body>
</html>

Here, the data transmitted from the processing method to the front desk is displayed.


Add the project to tomcat server and run the program. The program has no error. Open the corresponding IP address of the browser input program and enter the index.html page:


Click on the link and jump to the Hello world.jsp page:



The data passed in the background is shown here.


The above simple Spring MVC example is complete. Finally, the data flow using Spring MVC is summarized.

After the request of the foreground is intercepted, it is forwarded to the corresponding processing class through the transponder, and a string is returned after the processing class is processed.

The view parser controls the item to jump to the corresponding page based on this string.

In this example, the data transfer between the front and back ends is realized through the Model class object.




Posted by shaunrigby on Mon, 24 Jun 2019 12:28:45 -0700