no3-springmvc核心基础

飞一样的编程
飞一样的编程
擅长邻域:Java,MySQL,Linux,nginx,springboot,mongodb,微信小程序,vue

分类: springboot 专栏: springboot学习 标签: springmvc

2023-03-14 16:07:47 756浏览

springmvc

1. springmvc概述

2.springMVC开发实战

先采用之前xml的方式写一遍,然后再用注解的方式写一遍

返回页面-走视图解析器

自动装配类

//java spi(自动装配) 项目启动的时候就拉起springmvc框架
public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) {

        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AppConfig.class);

        // Create and register the DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(context);
        ServletRegistration.Dynamic registration = servletContext.addServlet("springmvc", servlet);
        registration.setLoadOnStartup(0);
        registration.addMapping("/");
    }
}
@Configuration //相当于applicationContext.xml
@ComponentScan("com.c55.ch02class.controller")
public class AppConfig {
    @Bean
    public InternalResourceViewResolver  internalResourceViewResolver(){
        InternalResourceViewResolver in = new InternalResourceViewResolver();
        in.setPrefix("/");
        in.setSuffix(".jsp");
        return in;
    }

}

返回json数据

注意引入jackson的jar

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
@EnableWebMvc//相当于<mvc:annotation-driven/>
public class AppConfig  {

采用fastjson的方式配置消息转换器

@Configuration //相当于applicationContext.xml
@ComponentScan("com.c55.ch02class.controller")
@EnableWebMvc//相当于<mvc:annotation-driven/>
public class AppConfig  implements  WebMvcConfigurer{
@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter httpMessageConverter = new StringHttpMessageConverter();
        httpMessageConverter.setDefaultCharset(Charset.forName("utf-8"));
        httpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));

        FastJsonHttpMessageConverter fastJsonHttpMessageConverter= new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));
        converters.add(httpMessageConverter);
        converters.add(fastJsonHttpMessageConverter);
    }

静态资源映射

@Configuration //相当于applicationContext.xml
@ComponentScan("com.c55.ch02class.controller")
@EnableWebMvc//相当于<mvc:annotation-driven/>
public class AppConfig  implements  WebMvcConfigurer{

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

拦截器的配置

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TestIntercept())
                .addPathPatterns("/**").excludePathPatterns("/test/userjson");
    }

文件上传

  @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setMaxUploadSize(10240000);
        commonsMultipartResolver.setDefaultEncoding("UTF-8");
        return commonsMultipartResolver;
    }

好博客就要一起分享哦!分享海报

此处可发布评论

评论(1展开评论

蓝色妖姬 能力:10

2023-03-20 10:48:01

点击查看更多评论

展开评论

您可能感兴趣的博客

客服QQ 1913284695