SpringMVC项目搭建

1,将spring jar包导入到项目中

2,在/WEB-INF/web.xml中配置

 <!-- 指定spring配置文件的路径 -->

 <context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath:spring-mvc.xml

</param-value>

 </context-param>

 <!-- 配置spring监听器 -->

 <listener>

  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

 </listener>

 <!-- 配置springMVCservlet -->

 <servlet>

  <servlet-name>spring</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <!-- 指定springMVC配置文件,如果不指定,默认是在WEB-INF目录下与            DispatcherServletName-servlet.xml -->

  <init-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:spring-mvc.xml</param-value>

  </init-param>

  <load-on-startup>1</load-on-startup>

 </servlet>

 <!-- 设置什么样的请求路径使用spring MVC -->

 <servlet-mapping>

  <servlet-name>spring</servlet-name>

  <url-pattern>/chp/*</url-pattern>

 </servlet-mapping>



spring-mvc.xml配置文件

<?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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:flex="http://www.springframework.org/schema/flex"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx 

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/mvc        

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--启动扫描的包  -->

<context:component-scan base-package="com.chp.*" />

<!-- 启动mvc注解 -->

<context:annotation-config/>

<!-- 

<mvc:annotation-driven/>

-->

<!-- 配置模型路径解析 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  p:prefix="/jsps/" p:suffix=".jsp"></bean>

</beans>


你可能感兴趣的:(SpringMVC项目搭建)