zoukankan      html  css  js  c++  java
  • SpringBoot的Web开发

    Web开发的自动配置类:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration

    位置:
           
       
      内部使用条件配置:
             @ConditionalOnClass,说明其内部条件是有servlet(说明是一个web工程)、DispatcherSevlet(有SpingMVC)、WebMvcConfigurerAdapter(webMvc适配器),三者是and关系,必须全部都包含。
     
    解释
          
    @ConditionalOnMissingBean({HiddenHttpMethodFilter.class})
    public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
    return new OrderedHiddenHttpMethodFilter();
    }
     
    @ConditionalOnMissingBean判断是否有这个bean,如果有就正常执行,没有这里创建一个

     
    自带自动配置视图解析器:
          

             ViewResolver

                               

    其前缀和后缀,在view对象中配置。
     
     
     1.进入规则为 /
     
    如果进入SpringMVC的规则为/时,Spring Boot的默认静态资源的路径为(建议把资源放入指定路径中去,一般是放入如下文件下去):
    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
     
    不指定资源位置时,也可访问。
     
     
    消息转化器配置:
          自定义消息转化器,只需要在@Configuration的类中添加消息转化器的@bean加入到Spring容器,就会被Spring Boot自动加入到容器中。(两种方法,这里是其一)
                    
     1 package com.example.demo;
     2 
     3 import org.springframework.boot.Banner;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.SpringBootConfiguration;
     6 import org.springframework.boot.autoconfigure.SpringBootApplication;
     7 import org.springframework.context.annotation.Bean;
     8 import org.springframework.http.converter.StringHttpMessageConverter;
     9 import org.springframework.stereotype.Controller;
    10 import org.springframework.web.bind.annotation.RequestMapping;
    11 import org.springframework.web.bind.annotation.ResponseBody;
    12 
    13 import java.nio.charset.Charset;
    14 
    15 
    16 @Controller
    17 @SpringBootApplication
    18 @SpringBootConfiguration
    19 public class Application {
    20 
    21     @RequestMapping("hello")
    22     @ResponseBody
    23     public String hello() {
    24         return "hello world!中国最好的人就是我";
    25     }
    26 
    27     public Application() {
    28     }
    29 
    30     /**
    31      * 自定义消息装换器
    32      *
    33      * @return
    34      */
    35     @Bean
    36     public StringHttpMessageConverter stringHttpMessageConverter() {
    37         StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("ISO-8859-1"));
    38         return converter;
    39     }
    40 
    41 
    42     public static void main(String[] args) {
    43         SpringApplication springApplication = new SpringApplication(Application.class);
    44         springApplication.setBannerMode(Banner.Mode.OFF);
    45         springApplication.run(args);
    46     }
    47 }
    View Code
     
    SpingBoot配置拦截器
     1 package com.example.demo;
     2 import java.nio.charset.Charset;
     3 import java.util.List;
     4 
     5 import javax.servlet.http.HttpServletRequest;
     6 import javax.servlet.http.HttpServletResponse;
     7 
     8 import org.springframework.context.annotation.Configuration;
     9 import org.springframework.http.converter.HttpMessageConverter;
    10 import org.springframework.http.converter.StringHttpMessageConverter;
    11 import org.springframework.web.servlet.HandlerInterceptor;
    12 import org.springframework.web.servlet.ModelAndView;
    13 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    14 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    15 
    16 @Configuration //申明这是一个配置
    17 public class MySrpingMVCConfig extends WebMvcConfigurerAdapter{
    18 
    19     // 自定义拦截器
    20     @Override
    21     public void addInterceptors(InterceptorRegistry registry) {
    22         HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
    23             @Override
    24             public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    25                     throws Exception {
    26                 System.out.println("自定义拦截器............");
    27                 return true;
    28             }
    29             
    30             @Override
    31             public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
    32                     ModelAndView modelAndView) throws Exception {
    33                 
    34             }
    35             
    36             @Override
    37             public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
    38                     Exception ex) throws Exception {
    39             }
    40         };
    41         registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
    42     }
    43 
    44     // 自定义消息转化器的第二种方法
    45     @Override
    46     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    47         StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
    48         converters.add(converter);
    49     }
    50 
    51 }
    View Code
  • 相关阅读:
    [HDOJ3523]Image copy detection
    [HDOJ3526]Computer Assembling
    Ubuntu12.04 配置步骤
    const 详解
    ubuntu 12.04 源
    函数参数和数据成员同名
    友元
    静态数据 成员和静态函数
    成员指针
    内存泄露
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/8729455.html
Copyright © 2011-2022 走看看