SpringMVC的初體驗-4

一. Restful風格的URL

  1. URL中不含文件的擴展名

  2. SpringMVC對Restful風格的支持

  • 控制層
@Controller
@RequestMapping("/article")
public class ArticleController {

	@RequestMapping("/list")
	public String list(Model model){
		return "article/list";
	}
	
	@RequestMapping("/details/{id}")
	public ModelAndView details(@PathVariable("id") int id){
		ModelAndView mav=new ModelAndView();
		if(id==1){
			mav.addObject("article", new Article("文章一","文章一的內容"));
		}else if(id==2){
			mav.addObject("article", new Article("文章二","文章二的內容"));
		}
		mav.setViewName("article/details");
		return mav;
	}
}
  • 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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringMvc</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>  Restful風格的URL設置
	</servlet-mapping>
</web-app>

二. Restful風格URL下靜態資源訪問障礙

前邊在配置web.xml時定義的url-pattern給的是/,它會把圖片之類的靜態資源攔截。攔截了靜態資源的SpringMVC又會指定web靜態資源的根(${pageContext.request.contextPath})爲/WEB-INF/jsp/。路徑不對了當然就會導致圖片無法正常顯示。

這裏無法正常顯示
<img alt="文章列表" src="${pageContext.request.contextPath}/resources/article_list.jpg">
這裏無法正常引用css
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources2/css.css"/>

搞清楚原因是圖片之類的靜態資源請求被SpringMVC攔截並映射到了錯誤的資源目錄:

    <!-- 視圖解析器 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp"></property>
	</bean>

解決方案就是在spring-mvc.xml配置文件中添加資源標籤mvc:resources

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

	<!-- 包掃描 -->
    <context:component-scan base-package="com.bee"/>
	
	<!-- 啓用註解 -->
	<mvc:annotation-driven/>
	
	<!-- 重新定義靜態資源的路徑映射 -->
	<mvc:resources mapping="/resources/**" location="/images/"/>
	<mvc:resources mapping="/resources2/**" location="/css/"/>
	
    <!-- 視圖解析器 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

這裏定義的

/images/
/css/
^
|---->  WebContent/images/
|---->  WebContent/css/

三. Eclipse中Web項目靜態資源的根目錄WebContent是怎麼定義出來的

右擊Web項目選擇屬性Properties
在這裏插入圖片描述
這裏的/就是Web項目靜態資源的根目錄。

http://localhost:8080/SpringMVCDemo/helloworld.do
                           ^       ^
                           |       |---> ${pageContext.request.contextPath} 即 /WebContent
                           |---> Context root

右擊Web項目選擇屬性Properties
在這裏插入圖片描述

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