|
-
Implement the PersonalDataAction class
-
Add necessary imports
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.DynaActionForm;
-
Our class needs to extend action
public class PersonalDataAction extends Action
-
Implement the Struts Action execute method. This method is what is called when your action is requested from the browser.
Signature of the execute method:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
NOTE: For more Struts class details, see the struts API which is included in the Zip file downloaded.
-
Get form bean data from our PersonalData class
-
Cast the form to PersonalData
-
In a full blown application, this is where the data would be stored to the database or otherwised processed.
PersonalData pd = (PersonalData) form;
-
The Struts ActionForward tells the Struts controller of where to proceed to.
if (form != null) {
return (mapping.findForward(Constants.SUCCESS_KEY));
else {
return (mapping.findForward(Constants.FAILURE_KEY));
}
-
Implement our form page to populate the PersonalData bean
- Right click pages under WebRoot, Select New => File
- Name the file PersonalData.jsp, and Click Finish
- Create two more files called Results.jsp and Error.jsp
-
Implement PersonalData.jsp
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<html:html>
<head>
<title>Form for entering Personal Data</title>
<html:base />
</head>
<body>
<html:form method="post" action="/Confirmation">
<table border="0">
<tr>
<td width="50">
<bean:message key="personalData.add.firstname"/>
</td>
<td>
<html:text property = "firstname" size = "30"/>
</td>
</tr>
<tr>
<td width="35">
<bean:message key="personalData.add.lastname"/>
</td>
<td>
<html:text property="lastname" size="30"/>
</td>
</tr>
<tr>
<td width="35">
<bean:message key="personalData.add.address"/>
</td>
<td>
<html:text property="address" size="80"/>
</td>
</tr>
<tr>
<td width="35">
<bean:message key="personalData.add.city"/>
</td>
<td>
<html:text property="city" size="30"/>
</td>
</tr>
<tr>
<td width="35">
<bean:message key="personalData.add.state"/>
</td>
<td>
<html:text property="state" size="30"/>
</td>
</tr>
</table>
<br />
<html:submit>
<bean:message key="personalData.add.submit"/>
</html:submit>
</html:form>
</body>
</html:html>
-
Now we need to create the ApplicationResources.properties file
-
From the main project expand the WebRoot tree
-
Select and expand the WEB-INF folder
-
Select and expand the src folder
-
Rename the MessageResources.properties file to ApplicationResources.properties
-
At the end of the file add in the following logic and save the file
# -- Personal Data
personalData.add.firstname = First Name
personalData.add.lastname = Last Name
personalData.add.address = Address
personalData.add.city = City
personalData.add.state = State
personalData.add.submit = Submit
-
Now we need to manually put this file into: C:\Tomcat5.0\webapps\BasicStrutsExample\WEB-INF\classes
-
Navigate to: C:\eclipse\workspace\BasicStrutsExample\WebRoot\WEB-INF\src
-
Copy the file
-
Navigate to: C:\Tomcat5.0\webapps\BasicStrutsExample\WEB-INF\classes
-
Paste the file here
-
Now we need to modify the struts-config file
-
From the main project expand the WebRoot tree
-
Select and expand the WEB-INF folder
-
Select and double click the struts-config.xml file
-
Under the Form Bean Definitions add a new Form Bean
- Name it PersonalDataBean
- Have it’s type equal your package and the bean class
-
In our case it would look something like:
<form-bean name="personalDataBean" type="pdata.PersonalData">
- Now add in all of the other properties that we had
-
Overall the Form Bean should look like the following:
<form-bean name="personalDataBean" type="pdata.PersonalData">
<form-property name="firstname" type="java.lang.String" />
<form-property name="lastname" type="java.lang.String" />
<form-property name="address" type="java.lang.String" />
<form-property name="city" type="java.lang.String" />
<form-property name="state" type="java.lang.String" />
</form-bean>
-
Now we need to modify the Action Mappings Definitions
-
Have the Action Path directed to the pages/PersonalData.jsp if any url address ends with /PersonalData. It should look like the following:
<action path="/PersonalData" forward="/pages/PersonalData.jsp" />
-
Now, add an action. Have it directed to the Results page or the Error page if there is a problem. It might look something like this:
<action
path="/Results"
type="pdata.PersonalDataAction"
scope="request"
name="personalDataBean"
input="/pages/PersonalData.jsp">
<forward name="success" path="/pages/Results.jsp" />
<forward name="failure" path="/pages/Error.jsp" />
</action>
-
Under Message Resources Definitions add the following logic:
<message-resources parameter="ApplicationResources" null = "false" />
-
Save and close the file
Page 1 Page 2 Page 4
|
|