最近开始做点页面,于是用strtus2,发现每次改动struts.xml和action的时候都要重启应用,严重影响开发效率。
设置了
struts.i18n.reload = true
struts.configuration.xml.reload = true
struts.devMode = true
缺发现struts.configuration.xml.reload = true似乎总是不起作用。
怒了,于是随手写了2个简单的类。虽然还有很多功能可以补充进去,比如生产,开发模式的开关等一系列功能,将以后在持续添加。
希望目前简单的实现可以对碰到类似问题的人有所帮助。
java1.4
package rabbit.struts2.dispatcher; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * * @author rabbit * */ public class FilterDispatcherJava4 implements Filter { private volatile Filter filter; private FilterConfig filterConfig; private volatile long lastModified = -1; private ClassLoader classLoader; public void destroy() { synchronized (this) { if (filter != null) { filter.destroy(); } } } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { getFilter().doFilter(arg0, arg1, arg2); } public void init(FilterConfig arg0) throws ServletException { filterConfig = arg0; classLoader = this.getClass().getClassLoader(); } private Filter getFilter() throws ServletException { URL url = classLoader.getResource("struts.xml"); File f = null; try { f = new File(url.toURI()); } catch (URISyntaxException e) { throw new RuntimeException(e); } synchronized (this) { long time = f.lastModified(); if (lastModified == time) { return filter; } if (filter != null) { filter.destroy(); } filter = new org.apache.struts2.dispatcher.FilterDispatcher(); filter.init(filterConfig); lastModified = time; return filter; } } }
java1.5
package rabbit.struts2.dispatcher; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * * @author rabbit * */ public class FilterDispatcherJava5 implements Filter { private volatile Filter filter; private FilterConfig filterConfig; private volatile long lastModified = -1; private ClassLoader classLoader; private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private Lock rl = rwl.readLock(); private Lock wl = rwl.writeLock(); public void destroy() { rl.lock(); try { if (filter != null) { filter.destroy(); } } finally { rl.unlock(); } } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { getFilter().doFilter(arg0, arg1, arg2); } public void init(FilterConfig arg0) throws ServletException { filterConfig = arg0; classLoader = this.getClass().getClassLoader(); } private Filter getFilter() throws ServletException { URL url = classLoader.getResource("struts.xml"); File f = null; try { f = new File(url.toURI()); } catch (URISyntaxException e) { throw new RuntimeException(e); } rl.lock(); try { if (lastModified == f.lastModified()) { return filter; } } finally { rl.unlock(); } wl.lock(); try { long time = f.lastModified(); if (lastModified == time) { return filter; } if (filter != null) { filter.destroy(); } filter = new org.apache.struts2.dispatcher.FilterDispatcher(); filter.init(filterConfig); lastModified = time; return filter; } finally { wl.unlock(); } } }
web.xml部分将sturts的org.apache.struts2.dispatcher.FilterDispatcher简单替换为
<filter> <filter-name>struts2</filter-name> <!--filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class--> <filter-class>rabbit.struts2.dispatcher.FilterDispatcherJava4</filter-class> </filter>
或者
<filter> <filter-name>struts2</filter-name> <!--filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class--> <filter-class>rabbit.struts2.dispatcher.FilterDispatcherJava5</filter-class> </filter>
即可。
开发的时候可以是用rabbit.struts2的过滤器,发布生产版本改在该回原来的struts2过滤器。