什么是rest风格?简单的来讲,请看csdn每一篇博客的地址,这种没有类似.html结尾,.jsp或者说参数的都是rest风格的。如下图:
那怎么来实现rest风格的呢,首先应该拦截所有的请求。然后在controller类中编写相关的映射。在写映射地址的时候,就像之前我们写一些什么action=什么的时候,我们把那个隐藏了。比如本来是list.do,我们拦截了所有的请求和就可以把那个list.do访问变成list访问。他们的访问效果是一样的。如下图:
这个只是最简单的rest风格体现,之前在struts2中的action和这种差不多。
由于springMVC拦截了所有的请求,导致我们本地上请求不到一些诸如图片的请求。这就需要进行相关的映射,springmvc.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.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>
<mvc:resources mapping="/resources/**" location="/images/"/>的作用是把images目录下的文件映射成resources目录下的。所以在访问图片的时候,应该把images改为resourse。比如图片的路径本来是/images/article_list.jpg
就要改成/resources/article_list.jpg。