Completed Struts Program (With View, Action, ActionForm , web.xml and struts-config.xml)

Completed Struts Program (With View, Action, ActionForm , web.xml and struts-config.xml)

Previous Home Next

 

Here we will cover complete example of struts based web application. We provide all required configuration file, action class, action from and input jsp view and forward files. You can use this application as foundation for you Struts based web application.

 Steps to use this application Struts Application
  • Select the web project in your IDEs.
  • Add the jar file of struts1.3 in your selected project.
  • Copy the following codes and paste according to following directory structure.
  • Start Tomcat server
  • Deploy your project.
  • Open web browser and Run this Example
Directory Structure of Login Example in Struts 1.3 Using MyEclipse IDE



index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<html:form action="/login" >
<table  border="1" bordercolor="red">
<tr> <td align="center" height="50px" style="background: gray;">
Login Example in Struts 1.3</td> </tr>
<tr><td align="center" width="250px" height="150px" style="background: gray;">
<table>
<tr> <td>User Name</td> 
<td><html:text name="loginForm" property="name"/></td> </tr> 
<tr> <td>Password</td> 
<td> <html:password name="loginForm" property="password"/></td> </tr>
 <tr> <td colspan="2" align="right">
<html:submit value="login"/></td> </tr>
</table></td></tr>
</table>
</html:form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</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>
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.r4r.struts.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="login" path="/login.do"/>
</global-forwards>
<action-mappings>
<action path="/login" type="com.r4r.struts.LoginAction" name="loginForm">
<forward name="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.r4r.struts.ApplicationResources"/>
</struts-config> 

LoginForm.java

package com.r4r.struts;
import org.apache.struts.action.ActionForm;
@SuppressWarnings("serial")
public class LoginForm extends ActionForm {
	private String name;
	private String password;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

LoginAction.java

package com.r4r.struts;
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 LoginAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
LoginForm loginForm=(LoginForm)form;
if(loginForm.getName().equals("Mukund")&&loginForm.getPassword().equals("Singh")){
return mapping.findForward("success");
}else{
return mapping.findForward("error");
	}
}
}

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<h4>You are successfully Login</h4>
<h5>Welcome, <bean:write name="loginForm" property="name"/></h5>

error.jsp

<h4 style="color: red;">Invalid UserName and Password</h4>
<jsp:include page="index.jsp"></jsp:include>

Output



Previous Home Next