作者:正茂专家 | 来源:互联网 | 2023-08-30 17:28
1、上下文跟默认地址默认情况下springboot中request.getServletContext().getRealPath返回的是一个临时文件夹的地址2、查看源码通过查看源
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 findAll();
public void saveUser(User book);
public User findOne(long id);
public void delete(long id);
public List findByName(String name);
}
3.3、userserviceimpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserRepository userRepository ;
@Override
public List 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 findByName(String name) {
return null;
}
}
3.4、UserRepository
public interface UserRepository extends CrudRepository {
@Query("select l from User l ")
public List findAll();
}
3.5、控制器controller
@RestController
public class UserController {
@Autowired
UserService userService ;
@RequestMapping(value = "/userlist")
public List getUserList(HttpServletRequest req, HttpServletResponse resp){
System.out.println(req.getSession().getServletContext().getRealPath("/"));
return userService.findAll() ;
}
}
4、创建文件夹
在src/main下创建文件夹webapp
5、访问http://localhost:8080/userlist得到结果
/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的文件夹,但是拿到的还是临时目录
进行如下设置