Java Reference
In-Depth Information
In Listing 4-8, the action mapping maps the logger interceptors to the HelloWorldAction action class
via the interceptor-ref element. Since the HelloWorldAction is declared its own interceptors, it
loses the default set of interceptors, and you have to explicitly declare the defaultStack in order to
use it, as illustrated in Listing 4-9.
Listing 4-9. Declaring a Default Stack
<package name="default" namespace="/" extends="struts-default">
<action name="helloAction"
class="HelloWorldAction" >
<interceptor-ref name="logger"/>
<interceptor-ref name="defaultStack"/>
<result name="success">/hello.jsp</result>
</action>
</package>
ValueStack and OGNL
The Object-Graph Navigation Language (OGNL 1 ) is a powerful expression language that is used to
set and get properties from JavaBeans and to invoke methods from Java classes. It also helps in
data transfer and type conversion. OGNL is similar to EL and JSTL, which evaluate expressions and
navigate object graphs using dot notation. As you saw in Figure 4-2 , OGNL and the ValueStack ,
though not part of MVC, are at the core of the Struts 2 framework. All the MVC components interact
with ValueStack to provide contextual data. These components access ValueStack using OGNL
syntax, and OGNL and ValueStack work together in Struts 2 to handle a request. Specifically, when
a request is sent to the Struts 2 application, a ValueStack object is created for that request, and
the references to all the objects that are created to serve that request as well as scope attributes
are maintained in the ValueStack . All these objects are available to the view through OGNL. You will
not find OGNL difficult to use because it is similar to EL and JSTL (which are covered in Chapter 3).
Listing 4-10 illustrates how OGNL looks. Notice that OGNL uses # , unlike JSP EL, which uses $ .
Listing 4-10. Using OGNL
<s:property value="#book.bookTitle" />
ResultType and Result
In Struts 2, the rendering of the response consists of the result type and the result. The result type
provides the implementation details for the type of view that is returned to the user. Each method on
an action returns a result, which includes specifying the result type. If no result type is specified, then
the default result type is used, which forwards to a JSP page, as illustrated in Listing 4-11.
Listing 4-11. Default Result Type
<result name="success">
/hello.jsp
</result>
1 http://commons.apache.org/proper/commons-ognl/
 
Search WWH ::




Custom Search