你知道目前最流行的SpringMVC框架嗎?如何搭建呢?
Spring MVC 是 Spring 家族中的一個 web 成員, 它是一種基于 Java 的實現(xiàn)了 Web MVC 設計思想的請求驅(qū)動類型的輕量級 Web 框架,即使用了 MVC 架構(gòu)模式的思想,將 web 層進行職責解耦,基于請求驅(qū)動指的就是使用請求-響應模型,框架的目的就是幫助我們簡化開發(fā),Spring MVC 也是要簡化我們?nèi)粘?Web 開發(fā)的。
Spring MVC 是服務到工作者思想的實現(xiàn)。前端控制器是 DispatcherServlet;應用控制器拆為處理器映射器(Handler Mapping)進行處理器管理和視圖解析器(View Resolver)進行視圖管理;支持本地化/國際化(Locale)解析及文件上傳等;提供了非常靈活的數(shù)據(jù)驗證、格式化和數(shù)據(jù)綁定機制;提供了強大的約定大于配置(慣例優(yōu)先原則)的契約式編程支持。
SpringMVC 搭建的方式
- 開發(fā)環(huán)境搭建
- 新建 Maven webApp
- Springmvc 環(huán)境 jar 包依賴
- 配置 web.xml (前端控制器配置)
- servlet-context.xml 配置
- 頁面控制器的編寫
- 添加視圖頁面
- 啟動 jetty 服務器
案例實操
開發(fā)環(huán)境搭建
Eclipse + jdk1.7 + maven + Jetty
新建 Maven webApp
建立 springmvc01 工程并調(diào)整 web 環(huán)境。
Springmvc 環(huán)境 jar 包依賴
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>springmvc01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!-- web servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<!-- jetty 插件 -->
<build>
<finalName>springmvc01</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<!-- 編譯環(huán)境插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<contextPath>/springmvc01</contextPath>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置 web.xml (前端控制器配置)
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 表示容器啟動時 加載上下文配置 這里指定 spring 相關配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*.xml</param-value>
</context-param>
<!-- 啟用 spring 容器環(huán)境上下文監(jiān)聽 -->
<listener>
<listener class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 編碼過濾 utf-8 -->
<filter>
<description>char encoding filter</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- servlet 請求分發(fā)器 -->
<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:servlet-context.xml</param-value>
</init-param>
<!-- 表示啟動容器時初始化該 Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<!-- 這是攔截請求, /代表攔截所有請求,攔截所有.do 請求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
要想啟動我們的 springMvc 環(huán)境,目前對于 mvc 框架的配置還未進行。以上在 web.xml 中引用了 servlet-context.xml 文件。
servlet-context.xml 配置
<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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 掃描 com.xxx.controller 下包 -->
<context:component-scan base-package="com.xxx.controller" />
<!-- mvc 請求映射 處理器與適配器配置-->
<mvc:annotation-driven/>
<!--配置視圖解析器 默認的視圖解析器- -->
<bean id="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="contentType" value="text/html" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
如果返回亂碼:配置消息轉(zhuǎn)換器
<!-- 消息轉(zhuǎn)換器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</mvc:message-converters>
頁面控制器的編寫
/**
* 采用注解掃描形式
*/
@Controller
public class HelloController {
/**
* 請求映射地址 /hello
* @return
*/
@RequestMapping("/hello")
public ModelAndView hello(){
ModelAndView mv=new ModelAndView();
mv.addObject("hello", "hello spring mvc");
mv.setViewName("hello");
return mv;
}
}
添加視圖頁面
在 WEB-INF 下新建 jsp 文件夾 ,并在文件加下新建 hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'hello.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<!-- el 表達式接收參數(shù)值 -->
${hello}
</body>
</html>
啟動 jetty 服務器
選中項目右鍵 → run as → maven build → goals 輸入框中輸入 jetty:run 即可啟動服務器。
如果這里啟動成功,瀏覽器(最好用強大的 chrome 或者火狐瀏覽器,程序員的最愛?。?!)訪問地址 http://localhost:8080/springmvc01/hello
擴展
Spring MVC 能幫我們做什么
讓我們能非常簡單的設計出干凈的 Web 層;
進行更簡潔的 Web 層的開發(fā);
天生與 Spring 框架集成(如 IoC 容器、AOP 等);
提供強大的約定大于配置的契約式編程支持;
能簡單的進行 Web 層的單元測試;
支持靈活的 URL 到頁面控制器的映射;
非常容易與其他視圖技術集成,如 Velocity、FreeMarker 等等,因為模型 數(shù)據(jù)不放在特定的 API 里,而是放在一個 Model 里(Map 數(shù)據(jù)結(jié)構(gòu)實現(xiàn),因此很容易被其他框架使用);
非常靈活的數(shù)據(jù)驗證、格式化和數(shù)據(jù)綁定機制,能使用任何對象進行數(shù)據(jù)綁定,不必實現(xiàn)特定框架的 API;
支持靈活的本地化等解析;
更加簡單的異常處理;
對靜態(tài)資源的支持;
支持 Restful 風格。