Spring Road (16) - to achieve a complete SpringMVC instance (using the xml configuration SpringMVC)

background

Before talking about the program, they are ordinary Java Project, that is output from the console output is relatively simple.

SpringMVC is a web application, you need to deploy to the web server (paper, Tomcat) to run on the Java Project and project structure is also different.

So the next article will be described in detail from start to finish using SpringMVC web development project process.

Note that still use paper forms to import jar package to operate without using mavan, or because maven think it will be will guide jar package, but will not necessarily lead jar package maven, take care of the foundation here is to maximize weak little brothers and sisters.

Development environment

This series of articles have already talked about the development environment, and here again the next tips:

  1. Install JDK, and configuration environment variable
  2. Download, unzip eclipse
  3. Download, unzip tomcat
  4. The configuration of the JDK and tomcat in eclipse
  5. Download spring corresponding jar package ready for use

See the following link specific steps, not repeat them here
Road Spring of (2) - around the past development environment configuration

Create a web project

Open Eclipse, then click on the menu bar [File] - [New] - [Other], select Dynamic Web Project, is to create a dynamic web site, as follows:
Here Insert Picture Description
Step direct default, then the third step in the following figure operation :
Here Insert Picture Description
click Finish have created a web project for us, the project name for the SpringMvcFirst

Project structure

The following diagram, we need to focus on the following directories:

  1. src directory contains the source code we've written java
  2. WebContent directory for web site content
  3. lib directory under WEB-INF can store jar package we need to reference
  4. web.xml under WEB-INF entire project configuration files

Here Insert Picture Description
We first establish org.maoge.first package store code under src, then spring into the relevant jar package lib directory, ready to start developing.

Web.xml configuration file

作为一个web项目,它的配置是由web.xml定义的,所以我们先来配置下web.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>SpringMvcFirst</display-name>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/springmvc-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

理解这个配置文件非常重要,所以我们逐一剖析里面的内容。

1、<?xml version="1.0" encoding="UTF-8"?>
这一行表示本文件是xml文件,没啥意思,xml配置文件都是这样开头的。

2、<web-app>
该标签表示这是一个web应用的配置文件,对web应用的配置都在这个标签内部。这个是自动生成的不用管。

3、<display-name>SpringMvcFirst</display-name>
此书是项目的显示名称,没啥大意思,可以删掉。

4、<servlet><servlet-mapping>
注意这两个标签是成对存在的,可以看到servlet-name都是springmvc,所以这两个标签是一对。<servlet>包含了对servlet的配置,而servlet-mapping说明了指定名称的servlet接受请求的url匹配。上面的配置说明以.do结尾的请求都交给名字是springmvc的servlet处理,注意*是通配符,表示所有。

5、<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
通过上面的配置,我们已经确定xxxxx.do这样的请求,都会交给名字为springmvc的servlet处理,然后该servlet具体的实现类是DispatcherServlet,也就是说从此处开始我们将传统的servlet替换为了SpringMVC封装的DispatcherServlet,所以相关请求会由DispatcherServlet处理。

6、<load-on-startup>1</load-on-startup>
该标签的意思是已启动容器就加载该servlet,这个不用说,应用启动了就该马上加载servlet,不然谁来处理请求?

7、<init-param>
这个表示初始化参数,此处是理解整个web项目的关键我们要好好琢磨琢磨。

	    <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/springmvc-config.xml</param-value>
		</init-param>

我们详细的看下这个配置,我们配置了一个名字叫contextConfigLocation的参数,它的值为/WEB-INF/springmvc-config.xml

参数是啥意思呢,context之前我们多次遇到了,一般表示Spring容器,config表示配置,location表示位置,那么我们可以推测出该参数指定了spring容器所使用的配置文件的位置。

OK,从此处我们就可以知道了,web项目运行时首先会加载web.xml,根据web.xml对项目的配置启动整个web项目,然后发现了contextConfigLocation,根据/WEB-INF/springmvc-config.xml的配置对spring相关容器进行配置和加载。从而实现了web项目与spring的结合。

配置springmvc-config.xml文件

然后我们看下springmvc-config.xml文件的配置内容,是不是非常简单,就是一个完全遵循Spring中xml配置规则普通配置文件啊。

稍微值得我们注意的是开启了对org.maoge.first包的扫描,如果该包下有相应的bean配置,会被Spring容器发现并纳入容器管理。

<?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"
	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-4.0.xsd">
	<context:component-scan base-package="org.maoge.first" />
</beans>

编写具体控制器处理请求

现在已经配置了DispatcherServlet接受用户请求,此时我们需要编写控制器具体处理用户的请求,代码如下:

package org.maoge.first;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
	@RequestMapping("/hello")
	public ModelAndView hello() {
		ModelAndView mv=new ModelAndView();	
		mv.setViewName("hello.jsp");
		return mv;
	}
}

如果曾经写过SSM或者SpringBoot相关的项目,看到这段代码应会倍感亲切。此处我还是要详细介绍下这段代码。

@Controller注解将HelloController类注册为容器管理的bean,不同于@Componet注册的普通bean,@Controller注册的bean具备处理器的功能,可以接受DispatcherServlet传递过来的请求并进行处理返回,所以叫控制器么。

@RequestMapping("/hello")表示该方法接受请求路径为/hello的请求,也就是说一个请求经过DispatcherServlet后,如果其请求url中匹配了/hello,则该请求由hello()方法处理。

ModelAndViewIs a packaged class, represents a model and view, we can see that the above code returns a name for the view containing the object hello.jsp mv, meaning that the page return hello.jsp

Written page views

Finally, write a simple hello.jsp under, into the WebContent directory, as follows:

<%@ page language="java" contentType="text/html; charset=UTF8"
	pageEncoding="UTF8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF8">
<title></title>
</head>
<body>
hello:springmvcfisrt
</body>
</html>

Run validation

Deploy the project to tomcat and run, open your browser and enter the path http://127.0.0.1:8080/SpringMvcFirst/hello.do, where we SpringMvcFirst is configured in the new project context root, hello is the access path controller configuration, while .do is configured in xml DispatcherServletrequest path matches.

Attention being given at this time

More than one fragment with the name [spring_web] was found. This is not legal with relative ordering.

In the web.xml <display-name>after adding <absolute-ordering />close to this issue.

Run again, the browser visit a success!

Published 330 original articles · won praise 238 · views 530 000 +

Guess you like

Origin blog.csdn.net/woshisangsang/article/details/104082778
Recommended