Posts

Showing posts from January, 2017

ValueStack and it Object Stack and Context Map

ValueStack is the platform or playground for the Struts 2 framework for its request handling.  Consider a user has sent request to the Struts 2 application.  First a ValueStack object is created for that request and then the objects like ACTION, Model and any Java resources that are needed are created. These objects references are maintained in the ValueStack. Apart from the Objects created, we might also need the objects like request attributes, Session attributes and Application context to set and access the objects in those scopes. The ValueStack also gets the references to these objects as well. So, if you consider ValueStack , then you should keep in mind that this object is like an address book or contact list for all the objects that are needed to handle this request. One of the major advantages of Valuestack is that any object with which we work or the model objects are available to the response UI pages through the OGNL expression language. We ...

OGNL introduction

OGNL is the Object Graph Navigation Language which is an opensource framework from Apache Commons project and is used in setting and getting properties from the Java Beans. OGNL also enables to invoke the methods from the Java classes. OGNL was primarily designed to use with the UI forms to set the form values to the Java Beans. OGNL was integrated to struts 2 when the Webworks framework was integrated and a lot of hardwork was put to bring in the OGNL into the Struts 2 to make it simple to use for the developers. Role of OGNL in Struts 2 OGNL helps in three areas in Struts 2 during the request handling process. OGNL offers the expression language which can be used in the forms and response pages. OGNL helps in the type conversion during setting the form values to the Java beans types. Helps in looking for objects in ValueStack .

Validation in detail

Struts 2 validation is configured via XML or annotations. Manual validation in the action is also possible, and may be combined with XML and annotation-driven validation. Validation also depends on both the validation and workflow interceptors (both are included in the default interceptor stack). The validation interceptor does the validation itself and creates a list of field-specific errors. The workflow interceptor checks for the presence of validation errors: if any are found, it returns the "input" result (by default), taking the user back to the form which contained the validation errors. If we're using the default settings and our action doesn't have an "input" result defined and there are validation (or, incidentally, type conversion) errors, we'll get an error message back telling us there's no "input" result defined for the action. Turning on Validation The default interceptor stack, "defaultStack...

Validation explained

Stuts2 uses XWork's validation framework internally.  It allows you to validate Models and Actions using a set of specialised (field)validators, grouped together in an xml file named YourModel-validation.xml or ActionName-validation (and, in the case of the alias, ActionName-alias-validation.xml). Since we only want to validate the crud action, we create a file EmployeeAction-crud-validation.xml and place it in our classpath (mostly next to our compiled Action class). < validators >    < field name = "employee.firstName" >       < field-validator type = "requiredstring" >          < message key = "errors.required.firstname" />        </ field-validator >    </ field >    < field name = "employee.lastName" >       < field-validator type = "required...

Value Stack Importance

The ValueStack is like a normal stack.  You can put objects on it, remove them from it, and query it. And even more, you can query it with expressions!  It's the heart of Struts 2, and allows easy access to a wide range of objects from nearly any place in the framework: interceptors, results, tags, ... you name it. Your action is placed on top of the stack when it is executed. Once it's on the valueStack, you could query for it 'employees' - a special glue language called OGNL will then transform that request to 'top.getEmployees()' - and since top is the top of your stack, and your Action is located just there, it will invoke the getEmployees() method on your Action, and return the result.

Predefined Result Types

The framework provides several implementations of the com.opensymphony.xwork2.Result interface, ready to use in your own applications. Chain Result Used for Action Chaining Dispatcher Result Used for web resource integration, including JSP integration FreeMarker Result Used for FreeMarker integration HttpHeader Result Used to control special HTTP behaviors Redirect Result Used to redirect to another URL (web resource) Redirect Action Result Used to redirect to another action mapping Stream Result Used to stream an InputStream back to the browser (usually for file downloads) Velocity Result Used for Velocity integration XSL Result Used for XML/XSLT integration PlainText Result Used to display the raw content of a particular page (i.e jsp, HTML) Tiles 2 Result Used to provide Tiles 2 integration Tiles 3 Result Used to provide Tiles 3 integration Postback Result Used to postback request parameters as a form to the specified destination JSON Result Used to serialize act...

About packages

Image
Packages The package element has one required attribute, name , which acts as the key for later reference to the package. The extends attribute is optional and allows one package to inherit the configuration of one or more previous packages - including all interceptor, interceptor-stack, and action configurations. Note that the configuration file is processed sequentially down the document, so the package referenced by an "extends" should be defined above the package which extends it. The optional abstract attribute creates a base package that can omit the action configuration. Attribute Required Description name yes key to for other packages to reference extends no inherits package behavior of the package it extends namespace no see Namespace Configuration abstract no declares package to be abstract (no action configurations required in package) Simple usage Package Example (struts.xml) <struts>    < package name= "em...

Object Factory

All objects created by the framework are instantiated by the ObjectFactory.  The ObjectFactory provides the means of integrating the framework with IoC containers like Spring, Pico, Plexus, and so forth. Customize Extend ObjectFactory Customized ObjectFactory must extend ObjectFactory or any of its descendants and have a default, no-argument constructor. To register a customized ObjectFactory, add or edit an entry in struts.properties struts.objectFactory=foo.bar.MyCustomObjectFactory where foo.bar.MyCustomObjectFactory is the custom object factory. public class MyObjectFactory extends ObjectFactory {      ..... } Define dedicated factory If you want to just extend one part of ObjectFactory, ie. to change how Result Types are build, you can implement ResultFactory interface and register it with dedicated name, see Extension Points for more details. Original ObjectFactory will use these dedicated factories t...

Namespace Configuration

The namespace attribute subdivides action configurations into logical modules, each with its own identifying prefix. Namespaces avoid conflicts between action names. Each namespace can have its own "menu" or "help" action, each with its own implementation. While the prefix appears in the browser URI, the tags are "namespace aware", so the namespace prefix does not need to be embedded in forms and links. Struts 2 Namespaces are the equivalent of Struts Action 1 modules, but more convenient and flexible. Default Namespace The default namespace is "" - an empty string. The default namespace is used as a "catch-all" namespace. If an action configuration is not found in a specified namespace, the default namespace is also be searched. The local/global strategy allows an application to have global action configurations outside of the action element "extends" hierarchy. The namespace prefix can be registered ...