SpringMVC(二)
第三章 Rest风格的资源 URL
第一节:Restful风格的资源 URL简介
第二节:SpringMvc对Rest风格的支持
第三节:@PathVariable获取 Url 变量
第四节:SpringMvc对静态资源的处理
图中csdn的地址就是Restful风格的,在web.xml中设置需要拦截所有请求, 这样也会把静态资源拦截掉,需要特别处理。
<mvc:resources mapping="/resources/**" location="/images/"/> 把图片的路径换成映射路径(css,js等引入文件都需要做处理,特别麻烦)。
原来的路径(会被DispatcherServlet拦截,报找不到映射路径的错误):<img alt="文章列表" src="${pageContext.request.contextPath}/images/article_list.jpg">
映射路径:<img alt="文章列表" src="${pageContext.request.contextPath}/resources/article_list.jpg">

cnblogs的可以说是仿Restful风格的,只要拦截*.html结尾的请求。这个比较好。


=================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.java1234"/> <mvc:annotation-driven/> <mvc:resources mapping="/resources/**" location="/images/"/> <mvc:resources mapping="/resources2/**" location="/css/"/> <!-- 视图解析器 --> <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> ====================web.xml===================== <?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>
@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; } }
<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> <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>
第一节:SpringMvc单文件上传
第二节:SpringMvc多文件上传
增加文件上传的bean : <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

在测试环境中filePath 获取的路径是tomcat或者eclipse插件部署项目的路径(看你server的设置),所以文件上传后,你刷新upload文件夹发现没有文件,但是在正式环境中没问题的。
@Controller public class FileUploadController { @RequestMapping("/upload") public String uploadFile(@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws Exception{ String filePath=request.getServletContext().getRealPath("/"); System.out.println(filePath); file1.transferTo(new File(filePath+"upload/"+file1.getOriginalFilename())); return "redirect:success.html"; } @RequestMapping("/upload2") public String uploadFiles(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception{ String filePath=request.getServletContext().getRealPath("/"); System.out.println(filePath); for(MultipartFile file:files){ file.transferTo(new File(filePath+"upload/"+file.getOriginalFilename())); } return "redirect:success.html"; } }
<?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" 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"> <!-- 使用注解的包,包括子集 --> <context:component-scan base-package="com.java1234"/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="10000000"/> <!-- 10M大小的文件 --> </bean> </beans>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="upload2.do" method="post" enctype="multipart/form-data"> <table> <tr> <th colspan="2">上传文件</th> </tr> <tr> <td>文件一</td> <td> <input type="file" name="file"/> </td> </tr> <tr> <td>文件二</td> <td> <input type="file" name="file"/> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="上传文件"/> </td> </tr> </table> </form> </body> </html>

浙公网安备 33010602011771号