Java Reference
In-Depth Information
Listing 4-41. menu.jsp
1.<li><div>
2.
3.<span class="label" style="margin-left: 15px;">
4.<a href="<s:url action=""/>">Categories</a></span>
5.</div>
6.<ul>
7.<li><s:form action=" booksByCategoryLink">
8.
9.<s:select name="category" list="#session['categoryList']"
10.listValue="categoryDescription" listKey="id" />
11.<s:submit value="Select" />
12.</s:form><a class="label" href=""><span class="label"
13.style="margin-left: 30px;"></span></a></li>
14.
15.</ul></li>
Lines 9 to 10
: These lines display the categories in the list that was stored in the
session in
LoginAction
using the expression language of Struts 2.
Line 11
: When the user clicks the select button, the selected category and the
action name in line 7,
booksByCategoryLink
, are sent to the container, and then
mapped via
struts.xml
to the action, which retrieves the list of books.
Listing Books by Category
In this section, you will retrieve the list of books by category from the database. The selected
category and the action name
booksByCategoryLink
are sent to the container, and then mapped via
struts.xml
to
BookController
. Listing 4-42 illustrates the code fragment of
struts.xml
.
Listing 4-42. Declaring BookController in struts.xml
1. <action name="*Link" method="{1}"
2. class="com.apress.bookstore.controller.BookController">
3. <result name="login" type="tiles">login</result>
4. <result name="allBooks" type="tiles">booklist</result>
5. <result name="booksByCategory" type="tiles">booklist</result>
6. <result name="searchByKeyword" type="tiles">booklist</result>
7. <result name="home" type="tiles">home</result>
8. <result name="executelogin" type="tiles">executelogin</result>
9. <result name="selectedBooks" type="tiles">selectedBooks</result>
10. <result name="logout" type="tiles">logout</result>
11. <result name="error" type="tiles">error</result>
12. </action>
The * in the Line 1 of the Listing 4-42 is the wildcard character. Any action name values that end in
Link
will be handled by this action mapping. Whatever value is before
Link
will be the value used for
the method attribute (the {1} place holder will be replaced with that value). So instead of writing nine
separate action mapping nodes in the configuration file for this simple application you can simply
use the wildcard character, *, in your name value and an attribute value place holder ( {1} ) for the