感谢您的输入,亚历克斯。
我创建了一个使用您上面提供的信息的 ServletFilter。我把它贴在这里以供后代使用。注意:这不是理想的代码(例如缓存太多),它只是为了让人们了解实现的样子。我的实现使用 SpringDelegatingFilterProxy
来告诉代码需要担心哪些组。就创建插件而言,您能否为我指出一些实现文档的方法,我在那方面很模糊?
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>
和....
弹簧配置
<bean id="wroDebugFilter" class="gov.mystate.dhw.idalink.web.filter.WroDebugFilter">
<property name="debugGroups">
<list>
<value>application</value>
</list>
</property>
</bean>