spring MVC配置文件解讀

本人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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章