trainingtrains Logo

91-9990449935

 0120-4256464

Struts 2 Zero Configuration by convention

We can create struts 2 application without the configuration file struts.xml. There are two ways to create zero configuration file:

  • By convention
  • By annotation

The steps to create zero configuration file using convention is as follows:

  1. Create input page (optional)
  2. Create the action class
  3. Create view components

Example to create zero configuration file by convention.

In this example, we are creating 4 pages :

  1. index.jsp
  2. LoginAction.java
  3. login-success.jsp
  4. login-error.jsp

1) Create index.jsp for input (optional)

This jsp page creates a form using struts UI tags. It receives name and password from the user.

index.jsp

2) Create the action class

The action class name must be suffixed by action following the request name or it must implement the Action interface (or extend ActionSupport). Suppose that request name is login, action class name should be LoginAction if you don't want to implement Action interface.

The action class must be located inside the action or actions or struts or struts2 package.

LoginAction.java

3) Create view components

The view components must be located inside the WEB-INF/content folder.

The view components must be named with request name - (hyphen) string returned by action class. Suppose that request name is login and string returned by action class is success and error, file name must be login-success.jsp and login-error.jsp.

login-success.jsp
login-error.jsp