导入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