java项目配置层_springMvc项目的搭建,暂时没有整合持久层框架(java Config配置对比xml配置)...

public class WebInit implements WebApplicationInitializer {

@Override

public void onStartup(ServletContext container) throws ServletException {

//项目启动则执行 :

//前端控制器

//对比xml配置,配置在web.xml中的

/*

*

* dispatcherServlet

* org.springframework.web.servlet.DispatcherServlet

*

* contextConfigLocation

* /WEB-INF/classes/application.xml

*

* 1

*

*

* dispatherServlet

* *.do

*

* */

//javaConfig配置

//这句相当于控制器核心类

ServletRegistration.Dynamic dispatcherServletRegistration = container.addServlet("dispatcher", new DispatcherServlet());

//这句相当于配置服务器启动就加载servlet容器

dispatcherServletRegistration.setLoadOnStartup(1);

//这句相当于配置注解驱动

dispatcherServletRegistration.setInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");

//这句相当于加载springmvc核心配置文件

dispatcherServletRegistration.setInitParameter("contextConfigLocation", "com.mike.small.config.SpringMvcConfig");

//拦截所有url

dispatcherServletRegistration.addMapping("/");

//处理乱码过滤器

CharacterEncodingFilter filter = new CharacterEncodingFilter();

filter.setEncoding("UTF-8");

FilterRegistration.Dynamic characterEncodingFilterRegistration = container.addFilter("characterEncodingFilter", filter);

characterEncodingFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");

//注解驱动类

AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

rootContext.register(SpringMvcConfig.class);

//这句相当于web.xml的监听器

/*

org.springframework.web.context.ContextLoaderListener

*/

container.addListener(new ContextLoaderListener(rootContext));

}

}

@Configuration // this class contains bean definitions

@EnableWebMvc // same as

@ComponentScan(basePackages = {"com.mike.small"})

public class SpringMvcConfig extends WebMvcConfigurerAdapter {

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

// declare static resources

registry.addResourceHandler("/static/**").addResourceLocations("/static/");

}

@Bean

public InternalResourceViewResolver viewResolver() {

// view resolver

InternalResourceViewResolver resolver = new InternalResourceViewResolver();

resolver.setPrefix("/WEB-INF/jsp/");

resolver.setSuffix(".jsp");

return resolver;

}

@Bean

public CommonsMultipartResolver multipartResolver() {

CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();

multipartResolver.setMaxUploadSize(104857600);

return multipartResolver;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的springmvc-config.xml配置文件的例子: ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.example.controller"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置静态资源处理 --> <mvc:resources mapping="/static/**" location="/static/"/> <!-- 配置RequestMappingHandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <!-- 配置RequestMappingHandlerMapping --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> </beans> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值