知识点:css
咱们平时看到的spring项目请求都是*.do的,可是像下面这两个网址同样,咱们能够去掉.do,这样看起来就比较清爽。第一个是比较明显的REST风格URL,显示的网址没有后缀,第二种其实也算是一种REST风格URL。java
效果预览:能够看到地址栏上的url已经没有.do了。web
再点击"文章一"可见:直接用文章的id显示文章的地址。spring
首先配置web.xml文件,为全部的地址请求spring拦截。spring-mvc
<servlet>mvc
<servlet-name>springmvc</servlet-name>app
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>jsp
<init-param>测试
<param-name>contextConfigLocation</param-name>url
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<?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"/>
<!-- 视图解析器 -->
<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>
@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;
}
}
注解:@PathVariable和@RequestParam,从名字上就能够看出来,他们分别是从路径里面去获取变量,也就是把路径当作变量,后者是从请求里面获取参数。
<body>
<table>
<tr>
<th colspan="2">
文章列表
</th>
</tr>
<tr>
<td>1</td>
<td>
<a href="${pageContext.request.contextPath}/article/details/1" target="_blank">文章一</a>
</td>
</tr>
<tr>
<td>2</td>
<td>
<a href="${pageContext.request.contextPath}/article/details/2" target="_blank">文章二</a>
</td>
</tr>
</table>
</body>
注解:这里将文章一的路径写成/details/1,而后controller中就将1这个参数绑定到id上,再经过@PathVariable获取这个1。
<body>
<p>${article.title }</p>
<p>${article.content }</p>
</body>
上面的DEMO是在没有静态资源的状况下的rest风格,可是实际状况下是有的,通常js,css,img,都会有,在上面的demo中,若是添加图片或者其余东西是行不通的,由于在web.xml中将全部的请求都添加了过滤器。这个时候就须要咱们对静态资源作一步处理了。
Spring对静态资源的处理是经过<mvc:resources …来处理,具体解释就本身百度。
在上述DEMO的基础上添加以下代码:
<mvc:resources mapping="/resources/**" location="/images/"/>
<mvc:resources mapping="/resources2/**" location="/css/"/>
而后添加img和css, 这里我添加一个img图,和一个类css.具体css以下图代码:
而后修改list.jsp。将图片引用过来。
接着为details.jsp的文章内容,添加css.
测试:,
能够看到图片显示出来了,若是没有对静态资源进行处理的话,是不会显示的。而后再点击文章一,也能够看到文章一的标题是有变化了的。
好记性不如烂笔头,菜鸟边学边把学到的东西记录下来。