1:20 am - Wednesday January 25, 2017

Basic program using struts 1x with Example Program In Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Step by Step tutorial on running  your  SimpleProgram Struts application in Tomcat5.5 with Eclipse IDE

Step – 1  Creating Dynamic web Project

Step – 2  SimpleProgram  STRUTS folder structure

Step – 3  Creating your  SimpleProgram Struts program(Controller & Model)

Step – 4  Creating web.xml for SimpleProgram struts application

Step – 5  Creating Struts-config.xml

Step – 6  SimpleProgram  Struts view page

Step – 7  How to Add server in  SimpleProgram  Struts application

Step – 8  OutPut  for  Running  SimpleProgram  Struts application

SOURCE CODE  

Start Eclipse and goto File -> New -> Project -> Dynamic Web Project

Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from

JAR FILES + TLD FILES

2. SimpleProgram  STRUTS  folder structure:

 lib folder

Download The Following TLD files  and paste into WEB-INF

Step – 3 Creating your SimpleProgram Struts program(Controller & MyModel)

          

Controller in java(MyController.java)


package Com.Candidjava;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
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 MyController extends Action{
	public ActionForward execute(ActionMapping map,ActionForm fm,
								HttpServletRequest req,HttpServletResponse res)throws Exception{
		HttpSession ses = req.getSession(true);

		MyModel m = (MyModel)fm;
		String s=m.getName();
		ses.setAttribute("s",s);
		if(s.equals("")){
			return map.findForward("error");
 		}
		return map.findForward("success");

	}
}

Model in java(MyModel.java)

package Com.Candidjava;
import org.apache.struts.action.ActionForm;
public class MyModel extends ActionForm
{
private String name;
public void setName(String n)
{
name=n;
}
public String getName()
{
return name;
}
}

Step – 4 Creating web.xml for SimpleProgram struts application

XML file configuration:

Web.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<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>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
  <taglib>
    <taglib-uri>/struts-html</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
</web-app>

Step – 5 Creating Struts-config.xml

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
	<form-beans>
		<form-bean type="Com.Candidjava.MyModel" name ="model" />
	</form-beans>
	<action-mappings>
		<action path="/myActionForm"
		type="Com.Candidjava.MyController" name="model" input="/index.jsp">
				<forward name="success" path="/success.jsp" />
				<forward name="error" path="/fail.jsp" />

		</action>
	</action-mappings>
</struts-config>

Step – 6 DynaActionForm  Struts view page

Jsp file:                                                                                                                                                                                                                                                                          

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-html" prefix="html"%>
<!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 bgcolor="pink">
<br><br><br>
<html:html>
	<html:form action="/myActionForm" method="post">
		Enter Your Name:
		<html:text property="name" size="30" />
		<html:submit>Submit</html:submit>
		<br><br>
	</html:form>
</html:html>
</body>
</html>

fail.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-html" prefix="html"%>
<!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 bgcolor="pink">
Error;
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-html" prefix="html"%>
<!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 bgcolor="pink">

sucessfully added <%=session.getAttribute("s") %>
</body>
</html>

Step – 7 How to Add server in  DynaActionForm  Struts applicationWindow –>show view—->servers—>add new–>          

Select ther version which you installed in your System                                                                                                                                                       

How to run the Project:

Right click on project name———->click Run As —> Run On Server

Step – 8 OutPut  for  Running  DynaActionForm  Struts application

DOWNLOAD + SIMPLEPROGRAM                                                                                                                                                                                                 BACK

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply