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

发布于 2025-01-06 03:49:58 字数 2110 浏览 1 评论 0原文

我有 Vaadin Web 应用程序与 spring security 集成进行身份验证。 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>

servlet 初始化 Spring Context 并返回 Vaadin 应用程序。我还为此配置了安全性,并配置了一个自定义登录表单,如下所示:

<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 结构如下所示:

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

图像和 css 均未加载,因为显然所有这些请求都映射到 vaadin servlet。如何定义一些 Vaadin servlet 不提供服务的静态资源目录?我尝试过 spring mvc:resources 但没有用。感谢您的帮助。

再见, 菲利普

我已经弄清楚了。尽管这只是一种解决方法。我已将 Vaadin 应用程序 Servlet 映射到 /app/* 之类的位置,而不是 /* (请记住,在这种情况下,您还必须将相同的 servlet 映射到 /VAADIN/*)。通过此配置,我可以从我的 web 应用程序访问 jsp 目录,并且一切正常。我已经删除了整个 Spring 资源配置,因为这不起作用。

因此,我仍然对这个解决方案不太满意,宁愿以其他方式配置我的资源目录,但客户很高兴:)。如果有人找到了正确的解决方案,我将不胜感激地阅读它。

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>

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>

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

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.

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/*). 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难得心□动 2025-01-13 03:49:58

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

然后将 Vaadin 应用程序映射到 /vaadin 例如,并在 urlrewrite.xml 中配置 url 映射

 <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/ 目录中。

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>

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>   

EDIT
Other option is put static files in /VAADIN/ directory.

谁的年少不轻狂 2025-01-13 03:49:58

我已经想通了。尽管这只是一种解决方法。我已将 Vaadin 应用程序 Servlet 映射到 /app/* 之类的位置,而不是 /* (请记住,在这种情况下,您还必须将相同的 servlet 映射到 /VAADIN/*)。通过此配置,我可以从我的 web 应用程序访问 jsp 目录,并且一切正常。我已经删除了整个 Spring 资源配置,因为这不起作用。

所以再一次,我仍然对这个解决方案不太满意,宁愿以其他方式配置我的 RESOURCES 目录,但是 c

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 c

不即不离 2025-01-13 03:49:58

可能会迟到,但对于在使用 vaadin /* 映射时仍然遇到提供静态内容的问题的人来说,我找到的解决方案是使用 apache 的默认 servlet org.apache.catalina.servlets.DefaultServlet< /code>,因此 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>

因此,在上面的示例中,尽管 /* 处有 vaadin,但仍将提供 /customer/* 部分作为DefaultServlet 的静态内容

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文