建立一個名爲:springwebmvc-first的maven項目
css
要使用springWebMVC註解開發須要spring的如下模塊: java
在pom.xml文件添加以上的模塊 web
<properties> <org.springframework.version>4.0.5.RELEASE</org.springframework.version> <org.apache.tiles.version>3.0.4</org.apache.tiles.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-extras</artifactId> <version>${org.apache.tiles.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies>
說明: spring
以上經過<properties></properties>定義了各依賴包的版本號,這樣作有利於只須要修改一個地方就能將全部的版本號修改,而後使用${}將各版本配置到具體的依賴中。 apache
我這裏將Spring的配置和Spring Web MVC的配置分開爲兩個配置文件:applicationContext-conf.xml和applicationContext-mvc.xml。
spring-mvc
applicationContext-conf.xml: tomcat
<?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: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="com.zhiwen.dao" /> <context:component-scan base-package="com.zhiwen.service" /> </beans>
在/WEB-INF/在添加applicationContext-mvc.xml文件 mvc
applicationContext-mvc.xml app
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- 啓用spring MVC註解 --> <context:annotation-config /> <context:component-scan base-package="com.zhiwen.controller" /> <!----> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <!-- 這段是幹什麼的? --> <!-- 這是指明Spring的文件是在哪。 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext-config.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <!-- 這是指明Spring MVC的配置文件在哪。 --> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> </web-app>
那麼如何才能使用Spring的註解呢? jsp
先來看下<context:annotation-config /> 和<mvc:annotation-driven />是幹什麼的?
這樣,咱們就在就在Java web項目中使用了Spring Web MVC來處理請求了。
在實際操做中,使用maven管理java web 項目的依賴時沒有將相關的jar包部署tomcat中去,從而致使了找不到相關的jar包,解決辦法是:右擊項目屬性,在Deployment Assembly里加入Maven lib。