MAP | PageMixer Documents > Tutorial > Mixing with Struts > Handle errors | << | >> |
This section explains
how to handle message information of Struts,
which is held in "ActionMessages
",
even though title of this section has "error" in inself.
In this tutorial, abbreviated class names are used. Complete names are shown below.
Notation | Full name |
---|---|
ConsumerContext | jp.ne.dti.lares.foozy.pagemixer.mixer.ConsumerContext |
Filter | jp.ne.dti.lares.foozy.pagemixer.mixer.Filter |
HTMLSymbolSet | jp.ne.dti.lares.foozy.pagemixer.HTMLSymbolSet |
HashMapConsumerContext | jp.ne.dti.lares.foozy.pagemixer.mixer.HashMapConsumerContext |
MessagesInsetFilter | jp.ne.dti.lares.foozy.pagemixer.struts.MessagesInsetFilter |
SequenceWatcher | jp.ne.dti.lares.foozy.pagemixer.mixer.SequenceWatcher |
StrutsConsumerContext | jp.ne.dti.lares.foozy.pagemixer.struts.StrutsConsumerContext |
Notation | Full name |
---|---|
Bootstrap | pagemixer.filter.Bootstrap |
DummySetup | pagemixer.filter.DummySetup |
In this tutorial section, HTML page creation is explained before one about filter class, because HTML source example helps you to learn about messge handling filter class in PageMixer/Struts co-operation.
Example of error insetting tag in JSP for Struts is shown below.
<html:errors/>
With PageMixer framework, you should create HTML page shown below.
<!-- beginning of messages block --> <span class="Struts-errors"> <!-- beginning of header block --> <h3><font color="red">Validation Error</font></h3> <p>You must correct the following error(s) before proceeding:</p> <ul> <!-- end of header block --> <!-- beginning of iteration block --> <li class="Struts-error-iterate"> <!-- beginning of message block --> <span class="Struts-error"> error messages from resource bundle are placed here </span> <!-- end of message block --> </li> <!-- end of iteration block --> <!-- beginning of footer block --> </ul> <hr> <!-- end of footer block --> </span> <!-- end of messages block -->
Descriptions about "block"s shown in above HTML example are:
This block is target sub-sequence for message insetting.
Whole of this is trimmed,
if ActionMessages
(for error, in this example) is not found or is empty.
This should contain "iteration block" and "message block" as shown above.
You can recognize sub-sequence between beginning of "messages block" and beginning of "iteration block" as this.
This is corresponded to
the ResourceBundle
value
which is identified by "header
" attribute value as key
(or "errors.header
" itself as key)
for "html:messages"(or "html:errors") TagLib,
so this can be empty.
This block is iterated for each
"ActionMessage
"s,
which are contained by target "ActionMessages
".
This should contain(or equal to) "message block".
Whole of this block is replaced with message text
gotten from "MessageResources
"
at each "iteration block" repetition.
Difference of "iteration block" and this is corresponded to
the ResourceBundle
values
which are identified by
"errors.prefix
"/"errors.suffix
" as key
for "html:errors" TagLib.
You can recognize sub-sequence between end of "iteration block" and end of "messages block" as this.
This is corresponded to
the ResourceBundle
value
which is identified by "footer
" attribute value as key
("errors.footer
" as key)
for "html:messages"("html:errors") TagLib,
so this can be empty.
At first impression, you may think required HTML source is more complex than one for Strts TagLib. But this HTML source allows you to examine renderring result of HTML source without running web application on Servlet container.
PageMixer provides
"MessagesInsetFilter
"
to inset messages into HTML page.
Example of MessagesInsetFilter
creation
for Struts error information,
which is stored as Global.ERROR_KEY
in Servlet request attribute,
is shown below.
// to find "messages block" out SequenceWatcher messagesWatcher = new SequenceWatcher.Attr(SET.CLASS, "Struts-errors"); // to find "iteration block" out SequenceWatcher iterationWatcher = new SequenceWatcher.Attr(SET.CLASS, "Struts-error-iterate"); // to find "message block" out SequenceWatcher messageWatcher = new SequenceWatcher.Attr(SET.CLASS, "Struts-error"); MessagesInsetFilter filter = MessagesInsetFilter.forError(messagesWatcher, iterateWatcher, messageWatcher);
You can also create the MessagesInsetFilter
for Struts message information,
which is stored as Global.MESSAGE_KEY
in Servlet request attribute,
by forMessage()
method of MessagesInsetFilter
.
Please see API document for
detail about any other MessagesInsetFilter
features
for insetting message information
(e.g.: specify "property" of ActionMessages
).
As described above,
ActionError
stored in saved ActionErrors
is looked up by
ErrorKey
class.
With StrutsConsumerContext
,
it works correctly without no explicit preparation.
But with "HashMapConsumerContext
" or
other ConsumerContext
implementation class,
you should put corresponding ActionError
into it explicitly.
"DummySetup
" class helps
you to put ActionError
into specified
ConsumerContext
.
"setup(ConsumerContext context,
String[] args, int offset)
" of DummySetup
puts no ActionMessages
,
if (1)"args.length <= offset
" or
(2)"args[offset]
" is not "-
".
Otherwise,
it puts ActionMessages
created with element of "args
",
as key of ActionMessage
,
from index "offset
"(excluded)
to index "args.length
" or
one at which array contains "-
" string(excluded).
Now, everything needed are ready to use. Execution code is as below.
Bootstrap bootstrap = new Bootstrap.Default(args[0]){ protected void prepare(ConsumerContext context) { DummySetup.setup(context, args, 1); } }; MessagesInsetFilter filter = MessagesInsetFilter.forError(messagesWatcher, iterateWatcher, messageWatcher); bootstrap.execute(filter);
Command line example is shown below.
% java ClassName InputFile \ - error.database.missing \ error.fromAddress.format - OtherParameter ...
Above command line shows
only ones for
"error.fromAddress.format
"
in error information part of HTML page.
Omit arguments between two "-
" causes
trimming of whole error information part in HTML page.
MAP | PageMixer Documents > Tutorial > Mixing with Struts > Trim errors | << | >> |