Struts2 using bean XML

This will help us to create Struts2 project using bean XML project having action class, JSP, struts.xml, web.xml, etc.

Apache Struct 2 is an open source framework to build Java Enterprise Edition web applications. It is based on Model View Controller (MVC) design pattern. Action, Interceptors, ValueStack, OGNL are important features of Struts 2.

Struts 2 is the combination of webwork framework of opensymphony and struts 1.

We are going to use Eclipse as an editor to develop Struts application.

Follow below steps to create our very first Struts application project.

  1. Open Eclipse.
  2. Click on File in top navigation menu and than click on new. There will be an option Dynamic Web Project start coming. As soon as we select select Dynamic Web Project below scree will start get appearing.

Provide below highlighted information color in red and click on Next.

3. Below screen will appear. Click on Next button as no changes are required.

4. In below screen select below highlighted checkbox to generate web.xml file also as part of this project.

Click on Finish will create struts project.

5. Open Struts official website link to download below highlighted dependency.

6. Unzip the download file and copy the libs folder. Paste and replace the same inside below libs folder highlighted red in color.

7. Create index.jsp file inside WebContent folder and place below content as shown below.

8. Create user.jsp file inside WebContent folder and place below content as shown below.

9. Create error.jsp file for future reference inside WebContent folder and place below content as shown below.

10. Right click on src folder and create a package with name com.struts.javadoubts and create UserController.java file with below content.

Note: Never start package name with java as it is not allowed. e.g. java.struts.javadoubts

11. Go to web.xml and add below code

12. For Struts application, it is require to create a struts.xml file inside src folder as shown below:

Created a custom action name as user with the help of below tags and attributes:

<action> tag will help us to create an action.

class: class attribute in action tag will take class with complete package name as value.

method: Declare method name which which belong to a class declared in class attribute in action tag.

name: name attribute in action tag is to map an action which will get call after hitting below url as an example.
e.g. → http://localhost:8081/practic/user.action

<result> result tags will maintain a mapping. This mapping will be responsible for calling an action or interceptor at the time of the request reaching the server and also for loading a specific JSP as in response.

Below UserAction will return “success” as a String which will get match

13. Right click on the project and than Run As -> Run on Server will print the out on the page.

http://localhost:8081/struts-demo

http://localhost:8081/struts-demo/user.action

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0