ecshop商城網(wǎng)站建設(shè)seo搜索優(yōu)化工具
簡(jiǎn)介
Spring MVC 屬于 SpringFrameWork 的后續(xù)產(chǎn)品,已經(jīng)融合在 Spring Web Flow 里面;Spring 框架提供了構(gòu)建 Web 應(yīng)用程序的全功能 MVC 模塊;使用 Spring 可插入的 MVC 架構(gòu),從而在使用Spring進(jìn)行WEB開(kāi)發(fā)時(shí),可以選擇使用 Spring的SpringMVC 框架或集成其他MVC開(kāi)發(fā)框架;下面將演示搭建第一個(gè) SpringMVC 項(xiàng)目
實(shí)現(xiàn)步驟
-
首先我們先創(chuàng)建一個(gè)動(dòng)態(tài) web 項(xiàng)目,名為:SpringMVC;如果不用 maven 的話,Spring 的對(duì)應(yīng) jar 包可以直接在這個(gè)網(wǎng)址下載:https://repo.spring.io/libs-release-local/org/springframework/spring/
-
將 Spring 的包全部導(dǎo)入到項(xiàng)目的 lib 文件夾下,除了 Spring 的包之外,還有一個(gè) commons-logging.jar 包一樣要導(dǎo)入進(jìn)去
-
在 web.xml 中添加如下內(nèi)容;注意這里的 servlet-class:org.springframework.web.servlet.DispatcherServlet,我們要使用 Spring 的DispatcherServlet 來(lái)控制流程,攔截項(xiàng)目中其他的 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>
還可以順便加上中文過(guò)濾器
<!-- 字符過(guò)濾器 -->
<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)建一個(gè) spring-servlet.xml 文件
-
給 spring-servlet.xml 文件添加相應(yīng)的 schema 配置, 可以通過(guò)打開(kāi) \docs\spring-framework-reference\htmlsingle.html 文件,然后搜索:‘xmlns:mvc’ 找到相應(yīng)的 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是指程序會(huì)在哪些包下面去找添加了 annotation 的類 mvc:annotation-driven/ 是指打開(kāi) SpringMVC的annotation功能;最后的 bean,InternalResourceViewResolver 是指我們選擇這種方式來(lái)映射 view,里面的兩個(gè)配置分別是返回映射的前綴和后綴,假如:在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.新建一個(gè)controller.class來(lái)測(cè)試
@
Controller("testController")
@ Scope("singleton") //單例模式,默認(rèn),可省略;多例模式的話,應(yīng)配置成 prototype
public class TestController {@RequestMapping({"/hello", "/"})public String hello() {System.out.println("hello");return "hello";}
}
注意:如果按照以上步驟操作,出現(xiàn)404錯(cuò)誤,并發(fā)現(xiàn) nohandlerfound 異常
(1) 仔細(xì)檢查每一個(gè)配置文件中的配置內(nèi)容是否正確
(2) 如果確定每一個(gè)配置文件正確,引用的class也沒(méi)有問(wèn)題,那么可以嘗試在 eclipse 中手動(dòng) bulid project