Struts2

Getting Started with Struts2

How to Create  Simple Project Struts2( step by step ) using Maven in Eclipse :

Struts, an open-source Apache, is a MVC (Model-View-Controller) framework for creating user interfaces for Java web application. Struts is an extension of Java Servlets and JSP. Struts is in direct competition with JSF (Java Server Faces).

This application will show you how to create a JEE web application based on Struts 2 using Maven that can manage a single product entity with the ability to add remove, edit and update products in a single view.

In the first part, we assume that the products are stored in a HashMap type of collection without using a database.

We will show you :

  • How to create a Maven project using Struts2
  • How to configure Struts2 in struts.xml file.
  • How to Create The ModelAction.
  • Create views using JSP struts-tags.
  • How to validate the form with Struts.
  • Struts2 Localization, internationalization

In the second part, we assume that the products are stored in MySQL database 

We will show you :

  • How to integrate Struts2 with Spring and JPA/Hibernate.
  • How to integrate Spring with Struts2.
  • Dependency Injection Using Spring IOC
  • Data persistence with Hibernate JPA.
  • Transactions with Spring.

Before performing this project, we need to meet the following prerequisites:

Eclipse (Luna) Java EE IDE for Web Developers :

Now Let’s get started

First Part :

How to create a Maven project type Struts2

Open Eclipse and create new Project –> Others —> Maven Project and Next.

1

We need to configure a catalog to use struts in the maven project so just select configure.

2

Search Maven and select Archetype.

Add Remote Catalog and enter this URL http://struts.apache.org/archetype-catalog.xml.

add a simple description in our example is Struts2 and then Select Ok.

3

The Remote struts catalog  are created, select then apply and Ok

4

Select Catalog : Struts2 and choose struts2-archetype-blank and Next.

5

Choose a name of Group Id in our case : org.project and an artifact Id : myProjectStruts2.

6

You will find a demo project struts “Hello World example”. You can have a look 🙂

In our case we will delete all the file and package for this example to understand better struts.

7

Also delete the configuration by default in struts xml file : struts.xml

9

Fine, your project is empty and should look like this in your Project Explorer window:

8

How to configure struts.xml:

Package configuration: Packages are a way to group actions, results, result types, interceptors, and interceptor-stacks into a logical configuration unit.

The package element has one required attribute, name, which acts as the key for later reference to the package. The extends attribute is optional.

In our case the name is default, the namespace is “/” and extends  from struts-default.xml

A base configuration file named struts-default.xml is included in the struts2.jar file. This file is automatically included into struts.xml file to provide the standard configuration settings without having to copy them.

Action name is “index” and the result of this action in the JSP file index.jsp.

Create the following configuration :

10

Following figure shows the MVC / Struts2 Architecture :

struts

Let’s create a new folder ressource : views

11

Create a new JSP file in the views folder and Finish.

12

Delete the configuration by default using in the JSP file, we assume that we will work with HTML5.

we just need to test the configuration so just add if you want : Hello world in our case we put Test in the body.

13

Run the appilication on the server tomcat 7.

tomcat

Add the project on the server and Finish

tomcat2

I shall assume that you have installed and configured Tomcat server. I shall also assume that Tomcat is running on port 8080 : from follownig this link http://localost:8080/MyPojectStuts2/index

14When we follow this link  http://localost:8080/MyPojectStuts2/ we have an excpetion : there is no Action mapped for namespace.

we can add a default action without using /index so just  add inside the package a default action reference named index.15

How to Create The ModelAction :

Create a new Java class. in the package org.project.entity

16

The product is defined by a string reference and name, have a double price and an int quantity:

17Generate constructor with paramater and a constructor without paramater.

Generate getter and Setter.

18

Create an interface IproductDAO in the package dao.

method to add , delete and update a product and show list of the product and get a product.

19

Create a ProductDAOImpl to implements the method from the interface IProductDAO. Select Add ans search IProductDAO to add the interface implementation.

20

Without using a database we assume that we will store the product in HashMap List.

A HashMap Use the unique key String is the reference of the product.

21

Our list is empty by default so we need a method init to store a product to use it.

22We need also to create another interface Service Product and we assume that we have the same method in the interface DAO.

23Create a new Java class ProductServiceImpl to implements the methods used in the interface Service.

Just we need a field named dao in our case and have Setter to use it when we will create a Product Action and the injection dependency without using Spring IOC or EJB.

24

we assume that only one instance of a class is created using singleton without Spring IOC or EJB.

Create a new Java Class : SingletonService have a private static field instance name service with a getter.

in the static bloc will be called only once :

This is a basic configuration for Injection dependency without using a container IOC (Spring or EJB).

25

Create a new Java class : Production Action extends from ActionSupport, have a private Poduct and we must genereate the setter and the getter or keep it public because Struts will use it.

Create a method index and it returns success.

29

Add a new action name product from the class org.project.web.ProductAction.

method index and the result if succeded in another JPS page product .jsp

24.1

So In the Index Page we want to create a link name Product a link a href to redirect for another JSP file named Product.JSP.

  • The taglib directive declares the struts 2 tags, with prefix 's'.

Add a taglib struts2 tags with a prefix s in our case. you can choose the prefix that you want.

26

Run the appliction on the Server.

 

27Create another page product.jsp and andd the taglib struts.

The <s:form> defines a HTML form, with processing action of “save“. The “save” action is mapped to “save" class (in “struts.xml“).

 

28Add new table to show the list of the product.

31

Run on the server :

32

Create a simple css and add it in the webapp/css/style.css.

33add  <link rel=”stylesheet” type=”text/css” href=”css/style.css”> in product.jsp

Run on the server

34

Now we need to create another method save to add new product.

also we need to add new action name save use the method save and the result into our single page product.jsp

35

We can test the action save.

36

Now we want to add delete and edit. we use struts taglib to add two links one for delete and another for edit a product.

37

We must show the following results

38

We must to add a new action in struts.xml

action for delete with method delete and action for edit with method edit

39

Also we need to add the method in the ProductAction

editRun on the server :

When we edit a product and save, we have another product is addes, so we must configure this issue.

40

We just declare a new filed in the controller and the view type boolean :

if isEdit==true so update the product

if isEdit ==false so add new product

ifedit We need to add new <s:hidden> tag to create a HTML hidden field.

isedit

How to validate the form with Struts

Create a new XML file should name your class in ModelAction-action-validation.xml

In our case the Model Action is ProductAction, action is save : ProductAction-save-validation.xml

41

Create a new file named package.properties.

42

We have an excpetion : no result defined when we add an empty product

43

To resolve this issue we must add a result name is input to define the result in the same page.

44

Run on the server

45

Internationalization:

Struts 2 has the ability to use multiple property files provided the property file is found in the package hierarchy, a properties file with keys and values that can be referenced from multiple view pages and those view pages are rendered after executing different Action classes.

46

Create two file :

package_en.properties : English language

package_fr.properties : Frensh language

en-fr

package_en.properties :

The Struts 2 key attribute can be used in the textfield tag to instruct the framework what value to use for the textfield’s name and label attributes :

en

package_fr.properties :

frAdd a key in the texfiled :

labelkey

we assume that you use Mozilla : so just go the preference and select Content and choose your language.

48

In our case we choose fr language

50

Run on the Server and show the results.

I hope it will be useful for you all.

See you soon to complet the Next Part to integrate Struts2 with Spring.

Leave a comment