springBoot Web项目解决首页访问问题

方法一:写一个空的方法,return到指定的首页(每个都要写太麻烦不推荐)

方法二:在cofig类中指定(推荐)

@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /atguigu 请求来到 success
        registry.addViewController("/atguigu").setViewName("success");
    }

    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
            }
        };
        return adapter;
    }
}

注意一定要加@bean注解否则没有注入到容器里,是不会装配的。

同时页面引用的时候,记得用thymeleaf引擎。

引入样式时候,需要用th:herf="@{/webjars/........}"路径要在webjars下

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
步骤: 1. 创建Spring Boot项目 2. 引入相关依赖 在pom.xml中添加以下依赖: ```xml <dependencies> <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MySQL连接驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- MyBatis框架 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> </dependencies> ``` 3. 配置MySQL数据库信息 在application.properties文件中添加以下配置: ```properties # MySQL数据库配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root # MyBatis配置 mybatis.type-aliases-package=com.example.demo.entity mybatis.mapper-locations=classpath:mapper/*.xml ``` 其中,url中的test为数据库名称,后面的参数用于解决时区问题和中文乱码问题。 4. 创建实体类和DAO接口 创建一个实体类User,包含id、name、age三个属性,并生成对应的getters和setters方法。 创建一个DAO接口UserMapper,包含一个查询所有用户的方法findAll()。 ```java package com.example.demo.entity; public class User { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } ``` ```java package com.example.demo.dao; import com.example.demo.entity.User; import java.util.List; public interface UserMapper { List<User> findAll(); } ``` 5. 创建Mapper映射文件 在resources目录下创建mapper文件夹,在该文件夹下创建UserMapper.xml文件,用于映射UserMapper接口中的方法。 ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.dao.UserMapper"> <select id="findAll" resultType="com.example.demo.entity.User"> select * from user </select> </mapper> ``` 6. 创建Service和Controller层 创建一个UserService接口和一个UserServiceImpl实现类,用于调用UserMapper中的方法。 创建一个UserController类,用于处理用户请求并调用UserService中的方法,将查询结果放入ModelAndView中并返回。 ```java package com.example.demo.service; import com.example.demo.entity.User; import java.util.List; public interface UserService { List<User> findAll(); } ``` ```java package com.example.demo.service.impl; import com.example.demo.dao.UserMapper; import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> findAll() { return userMapper.findAll(); } } ``` ```java package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller public class UserController { @Autowired private UserService userService; @GetMapping("/") public ModelAndView index() { List<User> userList = userService.findAll(); ModelAndView modelAndView = new ModelAndView("index"); modelAndView.addObject("userList", userList); return modelAndView; } } ``` 7. 创建页面 在resources/templates目录下创建index.html文件,用于展示从数据库中查询到的用户数据。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户列表</title> </head> <body> <h1>用户列表</h1> <table border="1"> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> </tr> <tbody> <tr th:each="user : ${userList}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> </tr> </tbody> </table> </body> </html> ``` 8. 运行程序 在IDE中运行程序,访问http://localhost:8080/即可看到从数据库中查询到的用户数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值