SpringMVC對Rest風格的支援和靜態資原始檔的引用
阿新 • • 發佈:2019-01-07
什麼是rest風格?簡單的來講,請看csdn每一篇部落格的地址,這種沒有類似.html結尾,.jsp或者說引數的都是rest風格的。如下圖:
那怎麼來實現rest風格的呢,首先應該攔截所有的請求。然後在controller類中編寫相關的對映。在寫對映地址的時候,就像之前我們寫一些什麼action=什麼的時候,我們把那個隱藏了。比如本來是list.do,我們攔截了所有的請求和就可以把那個list.do訪問變成list訪問。他們的訪問效果是一樣的。如下圖:
這個只是最簡單的rest風格體現,之前在struts2中的action和這種差不多。
由於springMVC攔截了所有的請求,導致我們本地上請求不到一些諸如圖片的請求。這就需要進行相關的對映,springmvc.xml配置如下:
<mvc:resources mapping="/resources/**" location="/images/"/>的作用是把images目錄下的檔案對映成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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 使用註解的包,包括子集 --> <context:component-scan base-package="com.levi"/> <!-- 檢視解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"></property> </bean> <!-- 註解驅動--> <mvc:annotation-driven/> <!--靜態資源對映,不然由於springmvc攔截了所有請求。會訪問不到圖片等靜態資源--> <mvc:resources mapping="/resources/**" location="/images/"/> </beans>
目錄下的。所以在訪問圖片的時候,應該把images改為resourse。比如圖片的路徑本來是/images/article_list.jpg
就要改成/resources/article_list.jpg。