Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix Vaadin url mapping documentation #4072

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion articles/flow/integrations/spring/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,54 @@

By default, URL mapping is `/*`.

An additional servlet (e.g., `/my_mapping/*`) is required to handle the frontend resources for non-root servlets. The servlet can be defined in your application class. See https://raw.githubusercontent.com/vaadin/flow-and-components-documentation/master/tutorial-servlet-spring-boot/src/main/java/org/vaadin/tutorial/spring/Application.java[`Application` class] for an example.
=== Vaadin URL Mapping

When using a custom servlet URL mapping in a Vaadin application, special care must be taken when referencing static resources, such as images, in Vaadin views.
For example, if the application provides images in `src/main/resources/META-INF/resources/images` or `src/main/resources/static/images`, these images will be served from the root of the web application context (e.g., `http://localhost:8080/images/logo.png`).

Check warning on line 168 in articles/flow/integrations/spring/configuration.adoc

View workflow job for this annotation

GitHub Actions / lint

[vale] reported by reviewdog 🐶 [Vaadin.Will] Avoid using 'will'. Raw Output: {"message": "[Vaadin.Will] Avoid using 'will'.", "location": {"path": "articles/flow/integrations/spring/configuration.adoc", "range": {"start": {"line": 168, "column": 151}}}, "severity": "WARNING"}
If your Vaadin views are served under a custom mapping, such as `http://localhost:8080/my_mapping/`, you need to ensure that the correct path is provided for the images. To reference the image from the application root, you can use a relative path, such as `new Image("../logo.png", "Company Logo")`.
tepi marked this conversation as resolved.
Show resolved Hide resolved
However, this approach is not ideal when the same application can be deployed with different URL mappings or without any custom mapping, as it may lead to inconsistencies.
A solution could be using a helper method to compute a path relative to the context root:

[source,java]
----
public static String resolveStaticResource(String path) {
return UI.getCurrent().getInternals().getContextRootRelativePath()
+ path.replaceFirst("^/", "");
}

public class MyView extends Div {
public MyView() {
add(new Image(resolveStaticResource("images/logo.png")));
}
}
----

Another option is to register a Servlet Filter that intercepts static resource referenced by the Vaadin UI and forwards the request to the correct path.

[source,java]
----
@Bean
@ConditionalOnProperty(name = "vaadin.url-mapping")
FilterRegistrationBean<?> publicImagesAliasFilter(@Value("${vaadin.url-mapping}") String urlMapping) {
String baseMapping = urlMapping.replaceFirst("/\\*$", "");
FilterRegistrationBean<OncePerRequestFilter> registrationBean = new FilterRegistrationBean<>(
new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
throws ServletException, IOException {
// Remove Vaadin URL mapping from the path and forward the request
String path = request.getRequestURI().substring(baseMapping.length());
request.getRequestDispatcher(path)
forward(request, response);
}
});
registrationBean.addUrlPatterns(baseMapping + "/images/*");
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registrationBean;
}
----


== Configure Spring MVC Applications
Expand Down
Loading