如何从Vaadin / Spring应用程序提供静态资源? [英] How to serve static resources from a Vaadin/Spring application?
问题描述
我有Vaadin Web应用程序,具有Spring安全集成以进行身份验证。 Vaadin servlet的配置非常简单:
I have Vaadin web application with spring security integration for authentication. The configuration of the Vaadin servlet is pretty simple:
<servlet>
<servlet-name>Vaadin Application Servlet</servlet-name>
<servlet-class>com.example.SpringApplicationServlet</servlet-class>
<init-param>
<param-name>applicationBean</param-name>
<param-value>mainApplication</param-value>
</init-param>
<init-param>
<param-name>widgetset</param-name>
<param-value>com.example.widgetset.CustomWidgetSet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Vaadin Application Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
servlet初始化Spring Context并返回Vaadin应用程序。我还为此配置了安全性并具有如下配置的自定义登录表单:
The servlet initializes the Spring Context and returns the Vaadin application. I have also configured the security for that and have a custom login form configured like this:
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>/jsp/login.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login_error</servlet-name>
<jsp-file>/jsp/loginError.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>login_error</servlet-name>
<url-pattern>/login_error</url-pattern>
</servlet-mapping>
登录表单采用外部CSS设置,并且还有一些图像。基本上,图像位于/ jsp / img中,而样式表位于/jsp/login.css中。所以WAR结构如下所示:
The login form is styled with an external css and there are also some images. Basically the images are located in /jsp/img and the stylesheet in /jsp/login.css. So the WAR structure looks like:
- / jsp
- / META-INF
- / VAADIN
- / WEB-INF
- /jsp
- /META-INF
- /VAADIN
- /WEB-INF
图片都没有也没有加载css,因为显然所有这些请求都映射到vaadin servlet。如何定义一些静态资源目录,这些目录不会由Vaadin servlet提供?我尝试过spring mvc:resources但是没有用。感谢您的帮助。
Neither the images nor the css gets loaded, because obviously all those requests are mapped to the vaadin servlet. How can I define some static resources directory, which wouldn't be served by the Vaadin servlet? I have tried the spring mvc:resources but that didn't work. Thank you for your help.
再见,
Filip
Bye, Filip
我已经想到了这一点。虽然它是一种解决方法。我已经将Vaadin Application Servlet映射到/ app / *而不是/ *(请记住,在这种情况下,您还必须将相同的servlet映射到/ VAADIN / *)。使用此配置,我可以从我的webapp访问jsp目录,一切正常。我删除了整个Spring Resources配置,因为这不起作用。
I have figured this out. Although it is rather a workaround. I have mapped the Vaadin Application Servlet to something like /app/* instead of to /* (Remember that in this case you also have to map the same servlet to /VAADIN/*). With this configuration I am able to access the jsp directory from my webapp and everything works fine. I have deleted the whole Spring Resources configuration, as this just didn't work.
所以再一次,我仍然对这个解决方案不太满意而宁愿拥有我的资源目录配置其他方式,但客户很高兴:)。如果有人得到了正确的解决方案,我将不胜感激。
So once more, I am still pretty not pretty comfortable with this solution and would rather have my RESOURCES dir configured other way, but the client is happy :). If anyone has got the right solution I would appreciate to read it.
推荐答案
使用网址重写过滤器获取更多网址控制权映射。
Use a url rewrite filter to get more contro on url mapping.
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后将Vaadin应用程序映射到/ vaadin,并在urlrewrite.xml中配置url maping
then map Vaadin application to /vaadin for example and configure url maping in urlrewrite.xml
<rule>
<from>/styles/**</from>
<to last="true">/styles/$1</to>
</rule>
<rule>
<from>/images/**</from>
<to last="true">/images/$1</to>
</rule>
<rule>
<from>/**</from>
<to>/vaadin/$1</to>
</rule>
<outbound-rule>
<from>/vaadin/**</from>
<to>/$1</to>
</outbound-rule>
编辑
其他选项是将静态文件放入/ VAADIN / directory。
EDIT Other option is put static files in /VAADIN/ directory.
这篇关于如何从Vaadin / Spring应用程序提供静态资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!