On Thu, September 22, 2005 3:50 pm, Frank W. Zammetti said:
> P.S. - I just threw together a quick little sample app to see if the
> filter can be used this way in the first place... it didn't work right off
> the bat, although I think it's just a mapping issue, not so much a flaw
> with the underlying concept.  I'll reply later when I have it working, in
> case anyone is interested.

Ok, got it working.  Ugh, took me 20 minutes to find a stupid typo in the
blank Struts app I use to start a new project.  ARGH!

Anyway... like I said, it works, and it's pretty simple.  In case anyone
is interested, here's all I did...

(1) I started with a simple, typical blank Struts webapp...
struts-config.xml is bare-bones:

  ...
  <form-beans>
    <form-bean name="testForm1" type="app.blank.TestForm" />
  </form-beans>
  <action-mappings>
    <action path="/test" type="app.blank.TestAction" name="testForm1"
scope="session" validate="false" input="index.jsp">
      <forward name="defaultForward" path="/test.jsp" />
    </action>
  </action-mappings>
  ...

This app contains index.jsp, which has nothing but a link pointing to
TestAction... TestAction does nothing except returning defaultForward,
which goes to test.jsp, which is simply:

  <%@ page language="java" %>
  <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
  <html>
  <head>
  <title>test.jsp</title>
  </head>
  <body>
  <html:form action="test">
    <html:text property="field1" />
  </html:form>
  </body>
  </html>

So, to that app I just dropped javawebparts-filter.jar into WEB-INF/lib. 
This depends on Commans-Beanutils, Digester and Logging, so they are
already available to your app in some fashion.

(2) Add the filter description to web.xml.  I did this:

  <filter>
    <filter-name>DependencyFilter</filter-name>
    <filter-class>javawebparts.filter.DependencyFilter</filter-class>
    <init-param>
      <param-name>configFile</param-name>
      <param-value>/WEB-INF/dependencies_config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>createSession</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>DependencyFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

createSession determines whether a session will be created by this filter
firing or not.  I think in most cases you would want it to, unless you
need to do something around session creation and a listener isn't
sufficient... Of course, if set it to false then no dependencies will be
added to session scope :)

(3) Add the dependencies_config.xml which in this case is very simple:

  <dependencies>
    <dependency scope="session" name="testForm1">
      <className>app.blank.TestForm</className>
      <initProp name="field1" value="Hello from DependencyFilter!" />
    </dependency>
  </dependencies>

field1 is a String, and it's the only thing in the ActionForm.

That's it!  Firing up the app and clicking the link results in "Hello from
DependencyFilter!" showing up in the HTML for on test.jsp, as expected.

As I mentioned before, you can do quiet a bit with DependencyFilter beyond
this... you can initialize Lists, Maps and Arrays, call methods on it or
pass it off to another named class after its fields have been populated
(if applicable) to do whatever more advanced configuration you need (i.e.,
reading from a database, etc).

One thing I did forget is that unlike most of the other filters in JWP,
this one *does not* support the more extended mapping capabilities, so you
can only configure it like a typical filter.  This is something I intend
to rectify, but I may do so by putting the path mapping info on the
<dependency> elements above, so that you can have very fine-grained
control over what paths trigger what dependencies being created.

In any case, I hope that helps someone somewhere along the way :)

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to