博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC小结
阅读量:6257 次
发布时间:2019-06-22

本文共 6640 字,大约阅读时间需要 22 分钟。

Spring MVC项目搭建

添加依赖

(省略)

Spring MVC配置类

@Configuration@EnableWebMvc@ComponentScan("com.sjx.springmvc")public class MyMvcConfig extends WebMvcConfigurerAdapter{    @Bean    public InternalResourceViewResolver viewResolver(){        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();        viewResolver.setPrefix("/WEB-INF/classes/views/");        viewResolver.setSuffix(".jsp");        viewResolver.setViewClass(JstlView.class);        return viewResolver;    }}

Web配置,实现WebApplicationInitializer接口代替web.xml文件

public class WebInitializer implements WebApplicationInitializer {    @Override    public void onStartup(ServletContext servletContext) throws ServletException {        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();        ctx.register(MyMvcConfig.class);        ctx.setServletContext(servletContext);        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));        servlet.addMapping("/");        servlet.setLoadOnStartup(1);    }}

Spring MVC的常用注解

  • @controller

    @controller注解在类上,表明这个类为Spring MVC的Controller,并讲Web请求映射到注解了@RequestMapping的方法上

  • @RequestMapping

    @RequestMapping是用来映射Web请求的的方法,可以注解在类上,方法上的@RequestMapping路径会继承类上的路基,并且produces属性可以设置请求的媒体类型

  • @ResponseBody

    @ResponseBody可以注解类上也可注解在方法上,支持把返回值放入response体内,而不是返回一个页面

  • @RequestBody

    @RequestBody注解在参数前,允许请求的参数在request体中,而不是直接放在链接地址的后面

  • @PathVariable

    @PathVariable用来接受路劲参数,次注解用在参数前面,如pathvar/{str},可接受参数str

  • @RestController

    @RestController是一个组合注解,是@Controller和@ResponseBody的组合

