Click to See Complete Forum and Search --> : show the value on Pageload


daina
03-14-2006, 12:07 AM
Hi ,

I have a jsp page In that there is a combobox & some textbox.Combobox contains all the username.which is stored in the xml file.I know the first UserName from the combobox.Suppose the first name is ' John'. Its userId is ' 1'. When I open the page.The comboBox has all the values. the first Username 'John ' will be selected. At that time the userId is showing null But it has to show userid '1'. If I change the userName then Onchange event will fire & then its userId will come in the userid textbox.I wrote "document.form1.submit() " on Onchange event of select tag.I tried to give same event on onLoad of body & form .But nothing happenes.Can anyone tell me what to do.I want the userId of the first element in the UserId textbox when page is loaded.

The JSP code:---

<form name="form1" method="post" onLoad="document.form1.submit()" >

<body bgcolor =cccccc onLoad="document.form1.submit()" >

<table border=0 cellpadding="4" cellspacing="3">

<tr>

<td>

<customtable:customtd name="LeaveId" value="<%= leavedata.getLeaveId() %>" readOnly="true" />

</td>

</tr>

<tr >

<customtable1:customtd1 name="UserName" comboValues="<%= leavedata.getResultComboPopulate()%>" changeEvent="document.form1.submit()"/>

<td>

</td>

<td><b>UserId</b></td>

<td>

<input type="text" name="UserId" value="<%= leavedata.getUserId() %>" readonly />

</td>

</tr>

<tr >

<td>

<customtable:customtd name="LeaveFrom" value="<%= leavedata.getToDayDate()%>" readOnly="true" />

</td>

<td>

<img name="calander" src="<%= request.getContextPath() %>/Framework/images/calendar.gif" width="16" height="15" onClick="javascript:show_cal(LeaveFrom,1)" />

</td>

<td>

<customtable:customtd name="LeaveTo" value="<%= leavedata.getToDayDate()%>" readOnly="true"/>

</td>

<td>

<img name="calander" src="<%= request.getContextPath() %>/Framework/images/calendar.gif" width="16" height="15" onClick="javascript:show_cal(LeaveTo,1)" />

</td>

<td>

<input type=CHECKBOX name="halfDay" value="HalfDay" onclick="txtDate('LeaveTo', 'LeaveFrom','NoOfDays');"><b>Half Day</b>

</td>

</tr>

<tr >

<td>

<customtable:customtd name="NoOfDays" value="1" readOnly="true" />

</td>

<td>

<customtable2:customtd2 name="Reason" value="" />

</td>

<td>

</td>

</tr>

<tr>

<td align=center colspan=5>

<input type = submit value = Submit onclick="txtDate('LeaveTo', 'LeaveFrom','NoOfDays');">

</td>

<td>

</td>

</tr>

</table>

Code for CustomTag for select : ----------

public int doStartTag() throws JspException {


StringBuffer sb = new StringBuffer();

JspWriter out = pageContext.getOut();


sb.append("<td><b><font size=3 face=Times New Roman > " + name + "</font><//b></td><td><select name ="+name);
sb.append(" onChange=" + changeEvent );
sb.append(">");

String valueObject = pageContext.getRequest().getParameter(name);

logger.info("valueObject " + valueObject );

for(int i=0;i<comboValues.length;i++)
{

sb.append("<option value=\"" + comboValues[i] + "\"");
if(valueObject != null){
if(valueObject.equals(comboValues[i]))
{
sb.append(" selected");
}
}
sb.append(">"+comboValues[i] + "</option>");

}
sb.append("</select><br>");
}

Khalid Ali
03-14-2006, 12:16 AM
remove the onload event from both body and form tags....then move the form tag inside the body tag...once you do that....test it and post ur result

daina
03-14-2006, 12:28 AM
Hi Khalid,

Thanks for your reply.I did what u have told.But it is showing the same result.The useId is null.

The JSP page code :---

<center>
<th bgcolor="#CCCCFF" colspan="2">
<font size="6"><b><u>Leave Application Form</u></b></font>
</th>

