Employees should be able to upload their profile picture, and that profile picture should be shown in the employee_info page. I was following this tutorial: https://www.youtube.com/watch?v=-Xf_fbKQit8 Uploading picture is working, but I can't bring image to web page.
My pictures are in a folder outside of Spring Boot project. I've tried this configuration:
How to "display" files from external folder with spring boot 2.1.1
My ConfigMVC class that implements WebMvcConfigurer:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer
{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
String employeeProfileUploadPath = "file:C:/my_company/profile_pictures/";
registry.addResourceHandler("/images/**").addResourceLocations(employeeProfileUploadPath);
}
Like in video tutorial, I'm passing URL of image via method in my DTO class for employees:
@Transient
public String getPhotoImagePath()
{
if(photo == null || nickname == null)
return null;
return "/my_company/profile_pictures/" + nickname + "/" + photo;
}
In controller I passed object of type EmployeeDTO to Model:
MyCompanyUserDetails userDetails = (MyCompanyUserDetails)principal;
int employeeId = userDetails.getEmployeeId();
Optional<EmployeeDTO> optional = employeeRepository.findById(employeeId);
EmployeeDTO employeeDTO = optional.get();
model.addAttribute("employeeDTO", employeeDTO);
And in html with Thymeleaf:
<img th:src="@{${employeeDTO.photoImagePath}}" width = "180" height = "180">
Thymeleaf placed URL:
http://localhost:8080/my_company/profile_pictures/admin1/admin1.jpg
Which is the right location of my image in my pc. I don't want to store it in ../static/images/** in Spring Boot project, I want this way and I've done MVC configuration as you could see above. I don't see what is the problem, but on Google Chrome console I get 404 error, resource not found.