I’m developing a spring application. I’m using Vaadin for building user interface.
I have class like this which should be responsible for displaying images.
@SpringComponent
@Scope("prototype")
@AnonymousAllowed
@Route(value="galery", layout=MainLayout.class)
@PageTitle("Photogalery")
public class GaleryView extends VerticalLayout{
private static final long serialVersionUID = 1L;
private Image pngImage = new Image("images/user.png","static image png ");
private Image jpegImage = new Image("images/IMG_1797.JPG","static image jpg");
private Image airplane = new Image("images/airplane.png","");
private Image appleTouch = new Image("images/apple-touch-icon.png","");
public GaleryView() {
add(pngImage);
add(jpegImage);
add(airplane);
add(appleTouch);
}
PNG images are displayed as expected, but problem is with JPG images, which are not displayed, just hepler text is displayed. Images are stored inside src/main/resources/META-INF/resouces/images folder. That is the way, which is recommended by Vaadin, for displaying images in Spring application. But I tried also other ways, none was worked. I’m using Tomcat as webserver, I’m not sure whether it’s needed to configure Tomcat somehow, but I cannot google much about that.
Please, Is here someone who has experience with problem like this?
I tried also different file path for images.
I also tried rename images, for example from IMG_1797.JPG to IMG_1797.JPEG and vice versa(I don’t know where exactly is the difference), but nothing worked.
You are using Spring, you should be use Resource
class and classpath:
prefix to help you to load the content.
You are loading the images correctly according to the Vaadin docs.
Do you have a security config that is not permitting these types of files? If so, you’ll want to make sure *.jpg
or *.jpeg
s added. It might look like this:
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends VaadinWebSecurity {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(
authorize -> authorize.requestMatchers(
new AntPathRequestMatcher("/images/*.png"),
new AntPathRequestMatcher("/images/*.jpg"),
new AntPathRequestMatcher("/images/*.svg")
).permitAll());
// ...
}
}
1