<br><br><br><br><br>
<body bgcolor =cccccc >
<form name="form1" method="post" >
<table border=0 cellpadding="4" cellspacing="3">
<tr>
<td>
<customtable:customtd name="LeaveId" value="<%= leavedata.getLeaveId() %>" readOnly="true" />
</td>
</tr>
<tr >
<customtable1:customtd1 name="UserName" comboValues="<%= leavedata.getResultComboPopulate()%>" changeEvent="document.form1.submit()"/>
<td>
</td>
<td><b>UserId</b></td>
<td>
<input type="text" name="UserId" value="<%= leavedata.getUserId() %>" readonly />
</td>
</tr>
<tr >
<td>
<customtable:customtd name="LeaveFrom" value="<%= leavedata.getToDayDate()%>" readOnly="true" />
</td>
<td>
<img name="calander" src="<%= request.getContextPath() %>/Framework/images/calendar.gif" width="16" height="15" onClick="javascript:show_cal(LeaveFrom,1)" />
</td>
<td>
<customtable:customtd name="LeaveTo" value="<%= leavedata.getToDayDate()%>" readOnly="true"/>
</td>
<td>
<img name="calander" src="<%= request.getContextPath() %>/Framework/images/calendar.gif" width="16" height="15" onClick="javascript:show_cal(LeaveTo,1)" />
</td>
<td>
<input type=CHECKBOX name="halfDay" value="HalfDay" onclick="txtDate('LeaveTo', 'LeaveFrom','NoOfDays');"><b>Half Day</b>
</td>
</tr>
<tr >
<td>
<customtable:customtd name="NoOfDays" value="1" readOnly="true" />
</td>
<td>
<customtable2:customtd2 name="Reason" value="" />
</td>
<td>
</td>
</tr>
<tr>
<td align=center colspan=5>
<input type = submit value = Submit onclick="txtDate('LeaveTo', 'LeaveFrom','NoOfDays');">
</td>
<td>
</td>
</tr>
</table>

<%
if((request.getParameter("UserName") == "") || (request.getParameter("LeaveFrom") == null) || (request.getParameter("Reason") == null) )
{
flag = 0;
}
else if(request.getParameter("UserName").equals("") || request.getParameter("LeaveFrom").equals("") || request.getParameter("Reason").equals("") )
{
flag=0;
}
else
{
flag = 1;
}
if(flag == 1)
{
%>
<jsp:forward page="LeaveServlet" />

<% } %>


</form>
</body>
</center>


The customTag for comboBox :---

public int doStartTag() throws JspException {

//Boolean errorpage = (Boolean) pageContext.getRequest().getAttribute("ErrorPage");

StringBuffer sb = new StringBuffer();

JspWriter out = pageContext.getOut();


sb.append("<td><b><font size=3 face=Times New Roman > " + name + "</font><//b></td><td><select name ="+name);
sb.append(" onChange=" + changeEvent );
sb.append(">");

String valueObject = pageContext.getRequest().getParameter(name);

logger.info("valueObject " + valueObject );

for(int i=0;i<comboValues.length;i++)
{

sb.append("<option value=\"" + comboValues[i] + "\"");
if(valueObject != null){
if(valueObject.equals(comboValues[i]))
{
sb.append(" selected");
}
}
sb.append(">"+comboValues[i] + "</option>");

}
sb.append("</select><br>");
}

The valueObject is returning null. If I change any name then valueObject will have that value. How to get the userId of the selected value on the pageLoad

Khalid Ali
03-14-2006, 12:30 AM
now run ur jsp code oon server and then when u see the page displayed where it actually shows a null value, take a look at the page source and post that here....

daina
03-14-2006, 01:06 AM
Hi

When I run the program the result I got is
















<link href="/LeaveApplication/Framework/Script/Calender1.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="/LeaveApplication/Framework/Script/Calendar1.js" type="text/javascript">
</script>
<div id="calendar" style=calendar></div>

