Learn Java for Web Development

(Tina Meador) #1
CHAPTER 4: Building a Web Application Using Struts 2 201

30.
31.
32.
33.
34.
35.</s:if>
36.</s:iterator>
37.
39.
40.
42.</s:iterator>
43.
45
47.

49.
51.</s:form>
52.
53.


54.


Listing 4-45 illustrates the usage of Struts 2 tags and how Struts 2 OGNL is used to navigate the
property bookTitle on line 27 and the properties firstName and lastName (of the author) on
lines 33 and 34. Line 31 illustrates the usage of a simple and nested s:iterator. Line 26 allows the
user to select books to add to the shopping cart. The user can check the books to select and submit
them by clicking the button on line 49. When the user clicks the Add to Cart button, the action
name selectedbooksLink on line 8 and the bookId on line 26 are sent to the container, which is then
mapped to theAddToCartaction via struts.xml. The AddToCart action then stores the book to the cart
in the database, and the AddToCart action returns the String success, which is mapped via action
mapping in struts.xml and the tile in the tiles.xml file to the view selectedBooks.jsp. The user
can click the Purchase button on the selectedBooks.jsp page to call PurchaseAction in the same
manner. This chapter sketched a brief overview of Struts 2. For more detailed coverage of Struts 2,
I recommend Practical Apache Struts 2 Web 2.0 Projects by Ian Roughley.


Summary


In this chapter, you saw the Struts 2 framework’s core components, which follow the MVC design
pattern, and you saw how Struts 2separates the cross-cutting concerns by means of interceptors. You
saw how the Struts 2 framework provides a declarative architecture in two forms: XML and annotations.
In the next chapter, you will learn another action-oriented framework, called Spring Web MVC.

Free download pdf