Struts project setup using bean XML

This tutorial will also help you to get overall idea of Struts project setup using bean XML with action class, jsp, 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

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

๐ŸŒ LinkedIn

๐Ÿ“ Blogs

๐Ÿ“ง Imran Khan