1. 程式人生 > >springmvc resources 讀取配置靜態檔案目錄

springmvc resources 讀取配置靜態檔案目錄

annomvc-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:p="http://www.springframework.org/schema/p"
	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-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<span style="color:#ff0000;"><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath*:system.properties</value>
			</list>
		</property>
	</bean>
	
	<context:component-scan base-package="xxxx.xxx" />
	
	<!-- 靜態資源對映配置,目前支援	*.json,*.css,*.js,*.ico,*.swf,*.gif,*.jpeg,*.bmp,*.jpg,*.png
		 若需新增,請在web.xml裡面配置相應路徑 -->
	<!-- 開發環境:路徑由開發者自行修改env.properties裡面的nas.root.dir -->
	<mvc:resources location="file:${nas.root.dir}/common/" mapping="/common/**"/>
	<mvc:resources location="file:${nas.root.dir}/pages/" mapping="/pages/**" /></span>
	
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

	<!-- 配置json -->
	<bean id="messageConverters" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
	<!-- 啟動 Spring MVC 的註解功能,完成請求和註解 POJO 的對映 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters" ref="messageConverters" />
	</bean>

	<!-- 配置試圖解析 -->
	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="contentNegotiationManager">
	     <bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
	     	<property name="mediaTypes">
	     		<props>
	     			<prop key="atom">application/atom+xml</prop>
	     			<prop key="html">text/html</prop>
	     			<prop key="json">application/json</prop>
	     		</props>
		</property>
	     </bean>
		</property>
		<property name="viewResolvers">
			<list>
				<!-- 對模型檢視名稱的解析,即在模型檢視名稱新增前後綴 -->
				<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
			</list>
		</property>
	</bean>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/pages/" p:suffix=".jsp" />
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/pages/" p:suffix=".html" />
		
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	...
	<servlet>
		<servlet-name>annomvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>annomvc</servlet-name>
		<url-pattern>*.shtml</url-pattern>
		<url-pattern>*.do</url-pattern>
		<url-pattern>/external/investinfosync</url-pattern>
		<!-- 本地靜態資源對映配置,與以下相同字尾的請求地址需要注意,千萬不能重複 -->
        <url-pattern>*.json</url-pattern>
		<url-pattern>*.css</url-pattern>
		<url-pattern>*.js</url-pattern>
		<url-pattern>*.ico</url-pattern>
        <url-pattern>*.swf</url-pattern>
        <url-pattern>*.gif</url-pattern>
        <url-pattern>*.jpeg</url-pattern>
        <url-pattern>*.bmp</url-pattern>
        <url-pattern>*.jpg</url-pattern>
        <url-pattern>*.png</url-pattern>
	</servlet-mapping>
	...
</web-app>