Spring Boot 零散内容

配置Servlet

import com.google.code.kaptcha.servlet.KaptchaServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MvcConfiguration {

    // Kaptcha 配置
    @Value("kaptcha.textproducer.font.color")
    private String fontColor;
    @Value("kaptcha.textproducer.font.size")
    private String fontSize;
    @Value("kaptcha.textproducer.font.names")
    private String fontName;
    @Value("kaptcha.image.height")
    private String imageHeight;
    @Value("kaptcha.image.width")
    private String imageWidth;
    @Value("kaptcha.textproducer.char.string")
    private String charString;
    @Value("kaptcha.textproducer.char.length")
    private String charLength;
    @Value("kaptcha.border")
    private String border;
    @Value("kaptcha.noise.color")
    private String noiseColor;

    @Bean
    public ServletRegistrationBean kaptchaServlet() {
        ServletRegistrationBean kaptchaServlet = new ServletRegistrationBean(new KaptchaServlet(), "/Kaptcha");
        kaptchaServlet.addInitParameter("kaptcha.textproducer.font.color", fontColor);
        kaptchaServlet.addInitParameter("kaptcha.textproducer.font.size", fontSize);
        kaptchaServlet.addInitParameter("kaptcha.textproducer.font.names", fontName);
        kaptchaServlet.addInitParameter("kaptcha.image.height",imageHeight);
        kaptchaServlet.addInitParameter("kaptcha.image.width", imageWidth);
        kaptchaServlet.addInitParameter("kaptcha.textproducer.char.string", charString);
        kaptchaServlet.addInitParameter("kaptcha.textproducer.char.length", charLength);
        kaptchaServlet.addInitParameter("kaptcha.border", border);
        kaptchaServlet.addInitParameter("kaptcha.noise.color", noiseColor);
        return kaptchaServlet;
    }

}

配置ViewResolver

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;


@Configuration
@EnableWebMvc // 会帮我们开启一些web mvc的默认配置:比如viewResolver或者messageConverter
public class MyConfiguration{
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/views/");//这是编译后jsp的路径
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }
}

访问本地图片

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class MvcConfiguration implements WebMvcConfigurer{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/tdog/**").addResourceLocations("file:/Users/baozi/temp-doc/tdog/");
    }
}

Spring Boot 热加载


    org.springframework.boot
    spring-boot-devtools
    true

 
      
         
            org.springframework.boot
            spring-boot-maven-plugin
            
               true 
            
         
      
   

你可能感兴趣的:(Spring Boot 零散内容)