- 我有 apache 并配置如下:
RewriteEngine On ProxyPass / <a href="http://demo1.example.com:8080/myApp" rel="noreferrer noopener nofollow">http://demo1.example.com:8080/myApp</a> ProxyPassReverse / ....(same url as above)
以下是 web.xml:
<filter> <filter-name>struts2</filter-name> <filter- class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter> <filter-name>UrlFilter</filter-name> <filter-class>com.rts.utils.UrlFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
我自定义了过滤器,可以从 url 读取子域名并在请求中设置:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpReq = (HttpServletRequest) request; if(httpReq.getServerName() != null && httpReq.getServerName().indexOf(".") != -1) { request.setAttribute("subdomain",...); chain.doFilter(request, response); } }
我使用的是 struts 2.3.8、Tomcat 6 和 JDK 1.6。我将应用程序部署在 webapps/myApp 下。
当我登录应用程序时,我可以看到以下网址出现 404 错误,并且我的页面无法正确加载:
http://example.com/myApp/struts/js/base/jquery-1.8.3.js
如果我尝试上面的网址(直接在浏览器中)而不给出“myApp”,如下所示,我将获取脚本
http://example.com/struts/js/base/jquery-1.8.3.js
这意味着struts2没有在正确的上下文路径中生成静态内容?
最佳答案
最后我找到了这个问题的解决方案。我只是想在这里发帖,这可能会对某人有所帮助。
<sj:head />
就可以了,这个标签有助于从 jar 文件复制静态内容。
如果我们将应用程序部署为 ROOT,我们只需要添加 <sj:head/>
,这会将内容复制到 <context>/struts/...
如果我们将 myApp 部署到 webapps,<sj:head/>
将内容复制到/myApp,但框架搜索“/struts/..”将不可用。
解决方案:<sj:head>
标签具有属性“scriptPath”,这告诉框架将内容复制到哪里。我在 jsp 中添加了以下代码解决了该问题:
<sj:head compressed="false" scriptPath="/struts/"/>
关于java - Struts2静态内容给出404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38387078/