12:46 pm - Saturday July 22, 2017

struts 1x Dyna Action Form 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 DynaActionForm Struts application in Tomcat5.5 with Eclipse IDE

Step – 1 Creating Dynamic web Project

Step – 2 DynaActionForm  STRUTS folder structure

Step – 3 Creating your DynaActionForm Struts program(Controller)

Step – 4 Creating web.xml for DynaActionForm struts application

Step – 5 Creating Struts-config.xml

Step – 6 DynaActionForm  Struts view page

Step – 7 How to Add server in  DynaActionForm  Struts application

Step – 8 OutPut  for  Running  DynaActionForm  Struts application

 

 SOURCE CODE 

 

Step – 1 Creating Dynamic web Project 

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

2.DynaActionForm  STRUTS folder structure

    Lib Folder

Download The Following TLD files  and paste into WEB-INF

 




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

Step – 3 Creating your DynaActionForm Struts program(Controller):

Controller in java(MyAction.java)

MyAction.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.DynaActionForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class MyAction extends Action
{
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception
{

	DynaActionForm loginForm = (DynaActionForm) form;
        String un = loginForm.get("userName").toString();
        String pw = loginForm.get("password").toString();
					System.out.println("u="+un);
					System.out.println("pw="+pw);
              if(un.equals("") || pw.equals(""))
                       {
return mapping.findForward("error");

				}
				else
				{
					 HttpSession session=request.getSession(true);
                            session.setAttribute("un",un);

					return mapping.findForward("success");
				}
	}
}

Step – 4 Creating web.xml for DynaActionForm struts application

XML file configuration:

Web.xml:


<!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>Struts Blank 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>
		<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-list>
	<taglib>
		<taglib-uri>/tags/struts-html</taglib-uri>
		<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>/tags/struts-bean</taglib-uri>
		<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
	</taglib>

</web-app>

Step – 5 Creating 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 name="LoginForm" type="org.apache.struts.action.DynaActionForm">
			<form-property name="userName" type="java.lang.String" />
			<form-property name="password" type="java.lang.String" />
		</form-bean>
	</form-beans>

	<action-mappings>

		<action path="/Login" type="Com.Candidjava.MyAction" name="LoginForm"
			input="index.jsp">
			<forward name="success" path="/Success.jsp" />
			<forward name="error" path="/Simple.jsp" />
		</action>

	</action-mappings>

</struts-config>

Step – 6 DynaActionForm  Struts view page

Jsp file:

index.jsp:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-html.tld" 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=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="pink">
<br>
<br>

<html:form action="/Login">
            User Name : <html:text name="LoginForm" property="userName" />
	<br>
            Password  : <html:password name="LoginForm"
		property="password" />
	<br>
	<html:submit value="Login" />
</html:form>

</body>
</html>

Success.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-html.tld" 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=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="pink">
<br>
<br>
Welcome :<%=session.getAttribute("un")%>

</body>
</html>







Simple.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-html.tld" 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=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="pink">
<br>
<br>
Please Provide the correct Username And Password

</body>
</html>

Step – 7 How to Add server in  DynaActionForm  Struts application

Window –>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+SOURCE CODE                                                                                                                                                                                                                BACK

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply