In this article, we will create a simple Hello World web application using Struts and we will deploy it on Tomcat server.
We can create a Struts application by manually configuring it. For this, we can create a web application in eclipse and download the struts jars from http://struts.apache.org/download.cgi and add them in the project lib folder.
But in this example, we will use Maven to create the struts web application for us.
Here are the tools & technologies used :
- Struts 1.3.10
- Eclipse
- Maven
- Tomcat server
So, lets start with creating an empty Maven Web application using following archetype :
mvn archetype:generate -DarchetypeArtifactId=maven‐archetype‐webapp -DgroupId=com.topjavatutorial.webapp -DartifactId=StrutsHello -DinteractiveMode=false
If you need help getting started with Maven, you may refer this article :
Now, import the StrutsHello project in Eclipse and add the following struts dependency in the pom.xml:
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts-core --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-core</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency>
Your project structure should look like this at this point :
If you don’t see src/main/java in Project explorer or Package explorer, refer this article on how to fix it :
How to fix missing src/main/java folder
Now the project structure is ready. Lets create the required artifacts for the Hello World struts project:
- Create Action Form
- Create Action class
- Create JSP page
- Create struts-config.xml
- Modify web.xml
Action Form
Create a Struts Action Form to hold the data that we want to display on screen.
import org.apache.struts.action.ActionForm; public class HelloForm extends ActionForm{ private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
Action Class
Create an action class HelloAction.java by extending org.apache.struts.action.Action class.
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class HelloAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { HelloForm helloForm = (HelloForm) form; helloForm.setMsg("Hello from Struts !!"); return mapping.findForward("success"); } }
JSP Page
Create a JSP Page for displaying the msg variable of HelloForm.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Hello world</title> </head> <body> <bean:write name="helloForm" property="msg" /> </body> </html>
struts-config.xml
Create a struts configuration file struts-config.xml in WEB-INF folder :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="helloForm" type="com.topjavatutorial.app.HelloForm" /> </form-beans> <action-mappings> <action path="/helloWorld" type="com.topjavatutorial.app.HelloAction" name="helloForm"> <forward name="success" path="/Hello.jsp" /> </action> </action-mappings> </struts-config>
In the config file, create entries for form bean and action class as shown above.
Also, add the forward for success.
Modify web.xml
In web.xml file, configure the Struts ActionServlet and provide servlet-mapping for url-pattern “*.do”.
This way, all the “*.do” pattern urls will get redirected to the ActionServlet.
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
Here is how the project structure looks like at this point :
Now, if you deploy the web application on Tomcat server and try the url “http://localhost:8080/StrutsHello/helloWorld.do”, you should see “Hello from Struts !!” message.
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post
Nvermind, fixed it. Seems some javax.servlet collisions occur when using Tomcat. Need to modify web.xml to use the “provided” scope:
javax.servlet
servlet-api
2.5
provided
javax.servlet.jsp
jsp-api
2.1
provided
org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.Hello_jsp . This doesn’t work.