Showing posts with label Struts2. Show all posts
Showing posts with label Struts2. Show all posts

Saturday, 23 April 2016

What are the main elements in struts.xml file ?


<struts> tag
Root tag for the struts.xml
It may contain the tags :  <package>, <include>, <bean> and <constant>


<package> tag 
It groups actions, results, result types, interceptors, and interceptor-stacks into a logical configuration unit.
1. name
Mandatory attribute which is an unique name for the package.
It acts as the key for later reference to the package.

2. extends
Optional attribute which allows one package to inherit the configuration of one or more previous packages - including all interceptor, interceptor-stack, and action configurations.
All configuration information (including action configurations) from the extended package will be available in the new package, under the new namespace.

3. namespace
It provides a mapping from the URL to the package.
Example : For 2 different packages, with namespace attributes defined as "pack1" and "pack2", the URLs would be something like "/webApp/pack1/my.action" and "/webApp/pack2/my.action"

4. abstract
If this attribute value is "true" the package is truly a configuration grouping and actions configured will not be accessible via the package name.
It is important to make sure you are extending the correct parent package so that the necessary pre-configured features will be available to you.


<include> tag
This tag is used to modularize a Struts2 application that needs to include other configuration files.
It contains only one attribute "file" that provides the name of the xml file to be included.
This file has exactly the same structure as the "struts.xml" configuration file.
For example, to break a configuration file of a finance application, you might choose to group together the invoices, admin, report configurations etc into separate files.


<bean> tag
It requires the class attribute which specifies the Java class to be created or manipulated.
A bean can either be created by the framework's container and injected into internal framework objects, or have values injected to its static methods.

The first use, object injection, is generally accompanied by the type attribute, which tells the container that which interface this object implements.
The second use, value injection, is good for allowing objects not created by the container to receive framework constants. Objects using value inject must define the the static attribute.
1. class - Name of the bean class
2. type - Primary Java interface this class implements
3. name - Unique name of this bean; must be unique among other beans that specify the same type
4. scope - Scope of the bean; must be either default, singleton, request, session, thread
5. static - whether to inject static methods or not


<constant> tag
There are two key roles for constants : 
1. They are used to override settings like the maximum file upload size or whether the Struts framework should be in devMode (development mode) or not. 
2. They specify which Bean should be chosen, among multiple implementations of a given type.

Constants can be declared in multiple files.
By default, constants are searched for in the following order, allowing for subsequent files to override by the previous ones:
struts-default.xml  >  struts-plugin.xml  >  struts.xml  >  struts.properties  >  web.xml

What filter chain includes in architecture of Struts 2x framework ?


The filter chain includes : 

ActionContextCleanUp filter
  • This filter is optional and it is useful when integration has to be done with other technologies like SiteMash Plugin.

FilterDispatcher
  • Next, the FilterDispatch is called which in turn uses the ActionMapper to determine weather to invoke an Action.
  • If the action is required to be invoked, FilterDispatcher delegates the control to ActionProxy.

ActionProxy
  • The ActionProxy takes the help from Configuration Files manager, which is initialized from the struts.xml.
  • Then the ActionProxy creates an ActionInvocation, which implements the command pattern.
 
  • The ActionInvocation process invokes the Interceptors (if configured) and then invokes the action.
  • The the ActionInvocation looks for proper result.
  • Then the result is executed, which involves the rendering of JSP or templates.

  • Then the Interceptors are executed again in reverse order.
  • Finally the response returned through the filters configured in web.xml file.

  • If the ActionContextCleanUp filter is configured,
    • The FilterDispatcher does not clean the ThreadLocal ActionContext.
  • If the ActionContextCleanUp filter is not present then,
    • The FilterDispatcher will cleanup all the ThreadLocals present.

How we can configure constants in Struts 2 ?


Constants provide a simple way to customize a Struts application by defining key settings that modify framework and plugin behavior.

Constant in struts.xml
<struts>
  <constant name="struts.devMode" value="true" />
  ...
