Springboot-Web开发的一些基础问题

本文介绍了SpringBoot在Web开发中的基础问题,包括静态资源的导入(优先级:resources > static > public),首页定制(将index.html放置在指定路径下),模板引擎Thymeleaf的使用(导入依赖,将HTML放在templates目录下,以及值的传递和展示),以及SpringMVC的扩展。通过这些内容,帮助开发者理解SpringBoot的基础配置和操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot-Web开发

要解决的问题:
1)导入静态资源
2)首页定制
3)jsp,模板引擎 Thymeleaf
4)装配扩展SpringMVC
5) 增删改查
6) 国际化
7) 拦截器

静态资源导入

在类public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer

public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};


在这里插入图片描述

可以直接访问
在这里插入图片描述

优先级 resources>static(默认)>public

总结,在springboot中有以下方式可以处理静态资源
1)webjars (基本不用) localhost:8080/webjars/
2)/**,/resources,/static,/public localhost:8080/

首页如何定制

public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware类中

@Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
            WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
            welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
            welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
            return welcomePageHandlerMapping;
        }

        private Optional<Resource> getWelcomePage() {
            String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
            return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
        }

        private Resource getIndexHtml(String location) {
            return this.resourceLoader.getResource(location + "index.html");
        }

可以看出,命名一个index.html放在/resources,/static,/public 任一路径下即为首页

直接访问http://localhost:8080/

在这里插入图片描述

模板引擎

以前的jsp
在这里插入图片描述
现在我们使用thymeleaf
首先导入依赖

<!--Thymeleaf-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

在这里插入图片描述
结论:只要需要使用thymeleaf,只需要导入对应的依赖就可以了!我们将html放在我们的templates目录下即可!
传值

@Controller
public class hellocontroller {


    @GetMapping("/hello")
    public String hello(Model model){
        model.addAttribute("msg","Hi,你好");
        model.addAttribute("list", Arrays.asList("wangli","tianyou"));
        return "hello";
    }
}

取值

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--html中所有元素都可以被thymeleaf接管   th:text  th:class-->
    <div th:text="${msg}" th:id="div1"></div>

    <h3 th:each=" temp:${list}" th:text="${temp}"></h3>
</body>
</html>

展示
在这里插入图片描述

扩展MVC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值