Spring-web项目-纯java配置

创建mavn-web项目

引入依赖

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.1.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>

spring的配置文件(applicationContext.xml)

@Configuration
@ComponentScan(basePackages = "com.nq",
        useDefaultFilters = true,
        excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,
                classes = Controller.class)})
public class SpringConfig {

}

springmvc配置文件

@Configuration
@ComponentScan(basePackages = "com.nq",
        useDefaultFilters = false,
        includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,
                classes = Controller.class)})
public class SpringMVCConfig {
}

配置替代Web.xml

主要实现WebApplicationInitializer接口,重写onStartup方法

public class WebInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        // 替代之前的扫描mvc配置文件
        ctx.register(SpringMVCConfig.class);
        // 配置DispatcherServlet
        ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet());
        springmvc.addMapping("/");
        springmvc.setLoadOnStartup(1); // 启动时加载
    }
}

在web.xml配置中只配置了springmvc文件,而spring容器并未加载,

解决方案:直接扫描配置,舍弃spring配置

@Configuration
@ComponentScan(basePackages = "com.nq")
public class SpringMVCConfig {
}

配置静态资源

@Configuration
@ComponentScan(basePackages = "com.nq")
public class SpringMVCConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations(("classpath:/"))
    }
}

配置视图解析器

/**
 * 视图解析器
 * @param registry
 */
@Override
protected void configureViewResolvers(ViewResolverRegistry registry) {
    registry.jsp("/jsp",".jsp");
}

路径映射

@Override
protected void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/test").setViewName("test");
}

添加json配置

@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(Charset.forName("UTF-8"));
    converter.setFastJsonConfig(fastJsonConfig);
    converters.add(converter);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AlfredNing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值