Set up a J2EE Web Application Project That Uses Struts Frameworks

Set up a J2EE Web Application Project That Uses Struts Frameworks

Previous Home Next

 

J2EE Web Application Project is dynamic web project. It is combination of Java, Servelet, JSP and Static Web Recourses(like html, css, images, etc ). In J2EE only one xml is required. That is deployment descriptor file (e.g. web.xml).This deployment descriptor file is used to define all configuration of the J2EE web based application like welcome file, securities, tag lib ,Servelet configurations and Servelet mapping etc. 


 In struts only on more extra file is required that is struts-config.xml. Which is used to configure the Actions, ActionForms, add plug-in etc. Here in Struts application we have configured and mapping ActionServet into web.xml .The ActionServet works as controller in Struts based application. We also need some Jars which we can download from http://struts.apache.org/download.cgi#struts1310 Extract it and copy it and past it into lib folder of your J2EE web application project. So we can say we have following steps to set up a J2EE web application project into struts 
  • Add struts frameworks jars into lib folder. 
  • Create a struts-config.xml file. 
  • Configure ActionServelet and add ActionServelet mapping into web.xml and add struts-config.xml into web.xml 
  • Create Subclasses ActionForm to gather the data of input JSPs and make Actions class which is called by ActionServelet and call appropriate models and finally returns a key. This key is used by ActionServelet to decide next view to user.
Directory Structure of UserInformation Example in Struts 1.3 Using MyEclipse IDE



index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html:form action="userInfoAction">
User Name:-<html:text property="userName"/><br/>
User Date Of Birth:-<html:text property="userDOB"/><br/>
User Course:-<html:text property="userCourse"/><br/>
User City:-<html:text property="userCity"/><br/>
User State:-<html:text property="userState"/><br/>
User Country:-<html:text property="userCountry"/><br/>
User Phone:-<html:text property="userPhone"/><br/>
<html:submit></html:submit>
</html:form>

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="userInfoForm" type="com.r4r.struts.UserInfoForm"/>
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="userInfoAction" path="/userInfoAction.do"/>
</global-forwards>
<action-mappings>
<action path="/userInfoAction" type="com.r4r.struts.UserInfoAction" name="userInfoForm">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.r4r.struts.ApplicationResources" />
</struts-config> 

UserInfoForm.java

package com.r4r.struts;
import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class UserInfoForm extends ActionForm {
	private String userName;
	private String userDOB;
	private String userCourse;
	private String userCity;
	private String userState;
	private String userCountry;
	private String userPhone;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserDOB() {
		return userDOB;
	}
	public void setUserDOB(String userDOB) {
		this.userDOB = userDOB;
	}
	public String getUserCourse() {
		return userCourse;
	}
	public void setUserCourse(String userCourse) {
		this.userCourse = userCourse;
	}
	public String getUserCity() {
		return userCity;
	}
	public void setUserCity(String userCity) {
		this.userCity = userCity;
	}
	public String getUserState() {
		return userState;
	}
	public void setUserState(String userState) {
		this.userState = userState;
	}
	public String getUserCountry() {
		return userCountry;
	}
	public void setUserCountry(String userCountry) {
		this.userCountry = userCountry;
	}
	public String getUserPhone() {
		return userPhone;
	}
	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}

}

UserInfoAction.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 UserInfoAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
return mapping.findForward("success");
	}

}

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

UserName:-<bean:write name="userInfoForm" property="userName"/><br/>
UserDOB:-<bean:write name="userInfoForm" property="userDOB"/><br/>
UserCourse:-<bean:write name="userInfoForm" property="userCourse"/><br/>
UserCity:-<bean:write name="userInfoForm" property="userCity"/><br/>
UserState:-<bean:write name="userInfoForm" property="userState"/><br/>
UserCountry:-<bean:write name="userInfoForm" property="userCountry"/><br/>
UserPhone:-<bean:write name="userInfoForm" property="userPhone"/>

Output



Previous Home Next