- 浏览: 627672 次
- 性别:
- 来自: 杭州
-
文章分类
- 全部博客 (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 Security处理静态资源的访问是非常重要的。 首先,Spring Boot在`src/main/resources`目录下存放静态资源,包括`static/`、`public/`或`resources/`等子目录。这些目录下的内容将被自动处理...
在Spring MVC框架中,处理动态请求是其核心功能,但同时它也需要支持对静态资源的访问,例如HTML、CSS、JavaScript、图片等。...通过灵活的配置,开发者可以根据项目需求来定制最佳的静态资源处理策略。
标题“狂神springboot静态资源.zip”表明这是一个关于Spring Boot框架中处理静态资源的教程或示例集合。Spring Boot是Java开发中一个流行的微服务框架,它简化了配置,提供了快速构建可生产级应用的方式。在Spring ...
Spring Framework 4.1 mvc添加了静态资源增强处理功能.本源码仅是一个简单使用例子.详细参考;http://blog.csdn.net/xiejx618/article/details/40478275
然而,在使用拦截器时,可能会遇到拦截器误拦截静态资源文件(如js、css、图片等)的问题。这不仅会影响网页的加载速度,还可能导致页面呈现异常。 在本文中,我们讨论了三种解决SpringMVC拦截器拦截静态资源文件的...
在Spring Boot框架中,静态资源处理是Web应用开发中的一个重要环节。Spring Boot为开发者提供了便捷的方式来管理和处理静态资源,如HTML、CSS、JavaScript文件等。这些资源通常用于构建用户界面,提供网页应用的视觉...
在JavaWeb开发中,静态资源文件通常指的是不通过服务器端代码处理,直接由Web服务器发送给客户端的文件,例如HTML、CSS、JavaScript、图片等。这些文件对于构建用户界面至关重要,HTML定义了页面结构,CSS负责样式和...
下面我们将深入探讨Spring Security的核心概念、功能以及它如何处理静态资源的保护。 1. **Spring Security简介** Spring Security 是Spring生态系统中的一个组件,用于提供认证(Authentication)和授权...
在这个“fastapi静态资源”压缩包中,我们可能找到了关于如何在FastAPI中处理静态资源的相关资料。现在,让我们深入探讨这个主题。 首先,静态资源在Web开发中指的是不会因用户交互或服务器端逻辑而改变的文件,如...
6. **使用Spring Boot**:在Spring Boot应用中,Spring Security与Spring Boot的静态资源处理机制集成良好。通过`spring.security.filter.static-resources-locations`属性,可以指定静态资源的位置,并自动处理权限...
在这个名为"狂神Spring Security静态资源"的资料中,我们可以期待学习到关于如何保护Web应用中的静态资源不被未经授权的用户访问。 首先,了解Spring Security的基本概念是必要的。它主要由四个组件构成:...
2. **使用Spring Boot**:Spring Boot简化了Java Web开发,它默认集成了静态资源处理。只需在`src/main/resources/static`或`public`目录下放置静态文件,Spring Boot就会自动处理它们。还可以通过配置`spring.web....
学习者可以从这里学习如何组织和引用这些静态资源,以及如何利用Spring Boot的静态资源处理功能。 在Spring Boot中,`spring-boot-starter-web`起步依赖包含了处理HTTP请求的DispatcherServlet和视图解析器,使得...
综上所述,这个压缩包提供的后台管理系统静态资源是一个强大的工具集,能够帮助开发者快速构建功能完善的后台管理界面,同时减少了前后端协作的复杂性。合理利用这些资源,可以显著提高开发效率,降低项目成本。
在Spring Boot应用中,处理静态资源是开发Web应用时常见的任务。静态资源通常包括HTML、CSS、JavaScript等文件,它们不需服务器动态处理,而是直接发送给客户端。本篇文章将探讨如何自定义资源映射来更好地管理和...
2. **简化部署流程**:通过Spring MVC直接处理静态资源,可以避免在部署过程中额外配置Web服务器(如Apache或Nginx)来处理这些资源。 3. **统一资源管理**:在同一个框架内管理所有资源,包括动态和静态资源,有助...
在Spring 4框架中,静态资源处理的增强是其重要的改进之一,这使得现代Web应用程序的开发更加便捷高效。在本文中,我们将深入探讨Spring 4在处理静态资源(如CSS、JavaScript、图片等)方面的新特性,以及这些变化...
在IT行业中,静态资源通常包括HTML、CSS、JavaScript、图片和其他不涉及服务器端动态处理的文件,它们是用户浏览器直接下载并呈现的部分。 【描述】中的反复提及"超市管理系统静态资源,smbms静态资源"表明这个系统...
在讨论“spring-security静态资源”这个主题时,我们将深入探讨Spring Security如何处理Web应用中的静态资源,如JavaScript、CSS、图片等。 1. **静态资源的安全访问** Spring Security 提供了一种机制来保护静态...