Spring-MVC,帶一小例子

以Spring 3.0作爲例子。也需要導Spring 3.0 Web Libraries

 

一、在web.xml 配置裏還需要加上如下代碼

<!-- Spring MVC的Servlet,在WEB-INF下還要有一個<Servlet名>-servlet.xml的文件 -->	
<servlet>
	<display-name>Spring MVC的控制器</display-name>
	<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>*.do</url-pattern>
</servlet-mapping>

這樣,所有的.do的請求,都會被DispatcherServlet處理。初始化 DispatcherServlet 時,該框架在 web 應用程序WEB-INF 目錄中尋找一個名爲[servlet-名稱]-servlet.xml的文件,並在那裏定義相關的Beans,重寫在全局中定義的任何Beans,像上面的web.xml中的代碼,對應的是dispatcher-servlet.xml;當然也可以使用<init-param>元素,手動指定配置文件的路徑。

 

符:漢字解碼的過濾器,也是配置在web.xml中

<!-- 處理字符集 -->
<filter>
	<description>漢字解碼</description>
	<filter-name>encodingFilter</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>
	<init-param>
		<param-name>forceEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>encodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

 

二、dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 對web包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 -->
	<context:component-scan base-package="org.e276.action" />

	<!-- 對web包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<!-- 對模型視圖名稱的解釋,即在模型視圖名稱添加前後綴 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/page/" p:suffix=".jsp" />

</beans>

 

三、一些常用的註解

1.@Controller:註解標識一個控制器

2.@RequestMapping:註解標記一個訪問的路徑("index.do"),return "index"標記返回視圖(index.jsp)。如果@RequestMapping註解在類級別上,則表示一相對路徑,在方法級別上,則標記訪問的路徑。

3.@CookieValue:獲取Cookie的值

 

四、國際化

在MVC配置文件中,配置國際化屬性文件:

<bean id="messageSource"
	class="org.springframework.context.support.ResourceBundleMessageSource"
	p:basename="message">
</bean>

 

那麼,Spring就會在項目中搜索相關的國際化屬性文件,如:message.properties、message_zh_CN.properties。

在VIEW中,引入Spring標籤:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

使用<spring:message code="key" />調用,即可。

 

如果一種語言,有多個語言文件,可以更改MVC配置文件爲:

<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
	<property name="basenames">
		<list>
			<value>message01</value>
			<value>message02</value>
			<value>message03</value>
		</list>
	</property>
</bean>

 

 五、demo

 E276-Annotation.zip

發佈了54 篇原創文章 · 獲贊 0 · 訪問量 2973
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章