Restful 风格的资源 URL 简介
https://blog.csdn.net/a909301740/article/details/80587580
SpringMVC对rest风格的支持
Article.java
int id、String title、String content、get和set、无参构造、有参构造、
web.xml
对springmvc的配置中,url-pattern不再是*.do“”,因为要做rest风格的,要请求所有的、
<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> <!-- 此处将*.do改成/ -->
</servlet-mapping>
spring-mvc.xml
除了配置使用注解的包和视图解析器外,要将静态资源路径映射成指定的路径、
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 将images映射成resources -->
<mvc:resources mapping="/resources/**" location="/images/"/>
<!-- 将css映射成resources2 -->
<mvc:resources mapping="/resources2/**" location="/css/"/>
ArticleController
@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){ //@PathVariable获取url变量值
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;
}
}
list.jsp
<!-- 以下三行可以放在table中 -->
<img alt="文章列表" src="${pageContext.request.contextPath}/resources/article_list.jpg">
<a href="${pageContext.request.contextPath}/article/details/1" target="_blank">文章一</a>
<a href="${pageContext.request.contextPath}/article/details/2" target="_blank">文章二</a>
details.jsp
<!-- 在head中引入 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources2/css.css"/>
<!-- body中 -->
<p class="p1">${article.title }</p>
<p>${article.content }</p>