ecshop商城網(wǎng)站建設seo搜索優(yōu)化工具
簡介
Spring MVC 屬于 SpringFrameWork 的后續(xù)產(chǎn)品,已經(jīng)融合在 Spring Web Flow 里面;Spring 框架提供了構(gòu)建 Web 應用程序的全功能 MVC 模塊;使用 Spring 可插入的 MVC 架構(gòu),從而在使用Spring進行WEB開發(fā)時,可以選擇使用 Spring的SpringMVC 框架或集成其他MVC開發(fā)框架;下面將演示搭建第一個 SpringMVC 項目
實現(xiàn)步驟
-
首先我們先創(chuàng)建一個動態(tài) web 項目,名為:SpringMVC;如果不用 maven 的話,Spring 的對應 jar 包可以直接在這個網(wǎng)址下載:https://repo.spring.io/libs-release-local/org/springframework/spring/
-
將 Spring 的包全部導入到項目的 lib 文件夾下,除了 Spring 的包之外,還有一個 commons-logging.jar 包一樣要導入進去
-
在 web.xml 中添加如下內(nèi)容;注意這里的 servlet-class:org.springframework.web.servlet.DispatcherServlet,我們要使用 Spring 的DispatcherServlet 來控制流程,攔截項目中其他的 xml 文件
<servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern>
</servlet-mapping>
還可以順便加上中文過濾器
<!-- 字符過濾器 -->
<filter><filter-name>CharacterFilter</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>CharacterFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>
-
在 web.xml 同目錄下創(chuàng)建一個 spring-servlet.xml 文件
-
給 spring-servlet.xml 文件添加相應的 schema 配置, 可以通過打開 \docs\spring-framework-reference\htmlsingle.html 文件,然后搜索:‘xmlns:mvc’ 找到相應的 schema,注意還要添加 context 的 schema,最基本的內(nèi)容如下
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- 配置如下內(nèi)容,context:component-scan是指程序會在哪些包下面去找添加了 annotation 的類 mvc:annotation-driven/ 是指打開 SpringMVC的annotation功能;最后的 bean,InternalResourceViewResolver 是指我們選擇這種方式來映射 view,里面的兩個配置分別是返回映射的前綴和后綴,假如:在controller 中返回了 ‘hello’ 字符串,那么,view的路徑就是 view path = prefix + ‘hello’ + ‘.jsp’
<context:component-scan base-package="com.ibm.reskill"/>
<mvc:annotation-driven/>
<!--(推薦)第一種:視圖層配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><! -- 可省略 --><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/>
</bean><!--第二種:視圖層配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"><! -- 可省略 --><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" />
</bean>
7.新建一個controller.class來測試
@
Controller("testController")
@ Scope("singleton") //單例模式,默認,可省略;多例模式的話,應配置成 prototype
public class TestController {@RequestMapping({"/hello", "/"})public String hello() {System.out.println("hello");return "hello";}
}
注意:如果按照以上步驟操作,出現(xiàn)404錯誤,并發(fā)現(xiàn) nohandlerfound 異常
(1) 仔細檢查每一個配置文件中的配置內(nèi)容是否正確
(2) 如果確定每一個配置文件正確,引用的class也沒有問題,那么可以嘗試在 eclipse 中手動 bulid project