Spring 基础

为了使用Spring MVC,需要在web.xml 中配置一个分派器,将一些特定格式的请求交给
Spring MVC 来处理(其实就是一个Servlet,这和Struts有些类似),如果它的名字是dispatcher,
那么Spring 默认会去寻找名为dispatcher-servlet.xml 的配置文件

web.xml,每个Java EE 的Web 项目都会有这个配置文件,具体内容如下:
代码:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
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
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!-- Spring ApplicationContext配置文件的路径可使用通配符,多个路径用,号分隔,此参数用于后面的Spring-Context loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/modules/applicationContext*.xml</param-value>
</context-param>

<!-- SpringMVC 分派器及相关映射 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!--Spring ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- 支持session scope的Spring bean -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<!-- Character Encoding filter -->
<filter>
<filter-name>setCharacterEncoding</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>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--Hibernate Open Session in View Filter-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

此处加载了Spring 的配置文件,并对字符编码进行了处理,*.do 和*.html 的请求都转交给了
Spring MVC 的分派器。OpenSessionInViewFilter 是用来解决Hibernate 的OpenSessionInVie w
问题的,如果没有使用Hibernate 则无需配置此过滤器。
项目的部署和一般的Web 项目没有任何区别,将项目打成War 包或者直接将目录放到
Tomcat 的webapps 中即可。假设目录的名字是SpringDemo ,启动Tomcat 后访问
http://localhost:8080/SpringDemo/listarticle.html 就能看到页面的效果了。

在spring中使用属性文件里面的值配置:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/classes/jdbc.properties</value>
<value>WEB-INF/classes/config.properties</value>
</list>
</property>
</bean>


web.xml中配置来查找spring的配置文件以便初始化

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-somehost.com.xml
</param-value>
</context -param>


有很多可以添加额外的资源以用于配置的方式
。其中最明显的就是使用<import>标签将这一额外资源包含在
applicationContext.xml 配置文件中。使用时,要将该标签放在
applicationContext.xml 文件开头。例如:
<import resource="applicationContext-somehost.com.xml"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值