一、Restful风格的资源URL:
这个图片中的地址是我的一篇博客的地址,可以看到这个地址的后面没有什么后缀。查看前面的博客可以发现,之前博客里面的示例工程中的网页地址都是有后缀的,比如.do,.jsp等。向这种没有后缀的URL地址就是Restful风格的URL,这种风格很优雅,但是也有一些问题。还有的URL地址他们的后缀统一是.html或者.htm,这种虽然不是严格意义上的Restful风格,但是这种是常用的一种方式。
二、SpringMvc对Restful风格的支持
1.新建工程SpringMVC03,按照前面的配置配置好SpringMvc。
2.新建类Article:
package com.test.model;
public class Article {
private int id;
private String title;
private String content;
public Article(String title, String content) {
super();
this.title = title;
this.content = content;
}
public Article() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
3.新建控制层ArticleController:
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.test.model.Article;
@Controller
@RequestMapping("/article")
public class ArticleController {
@RequestMapping("/list")
public String list(Model model){
return "article/list";
}
}
这里新建了一个list方法,方法返回一个字符串。按照返回的内容,需要在WEB-INFO/jsp文件夹下新建文件夹article。
4.既然是Restful风格的URL,那么就必须要拦截所有的请求,而不仅仅是拦截*.do请求:
<?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>SpringMVC01</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>
</servlet-mapping>
</web-app>
这里修改了配置文件web.xml,改成了拦截所有请求。
5.在article文件夹下新建list.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<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>
</html>
6.先测试一下,运行程序:
可以看到这就是一个Restful风格的请求。按照配置,SpringMvc会拦截所有请求,这个请求被拦截后经过SpringMvc的转发器转发到ArticleController的list方法中,然后重定向到list页面。
7.点击文章标题,跳转到文章内容页面。ArticleController类中新建处理方法:
@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(),使用这个注解后可以获取到URL的变量值。这里使用的是@PathVariable("id"),表示取URL中变量id的值,付给形参id。跳转到article文件夹下details.jsp页面。
8.新建details.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/resources2/css.css"/>
</head>
<body>
<p>${article.title }</p>
<p>${article.content }</p>
</body>
</html>
9.运行程序,点击文章一的超链接:
可以看到这里也是restful风格的url地址,并且跳转到了文章的内容。
10.修改list.jsp为:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<th colspan="2">
<img alt="文章列表" src="${pageContext.request.contextPath }/images/article_list.jpg">
</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>
</html>
这里不是使用文字了,而是加载了一张图片。
运行程序:
可以看到这里并没有把图片加载出来。控制台输出警告没有与/images/article_list.jpg请求匹配的映射。
出现这个问题的原因是web.xml文件中我们配置了拦截所有请求,包括什么图片、样式等的请求都会被分发处理。但是图片等这种是静态资源,这种请求静态资源的请求是不需要处理的。
SpringMvc提出的解决这个问题的方法是使用静态资源标签。
11.要想使用静态资源标签,需要添加新的配置。修改配置文件spring-mvc.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/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.test"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/images/"/>
<!-- 视图解析器 -->
<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>
这里添加了注解驱动。<mvc:resources mapping="/resources/**" location="/images/"/>把resources映射成images。
修改list.jsp为:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<th colspan="2">
<img alt="文章列表" src="${pageContext.request.contextPath }/resources/article_list.jpg">
</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>
</html>
这里把加载图片的地址中的images改成了resources。当请求resources时,SpringMvc会进行过滤,把这个请求当成静态资源处理。根据配置,又会把resources映射成images,从而能够找到图片的正确位置。
12.运行程序:
图片加载成功。这里把这个图片当成了静态资源处理。
13.不仅对图片资源这样处理,对于css、js文件也要这么处理。WebContent中新建css文件夹,并新建css.css文件:
.p1{
font-size:20px;
font-weight: bold;
}
14.这里添加了一个css文件,也要进行类似的处理,spring-mvc.xml文件中添加映射:
<mvc:resources mapping="/resources2/**" location="/css/"/>
15.修改details.jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/resources2/css.css"/>
</head>
<body>
<p class="p1">${article.title }</p>
<p>${article.content }</p>
</body>
</html>
这里引入了css.css文件,对文章标题引入css特性。
运行程序,点击文章一的超链接:
可以看到这里标题发生了变化。
总结:这篇博客讲述了springMVC对restful风格URL的支持,以及这种风格中的问题和解放方法。
真正的项目中不推荐使用这种方式,推荐使用把所有的URL地址改成.html或者.htm为后缀的格式(配置文件中配置成拦截.html的请求)。这样的话就不会出现上面的那些问题了。