Creating a struts project using Eclipse Ganymede

Posted on Mar 22, 2009 | 1 Comment

Here is a short article that may help you while creating a struts project in Eclipse’s latest 3.30 i.e Ganymede.  Please follow the following steps :

1 . Create a Dynamic Web Project

 

2. Configure the Web Project


3. Configuration Continues….

4. The project looks like the following :

5. Put the following Jars to Project Build Path

You can download these struts distribution from http://archive.apache.org/dist/jakarta/struts/

6. Now create the packages for actions and forms as shown in the image below

7. Here is the listing for the web.xml

Web.xml

xml version=“1.0″ encoding=“UTF-8″?>

<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns=“http://java.sun.com/xml/ns/javaee” xmlns:web=“http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=“WebApp_ID” version=“2.5″>
<display-name>StrutsTest</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>mapping</param-name>
<param-value>org.apache.struts.action.RequestActionMapping</param-value>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
<servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

8. Then the listing for struts-config.xml . Put this file under WEB-INF as mentioned in web.xml
Struts-config.xml

xml version=“1.0″ encoding=“ISO-8859-1″ ?>

DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.0//EN”
“http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd”>
<struts-config>
<form-beans>
<form-bean name=“starter” type=“com.deb.struts.forms.NewForm” />
</form-beans>
<global-forwards>
</global-forwards>
<action-mappings>
<action
path=“/starter”
name=“starter”
type=“com.deb.struts.actions.NewAction” >
<forward name=“valid” path=“/index.jsp” />
<forward name=“invalid” path=“/error.jsp” />
</action>
<action-mappings>
</struts-config>

9. Here comes our action class NewAction.java. Please be careful about package name.

package com.deb.struts.actions;
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;
import com.deb.struts.forms.NewForm;
publicclass NewAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse resp)
{
NewForm nf = (NewForm)form;
req.setAttribute(“NAME”, nf.getName());
return mapping.findForward(“valid”);
}
}

10……. and the action form bean
NewForm.java:

package com.deb.struts.forms;
import org.apache.struts.action.ActionForm;
publicclass NewForm extends ActionForm{
/**
*
*/
privatestaticfinallongserialVersionUID = 1L;
private String name =null;
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
}

11. Here are the two JSPs
The login.jsp
DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>

<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1″>
<title>Insert title here</title>
<head>
<body>
<form name=“login” action=“starter.do”>
Please write name here
<input type=“text” name =“name” />
<input type=“submit”/>
</form>
</body>
</html>
The index.jsp
_____________________________
DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1″>
<title>Insert title here</title>
<head>
<body>
The name was :
</body>
</html>
______________________________
The error.jsp
DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1″>
<title>Insert title here</title>
<head>
<body>
ERROR
</body>
</html>

12. Finally Your project looks like the following

13 . Now, you can build and deploy the project to a Server.

14. Once deployed hit the login.jsp page

The page shown above appears.
Enter some text in the text box as shown below

Click on the ‘Submit Query’ button.
Then this page should appear…..

11

This confirms that you have successfully created and deployed your struts project .
Happy Coding

[The Author Deb Kumar Basu is working as a Consultant in a leading MNC. He has provided great tips on how to use the latest version of Eclipse Ganymede to develop struts applications smoothly. If you want to develop struts projects but do not want to use RAD or WSAD from IBM due to cost then this is a solution for you.
The Author is always looking for simple inexpensive solutions for J2EE development. If you need more information on this please touch base with the Author through comment in the article.
]

TechcubeTalk Staff

This post/article/tutorial is written and published by TechCubeTalk.com staff (Blog editors, paid contributors or a guest contributors). TechCubeTalk.com reserves all rights and no part of this article can be published anywhere without prior written permission.

1 comment

  1. Parker says:

    Hey, from slide number 5, you mention struts.jar. Is this a Struts 1 thing or Struts 2? As far as I can tell, Struts 2 provides many JARs (“struts2-core,” “struts2-codebehind…”). -Parker

Leave a comment


2 × one =

Advertisement

Subscription

You can subscribe by e-mail to receive news updates and breaking stories.