zoukankan      html  css  js  c++  java
  • springboot07

    会为tomcat创建一个临时文件目录,如: 
    /tmp/tomcat.2233614112516545210.8080,做为tomcat的basedir。里面会放tomcat的临时文件,比如work目录。

    还会初始化Tomcat的一些Servlet,比如比较重要的default/jsp servlet:

    private void addDefaultServlet(Context context) {
        Wrapper defaultServlet = context.createWrapper();
        defaultServlet.setName("default");
        defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
        defaultServlet.addInitParameter("debug", "0");
        defaultServlet.addInitParameter("listings", "false");
        defaultServlet.setLoadOnStartup(1);
        // Otherwise the default location of a Spring DispatcherServlet cannot be set
        defaultServlet.setOverridable(true);
        context.addChild(defaultServlet);
        context.addServletMapping("/", "default");
    }
    
    private void addJspServlet(Context context) {
        Wrapper jspServlet = context.createWrapper();
        jspServlet.setName("jsp");
        jspServlet.setServletClass(getJspServletClassName());
        jspServlet.addInitParameter("fork", "false");
        jspServlet.setLoadOnStartup(3);
        context.addChild(jspServlet);
        context.addServletMapping("*.jsp", "jsp");
        context.addServletMapping("*.jspx", "jsp");
    }

    spring boot的web应用如何访问Resource

    当spring boot应用被打包为一个fat jar时,是如何访问到web resource的?

    实际上是通过Archive提供的URL,然后通过Classloader提供的访问classpath resource的能力来实现的。

    index.html

    比如需要配置一个index.html,这个可以直接放在代码里的src/main/resources/static目录下。

    对于index.html欢迎页,spring boot在初始化时,就会创建一个ViewController来处理:

    //ResourceProperties
    public class ResourceProperties implements ResourceLoaderAware {
    
        private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
    
        private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
                "classpath:/META-INF/resources/", "classpath:/resources/",
                "classpath:/static/", "classpath:/public/" };
    
    • 1
    //WebMvcAutoConfigurationAdapter
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                Resource page = this.resourceProperties.getWelcomePage();
                if (page != null) {
                    logger.info("Adding welcome page: " + page);
                    registry.addViewController("/").setViewName("forward:index.html");
                }
            }
    • 1

    template

    像页面模板文件可以放在src/main/resources/template目录下。但这个实际上是模板的实现类自己处理的。比如ThymeleafProperties类里的:

    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    • 1

    jsp

    jsp页面和template类似。实际上是通过spring mvc内置的JstlView来处理的。

    可以通过配置spring.view.prefix来设定jsp页面的目录:

    spring.view.prefix: /WEB-INF/jsp/
    • 1

    spring boot里统一的错误页面的处理

    对于错误页面,Spring boot也是通过创建一个BasicErrorController来统一处理的。

    @Controller
    @RequestMapping("${server.error.path:${error.path:/error}}")
    public class BasicErrorController extends AbstractErrorController 
    

    对应的View是一个简单的HTML提醒:

        @Configuration
        @ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)
        @Conditional(ErrorTemplateMissingCondition.class)
        protected static class WhitelabelErrorViewConfiguration {
    
            private final SpelView defaultErrorView = new SpelView(
                    "<html><body><h1>Whitelabel Error Page</h1>"
                            + "<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>"
                            + "<div id='created'>${timestamp}</div>"
                            + "<div>There was an unexpected error (type=${error}, status=${status}).</div>"
                            + "<div>${message}</div></body></html>");
    
            @Bean(name = "error")
            @ConditionalOnMissingBean(name = "error")
            public View defaultErrorView() {
                return this.defaultErrorView;
            }
  • 相关阅读:
    laravel中get方式表单提交后, 地址栏数据重复的问题
    laravel中firstOrCreate的使用
    表单使用clone方法后, 原有select无法生效
    浏览器提示内容编码错误, 不支持此压缩格式
    web端访问文件没有权限的问题
    $(this)在ajax里面不生效的探究
    如何在github上提交pr
    IDEA配置文件的配置文件配置
    quartz定时任务时间设置
    通过 EXPLAIN 分析低效 SQL 的执行计划
  • 原文地址:https://www.cnblogs.com/huaobin/p/14908813.html
Copyright © 2011-2022 走看看