<script language="javascript">
function txtDate(ldate,ndate,noofdays){
var toDate=new Date(document.getElementById(ldate).value)
var fromDate=new Date(document.getElementById(ndate).value)
var one_day=1000*60*60*24
if(form1.halfDay.checked )
{
var days=(Math.ceil((toDate.getTime()-fromDate.getTime())/(one_day))+1)- 0.5
}
else
{
var days=Math.ceil((toDate.getTime()-fromDate.getTime())/(one_day))+1
}
document.getElementById(noofdays).value=days
}
</script>
<center>
<th bgcolor="#CCCCFF" colspan="2">
<font size="6"><b><u>Leave Application Form</u></b></font>
</th>

<br><br><br><br><br>
<body bgcolor =cccccc >
<form name="form1" method="post" >
<table border=0 cellpadding="4" cellspacing="3">
<tr>
<td>
<b><font size=3 face=Times New Roman > LeaveId</font><//b><td><input type = text name =LeaveId value="5"readonly ><br></td>

</td>
</tr>
<tr>
<td><b><font size=3 face=Times New Roman > UserName</font><//b></td><td><select name =UserName onChange=document.form1.submit()><option value="Ann">Ann</option><option value="Mary">Mary</option><option value="Tinu">Tinu</option><option value="Minu">Minu</option></select><br>

<td>
</td>
<td><b>UserId</b></td>
<td>
<input type="text" name="UserId" value="null" readonly />
</td>
</tr>
<tr>
<td>
<b><font size=3 face=Times New Roman > LeaveFrom</font><//b><td><input type = text name =LeaveFrom value="03-14-2006"readonly ><br></td>

</td>
<td>
<img name="calander" src="/LeaveApplication/Framework/images/calendar.gif" width="16" height="15" onClick="javascript:show_cal(LeaveFrom,1)" />
</td>
<td>
<b><font size=3 face=Times New Roman > LeaveTo</font><//b><td><input type = text name =LeaveTo value="03-14-2006"readonly ><br></td>

</td>
<td>
<img name="calander" src="/LeaveApplication/Framework/images/calendar.gif" width="16" height="15" onClick="javascript:show_cal(LeaveTo,1)" />
</td>
<td>
<input type=CHECKBOX name="halfDay" value="HalfDay" onclick="txtDate('LeaveTo', 'LeaveFrom','NoOfDays');"><b>Half Day</b>
</td>
</tr>
<tr>
<td>
<b><font size=3 face=Times New Roman > NoOfDays</font><//b><td><input type = text name =NoOfDays value="1"readonly ><br></td>

</td>
<td>
<td><b><font size=3 face=Times New Roman > Reason</font><//b></td><td><textarea name =Reason value=""></textarea><br></td>

</td>
<td>
</td>
</tr>
<tr>
<td align=center colspan=5>
<input type = submit value = Submit onclick="txtDate('LeaveTo', 'LeaveFrom','NoOfDays');">
</td>
<td>
</td>
</tr>
</table>


</form>
</body>
</center>

In that userId's value is null <input type="text" name="UserId" value="null" readonly />

Khalid Ali
03-14-2006, 07:18 AM
ok now question is that in the page, I don't see code where you are creating a reference to the object leavedata...where is that part of the code

daina
03-14-2006, 11:39 PM
Hi

The Jsp page is :----

<%@ page language="Java" %>
<%@ page import="com.leaveapplication.model.LeaveDataVO" %>
<%@ taglib uri="/customtable1" prefix="customtable" %>
<%@ taglib uri="/customtable2" prefix="customtable1" %>
<%@ taglib uri="/customtable3" prefix="customtable2" %>
<%@ taglib uri="/customtable4" prefix="customtable3" %>

<jsp:useBean id="leavedata" class = "com.leaveapplication.model.LeaveDataVO" scope="session"/>
<jsp:setProperty name="leavedata" property="leaveId" param="LeaveId"/>
<jsp:setProperty name="leavedata" property="userName" param="UserName"/>
<jsp:setProperty name="leavedata" property="userId" param="UserId"/>
<jsp:setProperty name="leavedata" property="leaveFrom" param="LeaveFrom"/>
<jsp:setProperty name="leavedata" property="leaveTo" param="LeaveTo"/>
<jsp:setProperty name="leavedata" property="noOfDays" param="NoOfDays"/>
<jsp:setProperty name="leavedata" property="reason" param="Reason"/>


