1. 程式人生 > >spring boot 開發—第十篇修改tomcat容器上下文根地址

spring boot 開發—第十篇修改tomcat容器上下文根地址

1、上下文跟預設地址

預設情況下springboot中request.getServletContext().getRealPath 返回的是一個臨時資料夾的地址

2、檢視原始碼

通過檢視原始碼 位置在org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory中的getCommonDocumentRoot方法

private File getCommonDocumentRoot() {
    for (String commonDocRoot : COMMON_DOC_ROOTS) {
        File root = new File(commonDocRoot);
        if (root.exists() && root.isDirectory()) {
            return root.getAbsoluteFile();
        }
    }
    return null;
}

可以看到springboot 會嘗試讀取COMMON_DOC_ROOTS 配置裡面的路徑,所以我們只需要在專案所在的根目錄下新建一個public或者static的資料夾,那麼通過 request.getServletContext().getRealPath 就會得到public或者static的路徑

3、示例

3.1、user實體

@Entity
@Table(name = "t_user")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "t_id")
    private int id ;

    @Column(name = "t_name")
    private String name ;

    @Column(name = "t_age")
    private int age ;

    @Column(name = "t_address")
    private String address ;

    @Column(name = "t_pwd")
    private String password ;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

3.2、userservice

public interface UserService {

    public List<User> findAll();

    public void saveUser(User book);

    public User findOne(long id);

    public void delete(long id);

    public List<User> findByName(String name);
}

3.3、userserviceimpl

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserRepository userRepository ;

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public void saveUser(User book) {

    }

    @Override
    public User findOne(long id) {
        return null;
    }

    @Override
    public void delete(long id) {

    }

    @Override
    public List<User> findByName(String name) {
        return null;
    }
}

3.4、UserRepository

public interface UserRepository extends CrudRepository<User,Long> {

    @Query("select l from User l ")
    public List<User> findAll();
}

3.5、控制器controller

@RestController
public class UserController {

    @Autowired
    UserService userService ;

    @RequestMapping(value = "/userlist")
    public List<User> getUserList(HttpServletRequest req, HttpServletResponse resp){
        System.out.println(req.getSession().getServletContext().getRealPath("/"));

        return userService.findAll() ;
    }
}

4、建立資料夾

在src/main下建立資料夾webapp

/Users/vesus/Documents/workspace-sts/spring-boot-demo/springtomcat/src/main/webapp/
Hibernate: select user0_.t_id as t_id1_0_, user0_.t_address as t_addres2_0_, user0_.t_age as t_age3_0_, user0_.t_name as t_name4_0_, user0_.t_pwd as t_pwd5_0_ from t_user user0_

6、出現問題描述

在專案所在的根目錄下新建一個public或者static的資料夾,但是拿到的還是臨時目錄

進行如下設定
這裡寫圖片描述