建立一个名为: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。