<link href="<%= request.getContextPath() %>/Framework/Script/Calender1.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="<%= request.getContextPath() %>/Framework/Script/Calendar1.js" type="text/javascript">
</script>
<div id="calendar" style=calendar></div>

<script language="javascript">
function txtDate(ldate,ndate,noofdays){
var toDate=new Date(document.getElementById(ldate).value)
var fromDate=new Date(document.getElementById(ndate).value)
var one_day=1000*60*60*24
if(form1.halfDay.checked )
{
var days=(Math.ceil((toDate.getTime()-fromDate.getTime())/(one_day))+1)- 0.5
}
else
{
var days=Math.ceil((toDate.getTime()-fromDate.getTime())/(one_day))+1
}
document.getElementById(noofdays).value=days
}
</script>

<% int flag = 0; %>
<center>
<th bgcolor="#CCCCFF" colspan="2">
<font size="6"><b><u>Leave Application Form</u></b></font>
</th>
<br><br><br><br><br>

<%System.out.println(" leave data vo user name : " + leavedata.getUserName()); %>
<body bgcolor =cccccc >

<form name="form1" method="post" >
<table border=0 cellpadding="4" cellspacing="3">
<tr>
<td>
<customtable:customtd name="LeaveId" value="<%= leavedata.getLeaveId() %>" readOnly="true" />
</td>
</tr>
<tr>
<customtable1:customtd1 name="UserName" comboValues="<%= leavedata.getUName()%>" changeEvent="document.form1.submit()"/>
<td>
</td>
<td>
<customtable3:customtd3 name="UserId" value="<%= leavedata.getUserId() %>" readOnly="true" />
</td>
</tr>
<tr>
<td>
<customtable:customtd name="LeaveFrom" value="<%= leavedata.getToDayDate()%>" readOnly="true" />
</td>
<td>
<img name="calander" src="<%= request.getContextPath() %>/Framework/images/calendar.gif" width="16" height="15" onClick="javascript:show_cal(LeaveFrom,1)" />
</td>
<td>
<customtable:customtd name="LeaveTo" value="<%= leavedata.getToDayDate()%>" changeEvent="txtDate('LeaveTo', 'LeaveFrom','NoOfDays');" readOnly="true"/>
</td>
<td>
<img name="calander" src="<%= request.getContextPath() %>/Framework/images/calendar.gif" width="16" height="15" onClick="javascript:show_cal(LeaveTo,1)" />
</td>
<td>
<input type=CHECKBOX name="halfDay" value="HalfDay" onclick="txtDate('LeaveTo', 'LeaveFrom','NoOfDays');"><b>Half Day</b>
</td>
</tr>
<tr>
<td>
<customtable:customtd name="NoOfDays" value="1" readOnly="true" />
</td>
<td>
<customtable2:customtd2 name="Reason" value="" />
</td>
<td>
</td>
</tr>
<tr>
<td align=center colspan=5>
<input type = submit value = Submit onclick="txtDate('LeaveTo', 'LeaveFrom','NoOfDays');">
</td>
<td>
</td>
</tr>
</table>

<%
if((request.getParameter("Reason") == null) )
{
flag = 0;
}
else if(request.getParameter("Reason").equals("") )
{
flag=0;
}
else
{
flag = 1;
}

if(flag == 1)
{
%>
<jsp:forward page="LeaveServlet" />

<% } %>
</form>
</body>
</center>

Khalid Ali
03-15-2006, 06:11 AM
did u try to use system.out statement to print userID? if not do that and see if you have a value there at all?

daina
03-15-2006, 06:22 AM
Hi Khalid,

Thanks for your reply. I got the answer. In bean I set the usernmae value as first element in the xml.then it is working.Thanks again.

Khalid Ali
03-15-2006, 07:41 AM
u are welcome...yep thats what I was getting at that user id does not have any value...great...:-)