Javatpoint Logo
Javatpoint Logo

Struts 2 Annotations Example

Struts 2 provides you convenient way to create struts application using annotations. So, there is no need to have struts.xml file.

As we have said earlier, there are 2 ways to use zero configuration file (no struts.xml file).

  1. By convention
  2. By annotation

Annotations used in struts 2 application

For simple annotation example of struts 2, we can use 3 annotations:

1) @Action annotation is used to mark the action class.

2) @Results annotation is used to define multiple results for one action.

3) @Result annotation is used to display single result.

Example of struts 2 application using annotations

You need to create 4 files for struts annotated application:

  1. index.jsp
  2. Action class
  3. inside src directory
  4. Result page
  5. web.xml file

Let's have a look at the directory structure first.

directory structure of struts 2 application using annotation

1) Create index.jsp for input

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


2) Create the action class

This action class uses the annotations for the action and result.

3) Create file inside src directory

4) Create result.jsp to display message

This jsp page displays the username.



struts annotation example 1 struts 2 annotation example 2

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA