javascript
Spring 4 MVC入门实例
Spring 4發(fā)布很長一段時(shí)間了,從 Spring 3 到 Spring 4 有巨大的改變。網(wǎng)上還有很多教程是基于 Spring 3.0 的,甚至 Spring 2.5,要想按照網(wǎng)上的教程“按圖索驥”還是挺困難的??v有一些 Spring 4.0 MVC 的教程例子,也往往是 “Spring MVC + hibernate 集成實(shí)例”這樣的例子,想找一個(gè)淺顯點(diǎn)的例子都難。
所以,下面就是一個(gè)淺顯的例子,只為對(duì) Spring 4.0 MVC 形成一個(gè)最初的印象。
項(xiàng)目目錄結(jié)構(gòu)
帶箭頭的是需要改的
項(xiàng)目搭建過程
(1)使用 maven 建立一個(gè) web 項(xiàng)目( 教程點(diǎn)此 ),pom.xml文件加入如下依賴包:
注意注意:一定要記得在項(xiàng)目屬性里,設(shè)置 Deployment Assembly,將Maven Dependencies加入發(fā)布路徑里。設(shè)置完后運(yùn)行時(shí)才會(huì)把 pom.xml 文件里的依賴加入到 /WEB-INF/lib 目錄下。
<dependencies><!-- spring 核心模塊 --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.4.RELEASE</version></dependency><!-- spring 上下文模塊 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><!-- spring web --><!-- 提供web支持 --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.4.RELEASE</version></dependency> <!-- spring web mvc --><!-- 提供web mvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.4.RELEASE</version></dependency></dependencies>(2)新建 web.xml 文件,如下:
<?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" version="3.0"><!--1 指出 spring 配置文件的位置 --><context-param><param-name>configLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param><!--2 容器加載監(jiān)聽器, 監(jiān)聽到服務(wù)器啟動(dòng)的時(shí)候,根據(jù)spring 配置文件,創(chuàng)建Spring容器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--3 spring MVC的分發(fā)器,這是一個(gè)標(biāo)準(zhǔn)的Servlet,按照spring MVC的約定,這個(gè)Servlet 的配置文件應(yīng)該叫做:<servlet name >-servlet.xml --> <servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>配置 1、2 屬于Spring Web范疇,既是在web項(xiàng)目中使用Spring的方式,使用監(jiān)聽器監(jiān)聽到服務(wù)器啟動(dòng)時(shí)自動(dòng)創(chuàng)建Spring容器。對(duì)比一下,在普通項(xiàng)目中一般是硬編碼來創(chuàng)建Spring容器:
public static void main(String[] args){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");}配置 3 屬于Spring MVC,通過配置一個(gè)Servlet,匹配所有以“/”開頭的請(qǐng)求,作為Spring MVC的入口。
(3)創(chuàng)建spring配置文件,默認(rèn)為applicationContext.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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--加入下面兩個(gè)配置,就可以用注解方式配置bean--> <context:annotation-config></context:annotation-config><context:component-scan base-package="com.huanle"/></beans>(4)創(chuàng)建spring MVC的分發(fā)控制器的配置文件(默認(rèn)為 < servlet name >-servlet.xml),DispatcherServlet根據(jù)這里的配置進(jìn)行請(qǐng)求分發(fā)
<?xml version="1.0" encoding="UTF-8"?> <!--加入了MVC命名空間 --> <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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--下面兩個(gè)配置使得可以在項(xiàng)目中使用mvc注解,如@Controller--> <mvc:annotation-driven></mvc:annotation-driven><context:component-scan base-package="com.huanle.controller"/></beans>(5)創(chuàng)建controller,使用@Controller注解和@RequestMapping注解,代碼如下:
/* @Controller注解將SayHelloController 類配置為一個(gè)控制器Bean,這個(gè)Bean就可以接受http請(qǐng)求了;@RequestMapping("/SayHello")配置了這個(gè)Bean的訪問方式 */ @Controller @RequestMapping("/SayHello") public class SayHelloController {/*@RequestMapping注解配置了helloWorld 方法的訪問方式return 語句返回一個(gè)重定向字符串*/ @RequestMapping( path = "/getAnswer" , method = RequestMethod.GET)public String helloWorld() {return "redirect:/answer.jsp";}}這個(gè)Controller可以通過
localhost:8080/huanle/SayHello/getAnswer
訪問。huanle 是工程名
總結(jié)
以上是生活随笔為你收集整理的Spring 4 MVC入门实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《小鬼当家》男主现状感受下:主演新片开拍
- 下一篇: Spring MVC搭建REST风格网站