一切正常,生成的HTML代码如下所示:
我有一个用Struts2制作的网络应用程序。我正在使用struts2-Jquery插件。
在jsp head部分中使用此设置:
<%@ taglib prefix="sj" uri="/struts-jquery-tags" %>
<head>
<sj:head/>
</head>
一切正常,生成的HTML代码如下所示:
<script src="/MyContextRoot/struts/js/base/jquery-1.10.0.min.js" type="text/javascript">
对于API:https://code.google.com/p/struts2-jquery/wiki/HeadTag,如果我更改sj:head到它:
<sj:head scriptPath="/MyContextRoot/MySubDir/struts/"/>
然后生成的代码将如下所示(如预期的那样):
<script src="/MyContextRoot/MySubDir/struts/js/base/jquery-1.10.0.min.js" type="text/javascript">
但是这个请求给了我一个来自服务器的404错误。
我可以创建目录,并放入所需的js文件,但我希望这些请求将由Struts提供。在第一种情况下起作用。
我的Struts2过滤器映射到: /*
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
有什么想法吗?
谢了,兹森
发布于 2015-03-06 05:28:50
我找到了解决办法。
我扩展了org.apache.struts2.dispatcher.DefaultStaticContentLoader类,并在struts.xml中添加了一个常量,将类用于静态内容加载。
首先创建你的装载机。您必须重写两个方法: canHandle和findStaticResource。
public class MyStaticLoader extends DefaultStaticContentLoader{
...
public boolean canHandle(String resourcePath) {
return superCanHandle || resourcePath.contains("MySubDir/struts");
}
public void findStaticResource(String path, HttpServletRequest request, HttpServletResponse response) throws IOException {
if(path != null && path.startsWith("/MySubDir/struts")){
path = path.substring(9);
}
super.findStaticResource(path, request, response);
}
}
在struts.xml中,只需将该类添加为staticContentLoader即可。
<constant name="struts.staticContentLoader" value="hu.nebih.hfnyportal.util.HFNyPublicStaticLoader"/>
就这样。
对我来说已经足够了。
https://stackoverflow.com/questions/28896208
复制相似问题