導入jackson依賴:javascript
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.0</version> </dependency>
controller(由於要看效果, 因此就直接寫了)css
@ResponseBody @RequestMapping("testJson") public User testJson() { return new User("李四", 24); }
javascripthtml
<script src="js/jquery-3.4.1.min.js"></script> <script type="text/javascript"> $(() => { $("#jsonBtn").click(() => { $.ajax({ url: "resp/testJson", dataType: "JSON", success: (data) => { console.log(data); } }); }); }) </script>
<mvc:default-servlet-handler/>
<input type=」file」 />
springMvc文件上傳須要用到fileupload組件:java
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
編寫頁面jquery
<form action="fileupload" method="post" enctype="multipart/form-data"> file: <input type="file" name="file"/><br> <button type="submit">提交</button> </form>
編寫Controllergit
@RequestMapping("/testFileupload") public String testFileupload(HttpServletRequest request, @RequestParam("upload") MultipartFile file) throws IOException { System.out.println("testFileupload running..."); String path = request.getSession().getServletContext().getRealPath("/upload/"); File upload = new File(path); if (!upload.exists()) { if (!upload.mkdirs()) { throw new RuntimeException("文件夾建立失敗"); } } // 獲取post中的name的值 System.out.println(file.getName()); // 文件的MIME類型 System.out.println(file.getContentType()); // 文件名, 帶後綴 System.out.println(file.getOriginalFilename()); file.transferTo(new File(path + UUID.randomUUID() + file.getOriginalFilename())); return "redirect:/success.jsp"; }
配置解析器github
<!-- 配置文件解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> </bean>
跨服務器文件上傳須要用到的組件的依賴:web
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19.1</version> </dependency>
編寫頁面ajax
<form action="testFileupload2Server" method="post" enctype="multipart/form-data"> file: <input type="file" name="upload"/><br> <button type="submit">提交</button> </form>
編寫Controllerspring
@RequestMapping("/testFileupload2Server") public String testFileupload2Server(HttpServletRequest request, @RequestParam("upload") MultipartFile file) throws IOException { System.out.println("testFileupload2Server running..."); String path = "http://localhost:9090/springMvc02_file/upload/"; // 建立客戶端對象 Client client = Client.create(); // 與圖片服務器鏈接 WebResource resource = client.resource(path + UUID.randomUUID() + file.getOriginalFilename()); // 上傳文件 resource.put(file.getBytes()); return "redirect:/success.jsp"; }
與上面同樣:
<!-- 配置文件解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> </bean>
編寫異常處理器類(實現HandlerExceptionResolver接口便可)
public class ExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) { System.out.println("出異常了"); ModelAndView mv = new ModelAndView(); // 能夠有日誌記錄的相關操做, 而後轉發(重定向)到指定頁面 mv.setViewName("error"); return mv; } }
配置異常處理器到spring容器中
<bean id="exceptionResolver" class="cn.ann.web.resolver.ExceptionResolver"/>
配置攔截器
<mvc:interceptors> <mvc:interceptor> <!-- 配置攔截什麼 --> <mvc:mapping path="/**"/> <!-- 配置不攔截什麼 --> <!-- <mvc:exclude-mapping path=""/> --> <!-- 指定自定義攔截器對象 --> <bean class="cn.ann.web.interceptor.MyInterceptor"/> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <!-- <mvc:exclude-mapping path=""/> --> <bean class="cn.ann.web.interceptor.MyInterceptor2"/> </mvc:interceptor> </mvc:interceptors>
執行順序
本文代碼: 此處的 springMvc02