Home > Java > javaTutorial > body text

Detailed explanation of SpringMVC

大家讲道理
Release: 2017-08-19 13:58:59
Original
1913 people have browsed it

In this series of tutorials, we will introduce SpringMVC in detail. I believe that after completing this series of tutorials, you will be able to use it freely in actual development.

1. What is SpringMVC?

Before introducing what SpringMVC is, let’s first take a look at the basic architecture of Spring. As shown below:

 

We can see that in the basic architecture of Spring, the Spring Web MVC circled in red is the protagonist of this series, SpringMVC, which belongs to Spring It is a component of the basic architecture and is a follow-up product of SpringFrameWork. It has been integrated into Spring Web Flow, so when we integrate it with Spring later, there is almost no need for any other configuration.

 SpringMVC is an MVC framework similar to Struts2. In actual development, it receives the browser's request response, processes the data, and then returns the page for display, but it is much easier to get started than Struts2. And due to the security issues exposed by Struts2, SpringMVC has become the preferred framework for most enterprises.

Without saying much, let’s take a look at the magic of SpringMVC directly through an example.

ps: Download link for the source code of this blog: http://pan.baidu.com/s/1c1OJi5E Password: 9hnc

2. Create a web project and Import the corresponding jar package.

 

Here we have added all the jar packages of Spring 3.2, which just proves that SpringMVC mentioned above is part of the Spring architecture. Note: it must be circled in red ovals spring-webmvc-3.2.0.RELEASE.jar

3. Create a new SpringMVC global configuration file

Create a new springmvc.xml file in the src directory and add the following Code:

 

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


</beans>
Copy after login

 

4. Configure the front-end filter in the web.xml 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC_01</display-name>
  <!-- 配置前端控制器DispatcherServlet -->
  <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:springmvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
Copy after login

 

 

5. Write the processor Handler

package com.ys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelView = new ModelAndView();
		//类似于 request.setAttribute()
		modelView.addObject("name","张三");
		modelView.setViewName("/WEB-INF/view/index.jsp");
		return modelView;
	}

}
Copy after login

 

6. Configure Handler, processor mapper, processor adapter, and attempt parser in the springmvc.xml file

 In springmvc.xml Add the following code to the file:

<!-- 配置Handler -->    
<bean name="/hello.do" class="com.ys.controller.HelloController" />
   <!-- 配置处理器映射器
   	将bean的name作为url进行查找,需要在配置Handler时指定bean name(就是url)-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
   <!-- 配置处理器适配器,所有适配器都得实现 HandlerAdapter接口 -->
   <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> 

<!-- 配置视图解析器 
   	进行jsp解析,默认使用jstl标签,classpath下得有jstl的包-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
Copy after login

 

7. Create the index.jsp file in the /WEB-INF/view directory

 

<%@ 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>
hello:${name}
</body>
</html>
Copy after login

 

8. Enter http://localhost:8080/SpringMVC_01/hello.do in the browser

 

Following the above steps, I believe you have built a simple instance of SpringMVC, so why do you write it like this? Please see the next blog for breakdown! ! !

The above is the detailed content of Detailed explanation of SpringMVC. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!