简体   繁体   English

如何从Vaadin / Spring应用程序提供静态资源?

[英]How to serve static resources from a Vaadin/Spring application?

I have Vaadin web application with spring security integration for authentication. 我有Vaadin Web应用程序与Spring安全集成进行身份验证。 The configuration of the Vaadin servlet is pretty simple: Vaadin servlet的配置非常简单:

<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>

The servlet initializes the Spring Context and returns the Vaadin application. servlet初始化Spring Context并返回Vaadin应用程序。 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>

The login form is styled with an external css and there are also some images. 登录表单使用外部CSS进行样式设置,并且还有一些图像。 Basically the images are located in /jsp/img and the stylesheet in /jsp/login.css. 基本上,图像位于/ jsp / img中,样式表位于/jsp/login.css中。 So the WAR structure looks like: 所以WAR结构如下所示:

  • /jsp / JSP
  • /META-INF / META-INF
  • /VAADIN / VAADIN
  • /WEB-INF / WEB-INF

Neither the images nor the css gets loaded, because obviously all those requests are mapped to the vaadin servlet. 图像和css都没有加载,因为显然所有这些请求都被映射到vaadin servlet。 How can I define some static resources directory, which wouldn't be served by the Vaadin servlet? 如何定义一些静态资源目录,这些目录不会由Vaadin servlet提供? I have tried the spring mvc:resources but that didn't work. 我尝试过spring mvc:resources但是没有用。 Thank you for your help. 谢谢您的帮助。

Bye, Filip 再见,菲利普

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/*). 我已经将Vaadin Application Servlet映射到/ app / *而不是/ *(请记住,在这种情况下,您还必须将相同的servlet映射到/ VAADIN / *)。 With this configuration I am able to access the jsp directory from my webapp and everything works fine. 使用此配置,我可以从我的webapp访问jsp目录,一切正常。 I have deleted the whole Spring Resources configuration, as this just didn't work. 我删除了整个Spring Resources配置,因为这不起作用。

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 :). 所以再一次,我仍然对这个解决方案不太满意,宁愿以其他方式配置我的RESOURCES目录,但客户很高兴:)。 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. 使用url重写过滤器可以更好地控制url映射。

<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>

then map Vaadin application to /vaadin for example and configure url maping in urlrewrite.xml 然后将Vaadin应用程序映射到/ vaadin,并在urlrewrite.xml中配置url maping

 <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>   

EDIT Other option is put static files in /VAADIN/ directory. 编辑其他选项是将静态文件放在/ VAADIN /目录中。

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/*). 我已经将Vaadin Application Servlet映射到/ app / *而不是/ *(请记住,在这种情况下,您还必须将相同的servlet映射到/ VAADIN / *)。 With this configuration I am able to access the jsp directory from my webapp and everything works fine. 使用此配置,我可以从我的webapp访问jsp目录,一切正常。 I have deleted the whole Spring Resources configuration, as this just didn't work. 我删除了整个Spring Resources配置,因为这不起作用。

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 c 所以再一次,我仍然对这个解决方案不太满意,而宁愿以其他方式配置我的RESOURCES目录,但是c

Might be late but for who is still having problems with serving static content while using vaadin /* mapping, the solution I found was using apache's default servlet org.apache.catalina.servlets.DefaultServlet , so a web.xml will have something like: 可能会迟到但是对于在使用vaadin /*映射时仍然遇到静态内容问题,我找到的解决方案是使用apache的默认servlet org.apache.catalina.servlets.DefaultServlet ,因此web.xml将具有如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
  id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
    <init-param>
      <param-name>UI</param-name>
      <param-value>com.ex.myprj.MyUI</param-value>
    </init-param>
    <!-- If not using the default widget set-->
    <init-param>
      <param-name>widgetset</param-name>
      <param-value>com.ex.myprj.AppWidgetSet</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>Static content Servlet</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>0</param-value>
    </init-param>
    <init-param>
      <param-name>listings</param-name>
      <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Static content Servlet</servlet-name>
    <url-pattern>/customer/*</url-pattern>
  </servlet-mapping>
</web-app>

So in the example above, despite having vaadin at /* , the /customer/* part will be served as static content by the DefaultServlet 所以在上面的例子中,尽管在/*有vaadin, /customer/*部分将由DefaultServlet作为静态内容提供

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Spring / Servlet / Tomcat中从内存提供静态资源的优雅方法? - Elegant way to serve static resources from memory in Spring / Servlet / Tomcat? 使用Spring从压缩文件中提供静态资源 - Serve static resources from within a compressed file using Spring 如何向vaadin添加静态资源 - How to add static resources to vaadin 无法通过Spring Boot提供静态资源 - Unable to serve static resources with Spring Boot 在 vaadin 中提供 static 资源 - Serve static ressources in vaadin 将pdf作为在docker容器中运行的spring应用程序的静态资源提供 - Serve pdf as static resource from spring application running in docker container 无法在Spring引导应用程序中将src / main / resources / static / css /文件夹中的css文件提供给客户端 - Unable to serve css files placed in src/main/resources/static/css/ folder to client in Spring boot application 如何使本地JBOSS AS 7提供静态资源? - How to make a local JBOSS AS 7 serve static resources? Spring MVC-从应用程序上下文外部访问静态资源 - Spring MVC - Access static resources from outside the application context 如何动态地将静态资源添加到spring-boot jar应用程序? - How to dynamically add static resources to spring-boot jar application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM