Notes · Struts 2 Notes

Struts 2 in Action Notes Chapter 1, Chapter 2

I am drafting some notes from Struts 2 in Action. I used this book to learn Struts 2 framework which was one of the few good resources available at the time when I started with the framework. While reading it, I made some notes for myself. I am compiling those here. I am just documenting the notes here for a quick reference when I don’t have an access to the book. I hope I am not violating any laws. But if I am, please let me know, I will take down the content. Also, at times, I have added my own text, here and there, based on my understanding. So the notes here may not match the text from the book. For better reference, please refer to the book.

Chapter 1

Definition of Struts 2 interceptor:

Interceptors are Struts 2 components that execute both before and after the rest of the request processing. They provide an architectural component in which to define various workflow and cross-cutting tasks so that they can be easily reused as well as separated from other architectural concerns.

*******

Definition of ValueStack:

Struts 2 uses the ValueStack as a storage area for all application domain data that will be needed during the processing of a request. Data is moved to the ValueStack in preparation for request processing, it is manipulated there during action execution, and it is read from there when the results render their response pages.

—the ValueStack is where your data is stored while you work with it,and that OGNL is the expression language that you, and the framework, use to target this data from various parts of the request-processing cycle.

*******

Chapter 2

Using annotations for declarative architecture


@Results({
    @Result(name="input", value="/Registration.jsp" )
    @Result(value="/RegistrationSuccess.jsp" )
})
public class Login implements Action {
    public String execute() {
        //Business logic for login
    }
}

A sample web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
						http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>S2 Example Application - Chapter 1 - Hello World
	</display-name>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>
		<init-param>
			<param-name>actionPackages</param-name>
			<param-value>manning</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<servlet>
		<servlet-name>anotherServlet</servlet-name>
		<servlet-class>manning.servlet.AnotherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>anotherServlet</servlet-name>
		<url-pattern>/anotherServlet</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

The value of init-parameter actionPackages passed to the FilterDispatcher should give the names of packages where we have used annotations (for example, @Result)

Use empty action components to forward to your results, even if they’re simple JSPs that require no dynamic processing. This keeps the application’s architecture consistent, prewires your workflow in anticipation of increases in complexity, and hides the real structure of your resources behind the logical namespace of the Struts 2 actions.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s