SpringBoot Web开发探究

SpringBoot Web开发

jar:webapp!

自动装配

springboot到底帮我们配置了什么?我们能不能修改?能修改那些东西?能不能扩展?

  • xxxAutoConfiguraion…向容器中自动配置组件
  • xxxProperties:自动配置类,装配配置文件中自定义的一些内容!

要解决的问题:

  • 导入静态资源
  • 首页
  • jsp,模板引擎Thymeleaf
  • 装配扩展SpringMVC
  • 增删改查
  • 拦截器
  • 国际化!

导入静态资源
在这里插入图片描述

总结

  • 这几个地方呢都可以导入资源,研究源码可以得出结论
  • 在springboot,我们可以使用一下方式处理资源
    webjars localhost:8080/webjars/
    public,static,/**,resources localhost:8080/
  • 优先级:resources>static(默认)>public
  • 一定要学会读源码这些结论都是源码中得出,读源码会让你主动的学习

首页定制

在这里插入图片描述

  • 首页可以放在public,resources,static,templates中但是得注意如果你放在了templates中呢就得从controller中去跳转
  • controller包下面的类indexController
package com.cloud.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//在templates目录下的所有页面,只能通过controller来跳转!
//这个需要模板引擎的支持!thymeleaf
@Controller
public class indexController {
    @RequestMapping ("/a")
    public String index(){
        return "index";
    }
}

图标定制

  • 在新版中图标定制被干掉了

jsp,模板引擎Thymeleaf

  • 导入JRA包
 <!--Thymeleaf都是基于3.x来开发的如果导入2.x版本就会报错需要注意这个问题-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>
  • 项目结构

在这里插入图片描述

  • controller包下面的类indexController类
package com.cloud.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//在templates目录下的所有页面,只能通过controller来跳转!
//这个需要模板引擎的支持!thymeleaf
@Controller
public class indexController {
    @RequestMapping ("/index")
    public String index(){
        return "index";
    }
}

  • 这个也就是我们说的当你把资源放在templates下始是需要导入模板引擎的,导入之后就可以访问了

取值

package com.cloud.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

//在templates目录下的所有页面,只能通过controller来跳转!
//这个需要模板引擎的支持!thymeleaf
@Controller
public class indexController {
    @RequestMapping ("/index")
    public String index(Model model){
        model.addAttribute("a","hello");
        return "index";
    }
}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--所有的html元素都可以被thymeleaf替换接管: th:元素名-->
<div th:text="${a}" >首页</div>
</body>

MVC配置原理

  • 项目结构
    在这里插入图片描述
  • 代码练习
package com.cloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

//如果,你想diy一些定制化的功能,只要写这个组件,然后将它交给springboot,springboot就会帮助我们自动装配!
//扩展 springmvc  dispatchservlet

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    //ViewResolver 实现了视图解析器接口类,我们就可以把它看做视图解析器
    @Bean
    public ViewResolver myViewResolver(){
        return new MyViewResolver();
    }


    //自定义了一个自己的视图解析器MyViewResolver
    public static class MyViewResolver implements ViewResolver{

        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }
}

扩展SpringMVC

package com.cloud.config;


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

//如果扩展springmvc,官方建议我们这样去做!
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    //视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("cloud").setViewName("index");

    }
}

  • 在springboot中,有非常多的xxx Configuration帮助我们扩展配置,只要看见这个东西,我们就要注意了!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot 是一个快速构建基于 Spring 框架的应用程序的工具。它为 Spring 应用程序开发提供了一种简单的方法,无需繁琐地配置 XML,只需要使用注解即可实现常见的业务逻辑。 下面是一个基本的 Spring Boot Web 应用程序的步骤: 1. 创建一个 Maven 项目,并添加 Spring Boot 的依赖。 2. 创建一个 Controller 类,并添加处理请求的方法。 3. 配置应用程序的入口点,并启动应用程序。 以下是一个简单的示例: 1. 创建 Maven 项目 使用 Maven 创建一个新的项目,可以参考以下命令: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 2. 添加 Spring Boot 依赖 在 pom.xml 文件添加 Spring Boot Starter Web 依赖: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 3. 创建 Controller 类 创建一个 HomeController 类,并添加处理请求的方法: ``` @RestController public class HomeController { @GetMapping("/") public String home() { return "Hello, World!"; } } ``` 4. 配置应用程序的入口点 创建一个 SpringBootWebApplication 类,并使用 @SpringBootApplication 注解标记为应用程序的入口点: ``` @SpringBootApplication public class SpringBootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringBootWebApplication.class, args); } } ``` 5. 启动应用程序 使用以下命令启动应用程序: ``` mvn spring-boot:run ``` 在浏览器访问 http://localhost:8080/ ,即可看到 "Hello, World!"。 这就是一个简单的 Spring Boot Web 应用程序的开发过程。当然,除了以上步骤,还有很多其他的配置和实现方式,具体可以参考官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值