</struts>


Constant in struts.properties
struts.devMode = true


Constant in web.xml
<filter>
   <filter-name>struts</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
   <init-param>
      <param-name>struts.devMode</param-name>
      <param-value>true</param-value>
   </init-param>
</filter>

How to develop a Struts 2 application ?


Developing Struts 2 application

Make the directory structure
 1. Make source folder. Put following items:
     a. package with a class.
     b. struts.xml
     c. hello.xml
 2. Make jsp folder under webroot.
     Put jsp file inside it.
 3. Put empty applicationcontext.xml under WEB-INF.
     Put web.xml under WEB-INF.
 4. Make index.jsp in application root folder.

Add JARs in WEB-INF/lib folder
  •  antlr-2.7.2.jar
  •  commons-beanutils-1.6.jar
  •  commons-chain-1.1.jar
  •  commons-logging-1.0.4.jar
  •  commons-validator-1.3.0.jar
  •  freemarker-2.3.8.jar
  •  ognl-2.6.11.jar
  •  oro-2.0.8.jar
  •  struts-core-1.3.5.jar
  •  struts2-codebehind-plugin-2.0.6.jar
  •  struts2-config-browser-plugin-2.0.6.jar
  •  struts2-core-2.0.6.jar
  •  struts2-sitemash-plugin-2.0.6.jar
  •  struts2-spring-plugin-2.0.6.jar
  •  struts2-struts1-plugin-2.0.6.jar
  •  xwork-2.0.1.jar
  •  spring.jar
  •  commons-digester-1.6.jar
  •  commons-collections-3.1.jar


Give entries in web.xml for filter and filter-mapping (struts2), Spring listener, faces listener. 

Code struts.xml (keep it in classpath). 
Give the entry of xml file you want to use as struts configuration, include that file (like: hello.xml) using include tag.

Make struts configuration XML
Give DTD, package name, namespace name, action definition having class name and action name.

Code the applicationContext.xml (Spring configuration)

Code the action class.

Write the JSP file : /Webroot/jsp/HelloWorld.jsp

What is ValueStack and how it is used ?


What is ValueStack ?
  • The ValueStack builds a stack of objects.
  • Objects are examined to find property values.
  • The ValueStack allows the expression language to find property values across multiple objects.


How is the ValueStack used ?
  • The Action instance is always pushed onto the ValueStack.
  • The Model is pushed on by the ModelDrivenInterceptor.
  • The UI tags use it to push values on during their scope and evaluate expressions.
  • The s:iterator tag pushes the current item onto the stack.
  • The s:bean tag pushes a bean instance on the ValueStack.
  • The s:property tag evaluates an expression against the ValueStack.


All tag attribute values are evaluated against the stack when being set onto the tag instances.

How to use ModelDriven interface ?


Two ways to collect input for Actions :

1. Model-driven
  • Action has methods returning your model classes (myAction.getUser())
  • Fields in the view are fields of your model
  • Views refer directly to your model (property=‘user.name’)
  • Excellent because it allows model reuse


2. Field-driven
  • Action has fields of its own, which are fields in the view
  • execute() collates fields and interacts with the model
  • Views refer to action fields (property=‘daysTillNextBirthday’)
  • Useful where form is not parallel to model


The two can be mixed and used.


ModelDriven Interface
  • Direct support of model-driven Actions
  • The ModelDriven Interface has one method :public Object getModel()
  • Properties of the model are flattened :“name” instead of “user.name”
  • Applies to UI tags, form field names, and so forthpublic class Birthday extends ActionSupport          implements ModelDriven {
       …
      public Object getModel() {
        return user;
      }
    }


Applying the ModelDrivenInterceptor

In struts.xml
<action name=“birthday" class=“happy.Birthday">
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="defaultStack"/>
<result>/Model/success.jsp</result>
<result name="input">/Model/form.jsp</result>
</action>

The ModelDrivenInterceptor pushes the Model onto the ValueStack.