The General Idea
+---+
| F |
| i | request
request --> | l | --> + --> JSP
| t | DO
error <---- | e |
| r |
+---+
Sorry about the lame ASCII diagram...
Anyway, the idea is that the filter validates request URL parameters and
creates data objects that are available to the view. The intent is that
in some applications you actually need the URL parameters for proper DO
instantiation.
If there are problems then the filter will toss you back to an error page.
Download Application Jar
-
Download the jar file - Perhaps you'll toss it under /WEB-INF/lib
on a project that you want to test it on?
-
Or download the sample application - essentially this website - to
see how it is done.
Configure your web.xml
-
Add the following to your web.xml under the filter and filter mapping sections...
(Note that filters are loaded in order.)
<filter>
<filter-name>CoffeeFilter</filter-name>
<filter-class>MLBFrameWork.CoffeeFilter.CoreFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/coffeeFilter-config.xml</param-value>
</init-param>
<init-param>
<param-name>messages</param-name>
<param-value>/WEB-INF/Messages.properties</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CoffeeFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Configure your filter XML
Write your DO classes
-
DO's aren't necessary if you are just using the framework to
ensure that required URL parameters are there.
-
DO's are really just JavaBeans that have a process method called
on them to populate the bean values.
-
Assuming that you need DO's, you need to write a class that
does the following:
-
Implements the interface CoffeeFilterDataObjectInterface.
The framework will handle object according to this interface
and it ensures that there is a "process" method in the DO class.
This is the method that actually does all the work. Again, we can
use the example application's DO here: (I'm omitting the variables for brevity's sake...)
public void process(HashMap<String, String>reqArgs,
HashMap<String, String> optArgs)
throws FilterValidationException {
String display = optArgs.get("display");
if (display == null || display.equals("")) {
content = "home.jsp";
title = "CoffeeFilter - Java Framework";
} else if (display.equals("usage")) {
content = "usage.jsp";
title = "CoffeeFilter - Usage";
} else if (display.equals("contact")) {
content = "contact.jsp";
title = "CoffeeFilter - Contact";
} else if (display.equals("download")) {
content = "download.jsp";
title = "CoffeeFilter - Download";
} else { // Anything else...
throw new FilterValidationException("index.query.bad");
}
}
-
You read out your required or optional parameters by just querying the HashMap's passed in.
Since you defined the parameters needed, you just type look for them to get the value from the
hash... In this example, our config specified that "display" was an optional URL parameter.
We just query the optArgs has and get the value. Check for null, do your processing (populate bean
vars...),
and handle ayn error cases ("display=thisFrameworkSux"...) and you're done.
Add your JSP's
-
If you aren't using DO's then the only JSP you'll have to worry about is the error page.
-
On each page that uses the DO's, you just have to get the object out. I haven't figured out
an elegant way for the filter to set a PageContext attribute so this requires a little scriptlet-ing.
<%
// Get your class, and set it into the page context.
IndexPageData pageData = (IndexPageData) request.getAttribute("DATA");
pageContext.setAttribute("DATA", pageData);
%>
<%-- Bean has to have id that matches the attribute handle! --%>
<jsp:useBean id="DATA" class="MLBFrameWork.Page.IndexPageData" scope="request"/>
Use the bean properties as needed, c:out, jsp:getProperty, EL-magic, whatever...
Slap on an error page
-
Add an error page and message file. In the example application, the tag "error-page" has
"error.jsp" defined. The "error-page" is required and you can only have one.
You could also also bundle this page with any other error message
you want to display (global Struts error page...) The messages file is defined in the web.xml configuration. I
guess
I could move it under the config XML, but this works for now.
The error system gets used when you decide to toss an FilterValidationException from a DO
class or something just plain gets botched up. Using the example application again:
- The Messages.properties file is straight ahead. The first is a default value, the
second is a custom message.
query.invalid=You are missing some items from your request.
index.query.bad=The parameter you entered is incorrect.
-
Your error page should be similar to the JSPs, except you use the ErrorPageData class to
read out a message.
<%
// Get your class, and set it into the page context.
ErrorPageData pageData = (ErrorPageData) request.getAttribute("DATA");
pageContext.setAttribute("DATA", pageData);
%>
<%-- Bean has to have id that matches the attribute handle! --%>
<jsp:useBean id="DATA" class="MLBFrameWork.CoffeeFilter.Data.ErrorPageData" scope="request"/>
<c:out value="${DATA.message}"/>