@Controller@RequestMapping("/anno")public class DemoAnnoController {    @RequestMapping(produces = "text/plain;charset=UTF-8")    public @ResponseBody String index(HttpServletRequest request) {        return "url" + request.getRequestURI() + "can access";    }    // http://localhost:8080/springmvc1/anno/pathvar/sjx@RequestMapping(value = "pathvar/{str}", produces = "text/plain;charset=UTF-8")    public @ResponseBody String demoPathVar(@PathVariable String str, HttpServletRequest request) {        return "url" + request.getRequestURI() + " can access str:" + str;    }    // http://localhost:8080/springmvc1/anno/requestParam?id=25@RequestMapping(value = "/requestParam", produces = "text/plain;charset=UTF-8")    public @ResponseBody String passRequestParam(Long id, HttpServletRequest request) {        return "url" + request.getRequestURI() + " can access id:" + id;    }    @RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8")    @ResponseBodypublic String passObj(DemoObj obj, HttpServletRequest request) {        return "url" + request.getRequestURI() + " can access, obj id:" + obj.getId() + "  obj name:" + obj.getName();    }    @RequestMapping(value = { "/name1", "/name2" }, produces = "text/plain;charset=UTF-8")    public @ResponseBody String remove(HttpServletRequest request) {        return "url: " + request.getRequestURL() + " can access";    }}

Spring MVC的基本配置

拦截器配置

  • 拦截器类
    1. 继承HandlerInterceptorAdapter实现自定义拦截器
    2. 重写preHandle方法,请求发生时执行
    3. 重写postHandle方法,请求结束是执行
public class DemoInterceptor extends HandlerInterceptorAdapter {    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {        long startTime = System.currentTimeMillis();        request.setAttribute("startTime", startTime);        return true;    }    @Override    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {        Long startTime = (Long) request.getAttribute("startTime");        Long currentTime = System.currentTimeMillis();        long handlerTime = currentTime - startTime;        System.out.println("一次请求的时间为" + handlerTime + "ms");        request.setAttribute("handlerTime", handlerTime);    }}
  • 配置
    1. 配置拦截器的Bean
    2. 重写addInterceptors(InterceptorRegistry registry)方法,添加拦截器
@Configuration@EnableWebMvc@ComponentScan("com.sjx.springmvc")public class MyMvcConfig extends WebMvcConfigurerAdapter{    @Bean    public InternalResourceViewResolver viewResolver(){        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();        viewResolver.setPrefix("/WEB-INF/classes/views/");        viewResolver.setSuffix(".jsp");        viewResolver.setViewClass(JstlView.class);        return viewResolver;    }    //自定义拦截器    @Bean    public DemoInterceptor handlerTimeInterceptor(){        return new DemoInterceptor();    }    //添加拦截器    @Override    public void addInterceptors(InterceptorRegistry registry) {        registry.addInterceptor(handlerTimeInterceptor());    }}

@ControllerAdvice

使用@ControllerAdvice注解可以将对于控制器的全局配置放在一起

  • @ExceptionHandler 用于处理全局处理器中的异常
  • @ModelAttribute 本来是用于将键值对绑定到Model里,这里用于让全局的@RequestMapping都能接收到此键值对
//声明一个全局通知  此注解包含了@Component@ControllerAdvicepublic class ExceptionHandlerAdvice {    //拦截所有的异常 显示error界面@ExceptionHandler(value = Exception.class)    public ModelAndView exception(Exception exception, WebRequest request) {        ModelAndView view = new ModelAndView("error");// error界面        view.addObject("errorMessage", exception.getMessage());        return view;    }    //所有用@RequestMapping注解的方法 都能获取这个键值对    @ModelAttribute    public void addAttribute(Model mode) {        mode.addAttribue("msg", "额外信息");    }}

文件上传配置

  • 添加依赖
commons-fileupload
commons-fileupload
1.2
commons-io
commons-io
2.4
  • 上传页面
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>  
文件上传
  • 添加upLoad的ViewController
//集中处理跳转页面@Overridepublic void addViewControllers(ViewControllerRegistry registry) {    registry.addViewController("/toUpload").setViewName("/upload");}
  • 配置MultipartResolver
//配置MultipartResolver,用MultipartFile 接受文件上传@Beanpublic MultipartResolver multipartResolver(){    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();    multipartResolver.setMaxUploadSize(1000000);    return multipartResolver;}
  • 控制器
@Controllerpublic class UploadController {    @RequestMapping(value = "upload", method = RequestMethod.POST)    public @ResponseBody String upload(MultipartFile file) {        try {            // 使用 commons-io包实现快读文件写入磁盘            FileUtils.writeByteArrayToFile(new File("e:/upload/" + file.getOriginalFilename()), file.getBytes());            return "success";        } catch (IOException e) {            e.printStackTrace();            return "fail";        }    }}
来源:

转载地址:http://ykxsa.baihongyu.com/

你可能感兴趣的文章
智能手表ticwatch穿戴体验
查看>>
暑假第五周总结(2018.8.6-8.12)
查看>>
MFC下拉框Combo Box
查看>>
TCP带外数据读写
查看>>
uni-app采坑记录
查看>>
TP方法中打印地址栏中所有的参数:
查看>>
这是一个蒟蒻的计划……QAQ
查看>>
设置局域网共享文件不需要用户名密码
查看>>
raft--分布式一致性协议
查看>>
Solidity notes
查看>>
网上购物系统(Task005)——通用数据库访问函数集SqlHelper类
查看>>
java 单例模式浅析
查看>>
Codeforces Round #389 (Div. 2,) B C
查看>>
python中configparser模块记录
查看>>
IIIDX[九省联考2018]
查看>>
Protobuf3 序列化
查看>>
C语言面试题大汇总
查看>>
JavaSE-List常用方法
查看>>
json 和 pickel 详解
查看>>
Linux基础命令之grep
查看>>