Hello chandra
See the code very carefully , where it necessary change as per your package strcture. This is small module of a reservation project
---------------------
hotelReservation.jsp
---------------------
<%@taglib uri="
http://struts.apache.org/tags-html"; prefix="html" %>
<html:xhtml/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">;
<html xmlns="
http://www.w3.org/1999/xhtml">;
<head>
<title>Hotel Reservation System</title>
<script type="text/javascript" src="js/hotelReservation.js"></script>
<script type="text/javascript" src="js/prototype-1.4.0.js"></script>
</head>
<body>
<h1>Hotel Reservation System</h1>
<h3>* Required fields</h3>
<div id="errors">
<html:errors/>
</div>
<html:form action="saveReservation.do" method="post"
styleId="reservationForm">
<table border="0">
<tbody>
<tr>
<td>
<label>* Arrival Date:</label>
</td>
<td>
<html:text property="arrivalDate"
styleId="arrivalDate" onblur="validateForm();"/>
</td>
</tr>
<tr>
<td>
<label>* Departure Date:</label>
</td>
<td>
<html:text property="departDate"
styleId="departDate" onblur="validateForm();"/>
</td>
</tr>
<tr>
<td>
<label>* Smoking Preference:</label>
</td>
<td>
<html:select property="smokingPref"
styleId="smokingPref">
<html:option value="">Select One</html:option>
<html:option value="smoking">Smoking</html:option>
<html:option value="Non Smoking">
Non Smoking
</html:option>
</html:select>
</td>
</tr>
<tr>
<td>
<label>Special Requests:</label>
</td>
<td>
<html:textarea property="requests"
styleId="requests" rows="6" cols="50" />
</td>
</tr>
<tr>
<td>
<label>* Name:</label>
</td>
<td>
<html:text property="name"
styleId="name" />
</td>
</tr>
<tr>
<td>
<label>* Telephone:</label>
</td>
<td>
<html:text property="telephone"
styleId="telephone" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit"/>
</td>
</tr>
</tbody>
</table>
</html:form>
</body>
</html>
----------------------
| hotelReservation.js |
----------------------
function hasEntry(id) {
return $F(id).length > 0;
}
function isFormReadyForValidation() {
var ready = false;
if(hasEntry("arrivalDate")
&& hasEntry("departDate")
&& $F("smokingPref").length > 0) {
ready = true;
}
return ready;
}
function validateForm() {
var isReady = isFormReadyForValidation();
if(isReady) {
sendFormForValidation();
}
}
function sendFormForValidation() {
var queryString = Form.serialize("reservationForm");
queryString = queryString + "&ts=" + new Date().getTime();
var url = "validateReservation.do";
new Ajax.Request(url, {
asynchronous: true,
method: "get",
parameters: queryString,
onComplete: function(request) {
handleResponse(request.responseText);
}
});
}
function handleResponse(text) {
$("errors").innerHTML = text;
}
----------------------
| strut-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="reservationForm"
type="com.proajax.chapt5.validation.ui.ReservationForm"/>
</form-beans>
<action-mappings>
<action path="/reservation"
type="org.apache.struts.actions.ForwardAction"
name="reservationForm"
scope="request"
parameter="/hotelReservation.jsp" />
<action path="/validateReservation"
type="com.proajax.chapt5.validation.ui.ValidateReservationAction"
name="reservationForm" validate="true"
input="/jsp/validation/reservationErrors.jsp" >
<forward name="valid" path="/jsp/validation/blank.jsp"/>
<forward name="invalid"
path="/jsp/validation/reservationErrors.jsp"/>
</action>
<action path="/saveReservation"
type="com.proajax.chapt5.validation.ui.SaveReservationAction"
name="reservationForm"
validate="true"
input="/hotelReservation.jsp">
<forward name="success"
path="/jsp/validation/reservationSuccessful.jsp"/>
<forward name="fail" path="/hotelReservation.jsp"/>
</action>
</action-mappings>
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="MessageResources" />
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<!-- Path to XML definition file -->
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
<!-- Set Module-awareness to true -->
<set-property property="moduleAware" value="true" />
</plug-in>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
--------------------------
| ReservationForm.java |
--------------------------
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.ValidatorActionForm;
public class ReservationForm extends ValidatorActionForm {
private String arrivalDate;
private String departDate;
private String smokingPref;
private String requests;
private String name;
private String telephone;
private DateFormat parser = new SimpleDateFormat("MM/dd/yyyy");
public String getArrivalDate() {
return arrivalDate;
}
public Date getArrivalDateAsDate() {
try {
return parser.parse(arrivalDate);
}
catch(ParseException e) {
return null;
}
}
public void setArrivalDate(String arrivalDate) {
this.arrivalDate = arrivalDate;
}
public Date getDepartDateAsDate() {
try {
return parser.parse(departDate);
}
catch(ParseException e) {
return null;
}
}
public String getDepartDate() {
return departDate;
}
public void setDepartDate(String departDate) {
this.departDate = departDate;
}
public String getSmokingPref() {
return smokingPref;
}
public void setSmokingPref(String smokingPref) {
this.smokingPref = smokingPref;
}
public boolean isSmokingRequest() {
return smokingPref.equalsIgnoreCase("smoking");
}
public String getRequests() {
return requests;
}
public void setRequests(String requests) {
this.requests = requests;
}
public ActionErrors validate(ActionMapping mapping
, HttpServletRequest request) {
ActionErrors errors;
errors = super.validate(mapping, request);
DateFormat parser = new SimpleDateFormat("MM/dd/yyyy");
try {
Date arrival = parser.parse(arrivalDate);
Date departure = parser.parse(departDate);
if(departure.before(arrival)) {
errors.add(ActionErrors.GLOBAL_MESSAGE
, new ActionMessage("errors.departure.before.arrival"
, true));
}
}
catch (Exception e) {
// Do nothing -- date format validation is handled in
// validation.xml.
}
return errors;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
--------------------------
| reservationErrors.jsp |
---------------------------
<%@taglib uri="
http://struts.apache.org/tags-logic"; prefix="logic" %>
<%@taglib uri="
http://struts.apache.org/tags-html"; prefix="html" %>
<%@taglib uri="
http://struts.apache.org/tags-bean"; prefix="bean" %>
<logic:messagesPresent>
<ul>
<html:messages id="error">
<li style="color:red;">
<bean:write name="error"/>
</li>
</html:messages>
</ul>
</logic:messagesPresent>
--------------------------------
| validation.xml ||
--------------------------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.1.3//EN"
"
http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">;
<form-validation>
<global>
<constant>
<constant-name>phoneFormat</constant-name>
<constant-value>^\(?\d{3}\)?\s|-\d{3}-\d{4}$</constant-value>
</constant>
<constant>
<constant-name>dateFormat</constant-name>
<constant-value>^\d{1,2}/\d{1,2}/\d{4}$</constant-value>
</constant>
</global>
<formset>
<form name="/validateReservation">
<field property="arrivalDate" depends="required, mask">
<msg key="errors.date" name="mask"/>
<arg0 key="label.arrival.date" resource="true"/>
<arg1 key="format.date"/>
<var>
<var-name>mask</var-name>
<var-value>${dateFormat}</var-value>
</var>
</field>
<field property="departDate" depends="required, date">
<msg key="errors.date" name="mask"/>
<arg0 key="label.depart.date" resource="true"/>
<arg1 key="format.date"/>
<var>
<var-name>mask</var-name>
<var-value>${dateFormat}</var-value>
</var>
</field>
</form>
<form name="/saveReservation">
<field property="arrivalDate" depends="required, mask">
<msg key="errors.date" name="mask"/>
<arg0 key="label.arrival.date" resource="true"/>
<arg1 key="format.date"/>
<var>
<var-name>mask</var-name>
<var-value>${dateFormat}</var-value>
</var>
</field>
<field property="departDate" depends="required, mask">
<msg key="errors.date" name="mask"/>
<arg0 key="label.depart.date" resource="true"/>
<arg1 key="format.date"/>
<var>
<var-name>mask</var-name>
<var-value>${dateFormat}</var-value>
</var>
</field>
<field property="smokingPref" depends="required">
<arg0 key="label.smoking.pref" resource="true"/>
</field>
<field property="name" depends="required">
<arg0 key="label.name" resource="true"/>
</field>
<field property="telephone" depends="required, mask">
<msg key="errors.invalid.telephone.format" name="mask"/>
<arg0 key="label.telephone" resource="true"/>
<var>
<var-name>mask</var-name>
<var-value>${phoneFormat}</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
----------------------------
| messagesResources.properties
-------------------------------
label.arrival.date=Arrival Date
label.depart.date=Departure Date
label.smoking.pref=Smoking Preference
label.name=Name
label.telephone=Telephone Number
format.date=MM/DD/YYYY
errors.departure.before.arrival=Arrival date must occur before departure
date.
errors.invalid.date.format=Invalid date format.
errors.invalid.telephone.format=Invalid telephone format.
errors.reservation.not.available=The requested reservation is not
available.
---------------------------------
validateReservationAction.java
-------------------------------------
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;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
public class ValidateReservationAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm
actionForm
, HttpServletRequest request, HttpServletResponse response)
throws Exception {
ReservationForm form = (ReservationForm) actionForm;
ReservationService service = new ReservationService();
boolean isAvailable =
service.isReservationAvailable(form.getArrivalDateAsDate()
, form.getDepartDateAsDate()
, form.isSmokingRequest());
ActionMessages errors = this.getErrors(request);
if(!isAvailable) {
errors.add(ActionMessages.GLOBAL_MESSAGE
, new ActionMessage("errors.reservation.not.available"
, true));
}
saveErrors(request, errors);
ActionForward forward = null;
if(errors.size() > 0) {
forward = mapping.findForward("invalid");
}
else {
forward = mapping.findForward("valid");
}
return forward;
}
}
------------------------------
ReservationService.java
------------------------
import com.proajax.chapt5.exception.ReservationNotAvailableException;
import java.util.Date;
import java.util.Random;
public class ReservationService {
private static Random random = new Random();
public boolean isReservationAvailable(Date arrival, Date departure
, boolean isSmoking) {
//Of course a real implementation would actually check if the desired
//reservation was available. Here, just do it randomly so the
//reservation is unavailable about 1/3 of the time.
return ! ((random.nextInt(100) % 3) == 0);
}
public void saveReservation(Date arrival, Date departure
, boolean isSmoking, String requests
, String name, String telephone)
throws ReservationNotAvailableException {
if(!isReservationAvailable(arrival, departure, isSmoking)) {
throw new ReservationNotAvailableException();
}
// Logic to actually save the reservation goes here.
}
}
--------------------------
reservationSuccessful.jsp
-------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">;
<html xmlns="
http://www.w3.org/1999/xhtml">;
<head>
<title>Hotel Reservation Confirmed</title>
</head>
<body>
<h1>
Congratulations! Your reservation is confirmed.
</h1>
<ul>
<li>Arrival Date: ${reservationForm.arrivalDate}</li>
<li>Departure Date: ${reservationForm.departDate}</li>
<li>Smoking Preference: ${reservationForm.smokingPref}</li>
<li>Special Requests: ${reservationForm.requests}</li>
<li>Name: ${reservationForm.name}</li>
<li>Telephone: ${reservationForm.telephone}</li>
</ul>
</body>
</html>
Thanks
Rajanikant
[email protected]