Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Re: [S2] Is it possible to get Struts' FilterDispatcher to serve up
static files

Matt Raible

2007-02-27

Replies:


I came up with a StaticFilter (from http://issues.appfuse.org/browse/APF-431)
that seems to work (see code below). However, it causes another issue for
me. In my SiteMesh decorator, I'm including a messages.jsp that has calls to
the valueStack:

<%-- ActionError Messages - usually set in Actions --%>
<s:if test="hasActionErrors()">
  <div class="error" id="errorMessages">  
   <s:iterator value="actionErrors">
     <c:url value= "
       alt="<fmt:message key="icon.warning"/>" class="icon" />
    <s:property escape="false"/><br />
   </s:iterator>
 </div>
</s:if>

<%-- FieldError Messages - usually set by validation rules --%>
<s:if test="hasFieldErrors()">
  <div class="error" id="errorMessages">  
   <s:iterator value="fieldErrors">
      <s:iterator value="value">
        <c:url value= "
          alt="<fmt:message key="icon.warning"/>" class="icon" />
        <s:property escape="false"/><br />
      </s:iterator>
   </s:iterator>
 </div>
</s:if>

When I return a static page, this JSP causes issue:

java.lang.NullPointerException
    at org.apache.struts2.views.jsp.TagUtils.getStack (TagUtils.java:58)

This works fine when I request a Struts Action. Is there any way to check
for the stack in my messages.jsp?

Thanks,

Matt

<code>
public class StaticFilter extends OncePerRequestFilter {
  private final static String DEFAULT_INCLUDES = "*.html";
  private final static String DEFAULT_EXCLUDES = "";
  private String[] excludes;
  private String[] includes;
  public static final String INCLUDES_PARAMETER = "includes";
  public static final String EXCLUDES_PARAMETER = "excludes";

  /**
  * Read the includes/excludes paramters and set the filter accordingly.
  */
  public void initFilterBean() {
    String includesParam =
getFilterConfig().getInitParameter(INCLUDES_PARAMETER);
    if (StringUtils.isEmpty(includesParam)) {
       includes = parsePatterns(DEFAULT_INCLUDES);
    } else {
       includes = parsePatterns(includesParam);
    }

    String excludesParam =
getFilterConfig().getInitParameter(EXCLUDES_PARAMETER);
    if (StringUtils.isEmpty(excludesParam)) {
       excludes = parsePatterns(DEFAULT_EXCLUDES);
    } else {
       excludes = parsePatterns(excludesParam);
    }
  }

  private String[] parsePatterns(String delimitedPatterns) {
    //make sure no patterns are repeated.
    Set patternSet =
org.springframework.util.StringUtils.commaDelimitedListToSet(delimitedPatterns);
    String[] patterns = new String[patternSet.size()];
    int i = 0;
    for (Iterator iterator = patternSet.iterator(); iterator.hasNext();
i++) {
       //no trailing/leading white space.
       String pattern = (String) iterator.next();
       patterns[i] = pattern.trim();
    }
    return patterns;
  }

  public void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
                     FilterChain chain) throws IOException,
ServletException {

    UrlPathHelper urlPathHelper = new UrlPathHelper();
    String path = urlPathHelper.getPathWithinApplication(request);
    boolean pathExcluded = PatternMatchUtils.simpleMatch(excludes,
path);
    boolean pathIncluded = PatternMatchUtils.simpleMatch(includes,
path);

    if (pathIncluded && !pathExcluded) {
       RequestDispatcher rd =
getServletContext().getRequestDispatcher(path);
       rd.forward(request, response);
    }

    chain.doFilter(request, response);
  }
}
</code>

Stuart Piltch wrote:
>
> mraible <matt <at> raibledesigns.com> writes:
>> I'm using *.html as my default extension, which causes a whole host of
>> issues
>> with Dojo. Regardless, I want to fight through it and see if I can come
>> up
>> with a solution.
>
> Hi Matt,
>
> I'm not sure about your specific FilterDispatcher question, but we also
> use
> *.html as a default extension. We ended up copying all the dojo html files
> that
> we use into out own /WEB-INF/widgets directory and adding an extra
> struts-dojo.xml file to our config:
>
> <!DOCTYPE struts PUBLIC
>      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>      "http://struts.apache.org/dtds/struts-2.0.dtd">
>
> <struts>
>   <package name="dojo" namespace="/struts/dojo/src/widget/templates"
> extends="base">
>      <action name="*">
>         <result type="plainhtml">/WEB-INF/widgets/{1}.html</result>
>      </action>
>   </package>
>   <package name="dojoEditor2"
> namespace="/struts/dojo/src/widget/templates/Editor2" extends="base">
>      <action name="*">
>         <result
> type="plainhtml">/WEB-INF/widgets/Editor2/{1}.html</result>
>      </action>
>   </package>
>   <package name="dojoEditor2Dialog"
> namespace="/struts/dojo/src/widget/templates/Editor2/Dialog"
> extends="base">
>      <action name="*">
>         <result
> type="plainhtml">/WEB-INF/widgets/Editor2/Dialog/{1}.html</result>
>      </action>
>   </package>
> </struts>
>
> It seems convoluted, but it's working. The other dojo files (css, js, etc)
> are
> working fine from within the struts jar.
>
> Cheers,
>
> - stuart
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@(protected)
> For additional commands, e-mail: user-help@(protected)
>
>
>

--
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.