- 浏览: 609053 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (334)
- java core (12)
- struts2.x (2)
- spring (3)
- hibernate (8)
- jpa (6)
- maven (2)
- osgi (5)
- eclipse (4)
- struts2.x+spring2.x+hibernate 整合 (5)
- ebs (0)
- html (0)
- vaadin (1)
- css (0)
- jquery (0)
- javascript (0)
- svn (1)
- cvs (0)
- axas2.x (0)
- eclipse+maven (9)
- annotation (0)
- 基于OSGi的动态化系统搭建 (1)
- notenet (1)
- jboss eclipse (4)
- eclipse工具 (4)
- jdk1.6+maven3.0.3+nuxeo+svn+felix+cxf+spring+springDM (6)
- spring dm (1)
- Nexus介绍 (1)
- proxool listener (0)
- oracle (4)
- mysql (8)
- 搭建你的全文检索 (1)
- hibernatehibernatehibernate (0)
- cvsearchcvsearch (0)
- mycvseach (0)
- asdfasdfasdf (0)
- propertiey (0)
- hibernate annotation (0)
- libs (0)
- icam (2)
- start 数据库配置 (0)
- jboss (1)
- 让Eclipse启动时显示选择workspace的对话框 (1)
- table表头固定 (1)
- s2s3h4 (0)
- leaver (0)
- mycvsaerchddd (0)
- 关于jboss5.0.1部署 (4)
- bookmarks (0)
- PersistenceUnitDeployment (0)
- mycom (0)
- HKEY_CURRENT_USER = &H80000001 (0)
- syspath (1)
- css div (1)
- Dreamweaver CS5 (0)
- generate (0)
- mysql查看表结构命令 (1)
- LOG IN ERROR EMAIL TO SB (0)
- struts2 handle static resource (1)
- jsf (2)
- log4j (1)
- jbpm4.4 (2)
- down: jbpm4.4 (1)
- jstl1.2 (1)
- spring annotation (1)
- java design pattern (1)
- cache (1)
- ehcache (1)
- 11111 (0)
- myge (0)
- pom.xml (0)
- springquartz (0)
- OpenStack (9)
- hadoop (2)
- nginx (1)
- hadoop openstack (1)
- os (1)
- hadoop-2.6.0 zookeeper-3.4.6 hbase-0.98.9-hadoop2 集群 (5)
- hadoop2.7.0 ha Spark (2)
- tess (0)
- system (1)
- asdf (0)
- hbase (2)
- hbase create table error (1)
- ekl (1)
- gitignore (1)
- gitlab-ci.yml (1)
- shell (1)
- elasticsearch (2)
- Azkaban 3.0+ (1)
- centos用命令 (1)
- hive (1)
- kafka (1)
- CaptureBasic (0)
- CentOS7 (1)
- dev tools (1)
- README.md (1)
- Error (1)
- teamviewerd.service (1)
- scala (1)
- spark (1)
- standard (1)
- gitlab (1)
- IDEA (0)
- ApplicationContext (1)
- 传统数仓 (1)
- redis install (1)
- MYSQL AND COLUME (1)
- java版本选择 (1)
- hue (1)
- npm (1)
- es (1)
- 版本管理 (1)
- 升级npm版本 (1)
- git (1)
- 服务器参数设置 (1)
- 调大 IDEA 编译内存大小 (0)
- CentOS8安装GitLab (1)
- gitlab安装使用 (1)
最新评论
-
ssydxa219:
vim /etc/security/limits.confvi ...
ekl -
Gamehu520:
table中无数据
hbase 出现的问题 -
Xleer0102:
为什么都是只有问没有答,哭晕在厕所
hbase 出现的问题 -
jiajiao_5413:
itext table -
CoderDream:
不完整,缺com.tcs.org.demostic.pub.u ...
struts2.3.1.1+hibernate3.6.9Final+spring3.1.0+proxool+maven+annotation
静态资源处理问题
package com.sean.filter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class StaticResourceFilter implements Filter {
private static final String RESOURCE_BAST_PATH = "resourceBastPath";
private String basePath;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
HttpServletRequest req = (HttpServletRequest) request;
String uri = req.getRequestURI();
// find howto/xxx.swf request
Pattern pattern = Pattern.compile("(howto/.+\\.swf$)");
Matcher matcher = pattern.matcher(uri);
if (matcher.find()) {
String path = matcher.group(0);
// Read data from disk and transmit to response.
InputStream is = new FileInputStream(getBasePath() + "/" + path);
int iValue = -1;
while ((iValue = is.read()) != -1) {
response.getOutputStream().write(iValue);
}
response.getOutputStream().flush();
return;
}
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
setBasePath(filterConfig.getInitParameter(RESOURCE_BAST_PATH));
}
public String getBasePath() {
return basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
}
<!-- Filter to deal with request for swf resources -->
<filter>
<filter-name>resourceFilter</filter-name>
<filter-class>com.sean.filter.StaticResourceFilter</filter-class>
<init-param>
<param-name>resourceBastPath</param-name>
<param-value>/var/www/xxapp/resource/flash</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>resourceFilter</filter-name>
<!-- Filter all request -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<object classid="clsid:F08DF954-8592-11D1-B16A-00C0F0283628" id="Slider1"
width="100" height="50">
<param name="BorderStyle" value="1" />
<param name="MousePointer" value="0" />
<param name="Enabled" value="1" />
<param name="Min" value="0" />
<param name="Max" value="10"/>
<param name="src" value="howto/help.swf"/>
</object>
struts2.1.8 StrutsPrepareAndExecuteFilter 关于静态资源处理问题
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>packages</param-name>
<param-value>net.zdsoft.eis.template</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/static/*</url-pattern>
</filter-mapping>
用于处理静态资源(css、js、图片之类),启动后正常。但当修改了struts配置文件,并且struts.configuration.xml.reload=true时,再次加载静态资源时会出错如下错误:
java.lang.NullPointerException at org.apache.struts2.dispatcher.DefaultStaticContentLoader.findStaticResource(DefaultStaticContentLoader.java:164)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeStaticResourceRequest(ExecuteOperations.java:62)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:86)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
原因:重新加载配置文件时,静态资源的处理类DefaultStaticContentLoader的setHostConfig(HostConfig filterConfig)方法没调用,导致pathPrefixes为空。
解决方案:
1、继承StrutsPrepareOperations类,构造方法中传入参数FilterConfig。createActionContext方法中如果oldContext为空,则重新初始化静态资源配置。完整代码如下:
public class StrutsPrepareOperations extends PrepareOperations {
private ServletContext servletContext;
private Dispatcher dispatcher;
private FilterHostConfig config;
public StrutsPrepareOperations(ServletContext servletContext, Dispatcher dispatcher,
FilterConfig filterConfig) {
super(servletContext, dispatcher);
this.dispatcher = dispatcher;
this.servletContext = servletContext;
this.config = new FilterHostConfig(filterConfig);
}
// 重写doFilter,由于在struts配置文件重新加载后,静态资源有问题
/**
* Creates the action context and initializes the thread local
*/
public ActionContext createActionContext(HttpServletRequest request,
HttpServletResponse response) {
ActionContext ctx;
Integer counter = 1;
Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
if (oldCounter != null) {
counter = oldCounter + 1;
}
ActionContext oldContext = ActionContext.getContext();
if (oldContext != null) {
// detected existing context, so we are probably in a forward
ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
} else {
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class)
.createValueStack();
stack.getContext().putAll(
dispatcher.createContextMap(request, response, null, servletContext));
ctx = new ActionContext(stack.getContext());
// add by zhaosf
StaticContentLoader staticResourceLoader = dispatcher.getContainer().getInstance(
StaticContentLoader.class);
staticResourceLoader.setHostConfig(config);
}
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
ActionContext.setContext(ctx);
return ctx;
}
2、继承org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter时,重写init方法,如下:
public class StrutsFilter extends StrutsPrepareAndExecuteFilter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
InitOperations init = new InitOperations();
try {
FilterHostConfig config = new FilterHostConfig(filterConfig);
init.initLogging(config);
Dispatcher dispatcher = init.initDispatcher(config);
init.initStaticContentLoader(config, dispatcher);
prepare = new StrutsPrepareOperations(filterConfig.getServletContext(), dispatcher,
filterConfig);
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
postInit(dispatcher, filterConfig);
} finally {
init.cleanup();
}
}
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/static/*</url-pattern>
</filter-mapping>
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeStaticResourceRequest(ExecuteOperations.java:62)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:86)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
原因:重新加载配置文件时,静态资源的处理类DefaultStaticContentLoader的setHostConfig(HostConfig filterConfig)方法没调用,导致pathPrefixes为空。
private ServletContext servletContext;
private Dispatcher dispatcher;
private FilterHostConfig config;
FilterConfig filterConfig) {
super(servletContext, dispatcher);
this.dispatcher = dispatcher;
this.servletContext = servletContext;
this.config = new FilterHostConfig(filterConfig);
}
/**
* Creates the action context and initializes the thread local
*/
public ActionContext createActionContext(HttpServletRequest request,
HttpServletResponse response) {
ActionContext ctx;
Integer counter = 1;
Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
if (oldCounter != null) {
counter = oldCounter + 1;
}
if (oldContext != null) {
// detected existing context, so we are probably in a forward
ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
} else {
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class)
.createValueStack();
stack.getContext().putAll(
dispatcher.createContextMap(request, response, null, servletContext));
ctx = new ActionContext(stack.getContext());
StaticContentLoader staticResourceLoader = dispatcher.getContainer().getInstance(
StaticContentLoader.class);
staticResourceLoader.setHostConfig(config);
}
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
ActionContext.setContext(ctx);
return ctx;
}
public void init(FilterConfig filterConfig) throws ServletException {
InitOperations init = new InitOperations();
try {
FilterHostConfig config = new FilterHostConfig(filterConfig);
init.initLogging(config);
Dispatcher dispatcher = init.initDispatcher(config);
init.initStaticContentLoader(config, dispatcher);
filterConfig);
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
} finally {
init.cleanup();
}
}
相关推荐
Spring Framework 4.1 mvc添加了静态资源增强处理功能.本源码仅是一个简单使用例子.详细参考;http://blog.csdn.net/xiejx618/article/details/40478275
springmvc对静态资源处理。springmvc对静态资源处理。springmvc对静态资源处理。springmvc对静态资源处理。
而Vue CLI 3.x中,提供了public目录来代替static目录,对于静态资源的处理有如下两种方式。 经过webpack处理:在JavaScript被导入或在template/CSS中通过相对路径被引用的资源 不经过webpack处理:存放在public目录...
SpringBoot静态资源处理共5页.pdf.zip
Express请求处理-静态资源的处理示例代码
处理web静态资源文件在客户端浏览器缓存的工具,java编写的小程序工具。 主要处理js和css文件,将指定目录下所有匹配的js和css文件添加特定的版本号作为文件名,并处理指定目录下所有html文件中对该js或css的引用。 ...
Spring MVC和springboot静态资源处理.rar
Tomcat 中的请求都是由 Servlet 处理,静态资源也不例外。在默认的 web.xml 中,配置了一个 DefaultServlet 用于处理静态资源,它支持缓存和断点续传。 DefaultServlet 的基本处理过程如下: 查找资源是否存在缓存 ...
这里提供了一个有关java实现的文件静态的处理方法
pring4新特性之静态资源处理增强共4页.pdf.zip
解决方法: 1.首先你需要在自己的app下面创建2个目录 ... 您可能感兴趣的文章:django 解决自定义序列化返回处理数据为null的问题django queryset 去重 .distinct()说明django的403/404/500错误自定义页面的配置方式
主要介绍了详解Nodejs之静态资源处理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2292098
利用TBC进行GPS静态数据处理的方法.pdf
主要介绍了struts2配置静态资源的相关内容,文中涉及了具体代码介绍,需要的朋友可以参考下。
主要介绍了Spring Boot实战之静态资源处理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