Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Ajax Ajax with ksp
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Krishna Chandra Mohan
Ajax with ksp
1 Answer(s)      6 years and 2 months ago
Posted in : Ajax



Advertisement
View Answers

September 21, 2008 at 3:45 PM


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
rajanikant.misra@gmail.com


Related Tutorials/Questions & Answers:
Ajax with ksp - Ajax
Ajax with ksp  Good Morning friends, This is Chandra Mohan, I have a doubt on Ajax. The doubt is how can i built or write the Ajax application and how to integrate with JSP,struts. Please give me a suggetions
ajax
ajax  please describe the ajax
Advertisements
Ajax
Ajax  How to learn AJAX
Ajax
Ajax  Hi, What is Ajax? What is the use of Ajax? Thanks   Hi, Ajax is set of technologies used to develop dynamic web applications. In Ajax following technologies is used: a) JavaScript b) XML c) HTTP d) CSS Learn
Ajax
Ajax  how to impliment ajax in registration table using jsp-servlet
Ajax
Ajax  send the example to fetch the data from the server by using ajax in java. for ex:-if there are states which is used to display in frontend we use ajax. send it to me
Ajax
Ajax  how to include ajax in jsp page?   Hi, Please read Ajax First Example - Print Date and Time example. Instead of using PHP you can write your code in JSP. Thanks
ajax
ajax  how to connect ajax with mysql without using php,asp or any other scripting language. please answer soon
Ajax - Ajax
Ajax  What is Ajax ? How one can use ajax with Java
ajax
ajax  HI, In my application using ajax if i type a managername in the textbox it should display all the employees under the that manager... me reg this. thanks KK   Combobox box using Ajax in JSP   i
Ajax
in a tabular form. Same for >33kv We can use ajax where instead of a link a radio button can fetch the data and can populate a table.If ajax can be used
ajax
ajax  How can we fetch data in ajax through servlet?   "mainpage.jsp" <html> <head> <title>Ajax Example</title>...;/head> <body> <h1 align="center"><font color="#000080">Ajax
ajax
ajax  How can we fetch data in ajax through servlet?   "mainpage.jsp" <html> <head> <title>Ajax Example</title>...;/head> <body> <h1 align="center"><font color="#000080">Ajax
ajax
ajax  How can we fetch data in ajax through servlet?   "mainpage.jsp" <html> <head> <title>Ajax Example</title>...;/head> <body> <h1 align="center"><font color="#000080">Ajax
ajax
ajax  How can we fetch data in ajax through servlet?   "mainpage.jsp" <html> <head> <title>Ajax Example</title>...;/head> <body> <h1 align="center"><font color="#000080">Ajax
Ajax
options. I want to do it using ajax. My table is CREATE TABLE HT<em>
AJAX
ajax. My table is CREATE TABLE HTNEWMETER ( ID NUMBER (10) NOT NULL, METERNO
ajax
ajax  I am facing following problem, I am using ajax to get the data continousely without refreshing. Here i have devided a web page into 3 parts(top,left and right) using table in jsp. On the left part i am displaying some
AJAX - Ajax
AJAX  what is Ajax?WHAT IS THE USAGE OF IT?WHERE WE HAVE TO USE IT? GIVE ME EXPLANATION WITH EXAMPLE?  Hi friend, Ajax : Asynchronous JavaScript and XML or Ajax for short is new web development technique used
AJAX - Ajax
AJAX  What is AJAX?how we will use it in java?what is the use of AJAX?give me example?   hi friend, Ajax plays a major role... characteristics have made it popular among the users to develop their site using Ajax
ajax - Ajax
ajax  Hi all, I want ajax validation in struts1.0 , I tried from many days but, i cound't find solution.. I am attaching my code here. struts... ("Your browser does not support Ajax HTTP"); return; } var s
ajax - Ajax
ajax  Dear Roseindia, I want small application using ajax validation in struts1 login. I tried from many days but, i coudn't get solution..Do some favour me..it's urgent. Thanqs for advance.   Hi
Ajax - Ajax
that : form1.html where all Ajax code is written : function showHint...){ alert ("Your browser does not support AJAX!"); return
AJAX - Ajax
("Your browser does not support AJAX!"); return; } var url="http://localhost
AJAX with
AJAX with       Ajax resources, in a digg style, allows the user to register and addd his/her own links Read full Description
What is ajax? - Ajax
What is ajax?  What is Ajax Programming? Can any one share the basic examples of it? thanks
Ajax Learner - Ajax
Ajax Learner  Hi, This is Ziaul,I am working as Java/j2ee Developer from last one year. i have to use Ajax for my current project.pls guide me how should i proceed to learn Ajax. Thanks and Regards Ziaul Hauqe Siddique 
Ajax validation - Ajax
Ajax validation  how to validate a form using Ajax and php  ... you. Please visit for more informaton: http://www.roseindia.net/ajax/ajaxlogin/ajax-registration-program.shtml Thanks. Amardeep
Ajax Learner - Ajax
Ajax Learner  Hi, This is Ziaul,I am working as Java/j2ee Developer from last one year. i have to use Ajax for my current project.pls guide me how should i proceed to learn Ajax. Thanks and Regards Ziaul Hauqe Siddique 
ajax basics - Ajax
ajax basics  i need ajax basics where can i find these........................ pls help me thanx in advance  Hi friend, Ajax : Asynchronous JavaScript and XML or Ajax for short is new web development technique
ajax with php - Ajax
ajax with php  how to do autocomplete search using ajax wit php in object oriented format?.. data should me retrive from database.. plz the way?.. give some sample codings
Advertisements
 

 

 

DMCA.com