新建maven工程,添加maven依賴
<properties >
<spring.version > 4.1.3.RELEASE</spring.version >
</properties >
<dependencies >
<dependency >
<groupId > javax.servlet</groupId >
<artifactId > javax.servlet-api</artifactId >
<version > 3.0.1</version >
<scope > provided</scope >
</dependency >
<dependency >
<groupId > javax.servlet.jsp</groupId >
<artifactId > jsp-api</artifactId >
<version > 2.1</version >
<scope > provided</scope >
</dependency >
<dependency >
<groupId > org.springframework</groupId >
<artifactId > spring-core</artifactId >
<version > ${spring.version}</version >
</dependency >
<dependency >
<groupId > org.springframework</groupId >
<artifactId > spring-context</artifactId >
<version > ${spring.version}</version >
</dependency >
<dependency >
<groupId > org.springframework</groupId >
<artifactId > spring-webmvc</artifactId >
<version > ${spring.version}</version >
</dependency >
<dependency >
<groupId > org.springframework</groupId >
<artifactId > spring-web</artifactId >
<version > ${spring.version}</version >
</dependency >
</dependencies >
在Web.xml配置文件中關於SpringMVC的配置
通過閱讀springMVC的文檔可知在web.xml文件中有,如下配置
<web-app >
<context-param >
<param-name > contextConfigLocation</param-name >
<param-value > /WEB-INF/root-context.xml</param-value >
</context-param >
<servlet >
<servlet-name > dispatcher</servlet-name >
<servlet-class > org.springframework.web.servlet.DispatcherServlet</servlet-class >
<init-param >
<param-name > contextConfigLocation</param-name >
<param-value > </param-value >
</init-param >
<load-on-startup > 1</load-on-startup >
</servlet >
<servlet-mapping >
<servlet-name > dispatcher</servlet-name >
<url-pattern > /*</url-pattern >
</servlet-mapping >
<listener >
<listener-class > org.springframework.web.context.ContextLoaderListener</listener-class >
</listener >
</web-app >
根據自己的理解以及基於安全的原則,我們優化成如下結構
<web-app >
<servlet >
<servlet-name > dispatcherServlet</servlet-name >
<servlet-class > org.springframework.web.servlet.DispatcherServlet</servlet-class >
<init-param >
<param-name > contextConfigLocation</param-name >
<param-value > /WEB-INF/spring-mvc.xml</param-value >
</init-param >
<load-on-startup > 1</load-on-startup >
<servlet-mapping >
<servlet-name > dispatcher</servlet-name >
<url-pattern > /*</url-pattern >
</servlet-mapping >
</web-app >
當然Spring官方還爲我們提供了另一種初始化SpringMVC的方式
就是新建一個類實現WebApplicationInitializer接口,以實現將DispatcherServlet動態的註冊到web容器中,跟在配置文件中配置是一個道理
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup (ServletContext container) {
ServletRegistration.Dynamic registration = container.addServlet("dispatcher" , new DispatcherServlet());
registration.setLoadOnStartup(1 );
registration.addMapping("/example/*" );
}
}
在WEB-INF目錄下添加spring-mvc.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: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" >
<context:component-scan base-package ="使用了Controller註解的類所在的包" />
<bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver"
id ="internalResourceViewResolver" >
<property name ="prefix" value ="/WEB-INF/views/" />
<property name ="suffix" value =".jsp" />
</bean >
</beans >
編寫代碼
編寫Controller代碼
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping (value = "/hello" )
public class HelloController {
@RequestMapping (value = "/helloWorld" )
public String hello () {
System.out.println("----------------->/helloWorld" );
return "hello" ;
}
}
編寫hello.jsp頁面的代碼
<%@ 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 >
<h3 > hello,first Spring Mvc</h3 >
</body >
</html >
出現如下效果