现在的位置: 首页 > 综合 > 正文

spring MVC配置文件解读

2013年12月07日 ⁄ 综合 ⁄ 共 3105字 ⁄ 字号 评论关闭

本人spring新手,初玩是spring的感觉就是坑爹的配置文件,为此写此文为正在苦苦挣扎于spring配置文件的同学简单的解释下配置文件。

欢迎拍砖。

先来看web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
		<welcome-file>index.jsp</welcome-file>

	</welcome-file-list>
	<!-- 声明spring的核心类(其实是一个servlet,所以也逃不了要声明),同时此处载入spring的配置文件 -->
	<servlet>
		<servlet-name>dipatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/applicationContext.xml</param-value>
		</init-param>
	</servlet>
	<!-- 对那种请求进行转发 -->
	<servlet-mapping>
		<servlet-name>dipatcher</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<!-- 定义编码过滤器,对中文等进行重新编码,去除该过滤器则中文乱码 -->
	<filter>
		<filter-name>Set Character Encoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>Set Character Encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

下面我们在来看看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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	<!-- 表示层的声明:主要关于表示层前缀路径(如下:/view/)和后缀路径(如下:.jsp). 这么声明代表你的表示层文件都在/view/下,而且都是.jsp文件 -->
	<!-- efinition of View Resolver -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass">
			<value>org.springframework.web.servlet.view.JstlView</value>
		</property>
		<property name="prefix">
			<value>/view/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	<!--此处用于声明action,在此下方要一一列出action的配置 -->
	<!-- Request Mapping -->
	<bean id="urlMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/regAction.do">regAction</prop>
				<prop key="/loginAction.do">loginAction</prop>
			</props>
		</property>
	</bean>

	<!-- Action Definition -->
	<bean id="regAction" class="org.lee.springmvc.demo.RegAction">
		<property name="commandClass">
			<value>org.lee.springmvc.demo.RegInfo</value>
		</property>

		<property name="error_view">
			<value>error</value>
		</property>

		<property name="success_view">
			<value>success</value>
		</property>
	</bean>
	<bean id="loginAction" class="org.lee.springmvc.demo.LoginAction">
		<!-- 此类用于提取自动提取表单并填充该类(因此,没有get、set是会报错的),这是非常方便的 -->
		<property name="commandClass">
			<value>org.lee.springmvc.demo.RegInfo</value>
		</property>
		<property name="error_view">
			<value>error</value>
		</property>
		<property name="success_view">
			<value>loginSuccess</value>
		</property>
	</bean>
</beans>

至此,简单解释了配置文件。另附一个spring mvc的demo ,有简单的注册和登录功能(请用Myeclipse打开)。

http://download.csdn.net/detail/feichenwangyalin/4687449

【上篇】
【下篇】

抱歉!评论已关闭.