我使用通配符来加载我的javascript和css文件,它工作得很好,但是在开发阶段使用聚合的javascript文件是一个痛苦的工作。是否有一种方法可以让wro4j不聚合文件,而只是单独包含它们?
My wro.xml
File
<groups xmlns="http://www.isdc.ro/wro">
<group name="external">
<js>/app/lib/*.js</js>
</group>
<group name="application">
<js>/js/*.js</js>
<css>/css/normalize.css</css>
<css>/css/*.css</css>
</group>
</groups>
谢谢!
在开发模式(debug=true
)中实现这一点有一个技巧。有一个端点(/wro/wroAPI/model
-如果您将WroFilter映射到/wro/*
,这是有效的),它返回模型的JSON表示。使用这个JSON,您可以通过迭代组条目轻松地为给定组插入脚本和/或css链接。默认情况下还没有这样的小部件可用,但如果您创建了一个并且想要贡献它-欢迎您:)。
Alex,谢谢你的建议。
我已经创建了一个ServletFilter,它使用您上面提供的信息。我把它贴在这里供后人参考。注意:这不是理想的代码(例如,缓存太多),它只是在这里给人们一个实现的想法。我的实现使用Spring的DelegatingFilterProxy
来告诉代码应该考虑哪些组。至于创建一个插件,你能给我指出一些实现文档的方法吗,我对那部分不太清楚?
public class WroDebugFilter implements Filter
{
//http loader is shamelessly stolen from another project.
SimpleHttpLoader httpLoader = new SimpleHttpLoader();
private List<String> jscriptFiles;
private List<String> debugGroups;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setAttribute("jscriptFiles",getJavaScriptFiles((HttpServletRequest) request));
chain.doFilter(request,response);
}
private List<String> getJavaScriptFiles(HttpServletRequest request) {
if (jscriptFiles == null) {
List<String> cssFiles = new ArrayList<String>();
List<String> jsFiles = new ArrayList<String>();
JsonParser jsonParser = new JsonParser();
String url = httpLoader.httpGet(getApiUrl(request));
JsonObject data = jsonParser.parse(url).getAsJsonObject();
JsonArray groups = data.getAsJsonArray("groups");
for (JsonElement group : groups) {
JsonObject groupObject = group.getAsJsonObject();
String groupName = groupObject.get("name").getAsString();
if (debugGroups.contains(groupName)) {
JsonArray resources = groupObject.getAsJsonArray("resources");
for (JsonElement resource : resources) {
JsonObject resourceObject = resource.getAsJsonObject();
String uri = resourceObject.get("uri").getAsString();
if (uri.contains(".css")) {
cssFiles.add("t<link rel="stylesheet" href="" + request.getContextPath() + uri +"">");
}
else {
jsFiles.add("<script type="text/javascript" src="" + request.getContextPath() + uri + ""></script>");
}
}
}
}
cssFiles.addAll(jsFiles);
jscriptFiles = cssFiles;
}
return jscriptFiles;
}
private String getApiUrl(HttpServletRequest request) {
String serverName = request.getServerName();
String contextPath = request.getContextPath();
int port = request.getServerPort();
return String.format("http://%s:%s%s/%s",serverName,port,contextPath,"wro/wroAPI/model");
}
@Override
public void destroy() {
//To change body of implemented methods use File | Settings | File Templates.
}
public List<String> getDebugGroups() {
return debugGroups;
}
public void setDebugGroups(List<String> debugGroups) {
this.debugGroups = debugGroups;
}
}
wro.xml
文件
<groups xmlns="http://www.isdc.ro/wro">
<group name="external">
<js>/app/lib/*.js</js>
</group>
<group name="application">
<js>/js/*.js</js>
<css>/css/normalize.css</css>
<css>/css/*.css</css>
</group>
</groups>
和
Spring Configuration
<bean id="wroDebugFilter" class="gov.mystate.dhw.idalink.web.filter.WroDebugFilter">
<property name="debugGroups">
<list>
<value>application</value>
</list>
</property>
</bean>