SpringMVC框架配置

来源:互联网 发布:食品代加工 知乎 编辑:程序博客网 时间:2024/06/08 19:43

配置springMVC框架

环境:eclipse+Tomcat 7.0.70+JDK 1.8

架包:


该架包配置中包含了Spring架包,部分Hibernate架包,以及Junit和其他一些依赖项

Spring架包下载链接:https://repo.spring.io/webapp/#/home


配置文件目录结构:



web.xml配置

  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list><!-- 配置SpringMVC的DispatcherServlet 控制器 --><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置DispatcherServlet的一个初始化参数:配置SpringMVC配置文件的位置名称 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.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>  <!-- 配置Spring IOC 容器Spring ApplicationContext配置文件的路径 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- Spring IOC监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置编码方式过滤器,在所有过滤器的前面 --><filter><filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 使用SpringMVC框架实现REST风格,配置 HiddenHttpMethodFilter --><filter><filter-name>hiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>hiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>

applicationContext.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx.xsd    http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 注解配置 --><context:annotation-config /><!-- 扫描装配bean --><context:component-scan base-package="com.osms" /><!-- 配置自动扫描的包 --><context:component-scan base-package="com.osms" use-default-filters="false"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /><context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /></context:component-scan></beans>

springmvc.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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 配置自动扫描的包 --><context:component-scan base-package="com.osms" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/></context:component-scan><!-- 配置视图解析器:把handler方法返回值解析为实际的物理视图 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"></property><property name="suffix" value=".jsp"></property> </bean><!-- 配置静态资源:default-servlet-handler将在SpringMVC上下文中定义DefaultServletHttpRequestHandler, 它会对进入DispatcherServlet的请求进行帅选,如果发现是没有经过映射的请求,就将该请求交由WEB应用服务器默认的 Servlet处理。如果不是静态资源的请求,才由DispatcherServlet继续处理。 --><mvc:default-servlet-handler /><!-- 配置开启注解 --><mvc:annotation-driven/></beans>

tag:项目配置源码,若有需求将会贴出;也许各位自己配置的框架会报错,那是个版本之间的兼容问题,换对版本即可。

0 0
原创粉丝点击