Struts 2 Miscs



Struts 2 Miscs

Struts 2 vs Struts 1
1. There are no action forms in Struts 2.
In Struts 2, an HTML form maps directly to action properties or model object defined in action class.
2. Action is POJO.
Action needn't include any Struts2 specific API, even implementing the interface is optional, thus Action is easy to test, we don't need to mock HTTP objects.
3. Action Threading Model
In Struts1, Actions are singletons; there is only one action instance to handle all requests for that Action. Actions must be thread-safe and should not have instance variables.
In Struts2, Action objects are instantiated for each request, so no thread-safety issues any more.
Binding values into views and OGNL
Struts1 uses the standard JSP mechanism to bind objects into the page context to access.
However Struts 2 uses OGNL and ValueStack technology.
4. Enhanced Results, except JSP, Struts2 also supports other view technologies such as Free-marker, Velocity, PDF etc.
5. Simplified testability
Struts 2 Actions are HTTP independent and framework neutral. This enables to test struts applications very easily without resorting to mock objects.
6. Intelligent Defaults and Zero Configuration
Most configuration elements have a default value which can be set according to the need, using convention plugin, we can dramatically reduce configuration needed.
7. Annotations introduced
Struts 2 can use Java 5 annotations as an alternative to XML and Java properties configuration. Annotations minimize the use of xml.
8. Easy Spring integration - Struts 2 Actions are Spring-aware. Just need to add Spring beans!
9. Easy plugins - Struts 2 extensions can be added by dropping in a JAR. No manual configuration is required! We can easily write plugins to extend Struts2 functionality, and there are already many Struts2 plugin, such as rest, Json etc.
10. QuickStart - Many changes can be made on the fly without restarting a web container.


• Instead of a servlet controller like the ActionServlet class in Struts 1, Struts 2 uses a filter - FilterDispatcher to perform the same task.
• Use the Expression Language (OGNL) to access model objects from JSPs.

Design Philosophy in Struts 2
Struts splits task processing in its filter dispatcher into subcomponents called interceptors.
struts.xml file
Actions are grouped into packages, packages are like as modules.
It is advisable to divide it into smaller files and use include elements to reference the files.

Dev mode
In development mode, if you change the struts.xml file, you don't need to restart your web container.

Basic components of Struts 2
Filter dispatcher, actions, results, interceptors, OGNL.
Action
To access resources such as the ServletContext, HttpSession, HttpServletRequest, and HttpServletResponse objects either through the ServletActionContext object or by implementing Aware interfaces.
The latter is the recommended way that will make your action classes easier to test.
The ActionSupport Class
Result
An action method returns a String value. This value indicates to Struts what result to execute and where control should be forwarded to.
Result Type: dispatcher, chain, redirect, redirect-action, stream, plaintext, httpheader, xslt, freemarker, velocity.

Exception Handling with Action or Global Exception Mapping
Interceptors
The beauty of interceptors is they can be plugged in and out by editing the Struts' configuration file.
Type conversion
The Parameters interceptor is responsible for mapping request parameters with action properties.
Input validation
The validation interceptor is responsible for loading and executing registered validators.
Declarative Validation Using Configuration file and Bundled Validators
Programmatic Validation Using Validateable

OGNL
We use OGNL to access model objects, action properties in JSP.
For each action invocation, an object called the Value Stack is created prior to action method execution. The Value Stack is used to store the action and other objects. The Value Stack is accessed during processing (by interceptors) and by the view to display the action and other information.
With #, it will search the Context Map, without#, search will be conducted against the Object Stack.

Struts Tag Library - UI tags and non-UI tags
Model Driven and Prepare Interceptors
Why Model Driven?
public class EmployeeAction extends ActionSupport implements Preparable, ModelDriven {
    public void prepare() throws Exception {
        if (employeeId == 0) { employee = new Employee(); }
        else { employee = EmployeeManager.find(employeeId); }
    } 
    public Object getModel() { return employee; }
}

File Upload and File Upload Interceptor
File Download and Stream Result Type
Prevent Double Submits Using Token or Token Session Interceptor
The debug tag displays the content of the Value Stack and other objects.
Progress Meters and Execute and Wait Interceptor
Zero Configuration and Convention Plugin

Resources

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)