ModelDriven action and OGNL in Struts 2

Struts 2 has a set of tags that helps in controlling the application flow more easily. They may be control tags, data tags, form tags or ajax tags. While creating the demo application, we will be using some of the Struts UI tags, that are very much similar to the plain HTML tags and won’t be much difficult for you to understand.

To start with, let’s create some views inside the /WebContent directory of our project:

index.jsp

error.jsp

Depending on the fields that we have created in index.jsp, we must have a POJO class UserBean.java as well to be present to accept the form inputs.


Create a new package as com.jcombat.beans in the /src directory of our project and create UserBean.java

Now let’s create a mapping in struts.xml that would be deciding the navigations on the basis on actions we invoke. Make sure, struts.xml lies inside the /src directory of our project.

I hope you might be familiar to writing struts.xml as we had discussed in detail in our previous Struts tutorial. You can still navigate to http://www.jcombat.com/struts/create-your-first-login-application-using-struts if you need to recall.

Note that on form submit, we have an action UserAction that gets invoked.

Let’s create a package named com.jcombat.action in the /src directory of our project and create UserAction.java.

To understand, we are not using the usual POJO class as our action class. Instead, we preferred Model Driven here, enabling the application to set the respective UserBean properties automatically. All you need to do is, extend the ActionSupport class and implement the ModelDriven interface.

execute() and validate() are the default action method and validation method, if none are specified explicitly in the struts.xml.

Before you run the application, make sure to configure the /WebContent/WEB-INF/web.xml, as it is the entry point for any request to Struts 2, which needs to have the FilterDispatcher defined. So make sure, the web.xml looks something like:

We are now good to create result.jsp inside the /WebContent directory of our application to display whatever the end user submitted.

result.jsp

Note that we have used OGNL to display the submitted form data.

The Object-Graph Navigation Language (OGNL) is a powerful expression language. It is used to reference and manipulate data on the ValueStackOGNL is based on a context and Struts builds an ActionContext map for use with OGNL. The ActionContext map consists of:

  • application – application scoped variables
  • session – session scoped variables
  • root / value stack – all your action variables are stored here
  • request – request scoped variables
  • parameters – request parameters
  • attributes – the attributes stored in page, request, session and application scope

It is important to understand that the Action object is always available in the value stack. Therefore, if your Action object has properties a and b, they are readily available for you to use as we have used in the result.jsp above.

If you still feel the need to read more on this, you can navigate to http://www.tutorialspoint.com/struts_2/struts_value_stack_ognl.htm

Snapshots:

Struts form

 

On submitting the form with the entered data (result.jsp),

After form Submit

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.