How can I check for gender value by NameID? - java

I have this code that checks duplicate name id
NameRefBean myNameBean = new NameRefBean();
myNameBean.load(NameID);
if (!myNameBean.getErrFlag()) {
errExistFlag = true;
errCode = DUPLICATE_NAME_ID;
return(false);
I have switch case where it saves data from the jsp page form
case ACTION_SAVE:
if (hdTxnType.equalsIgnoreCase("Add")) {
if (validateInputData()) {
nameBean.setNameID(tbNameID);
nameBean.setName(tbName);
nameBean.setGender(tbGender);
hdTxnType = new String("Update");
}
} else {
nameBean.setNameID(hdNameID);
nameBean.setName(tbName);
nameBean.setGender(tbGender);
}
break;
My question is how can I check for gender value when particular nameid is selected? If user changes gender value from M to F, I need to give a warning on the jsp page saying "gender already exist do you want to modify it?"
Here is my jsp page
<% if (nameBean.getErrFlag()) {%>
<CENTER><b><font color=red><%= nameBean.getErrMsg() %></b></font></CENTER>
<% } %>
<TABLE WIDTH="800" BORDER="0">
<TR>
<TD><B>Name ID: </B></TD>
<% if (nameBean.getTxnType().equalsIgnoreCase("Add"))
{ %>
<TD><INPUT TYPE=TEXT NAME="tbNameID" VALUE="<%= nameBean.getnameID()%>" ></TD>
<% } else { %>
<TD><%= nameBean.getNameID()%> </TD>
<% } %>
<TD><B> Name: </B></TD>
<TD><INPUT TYPE=TEXT NAME="tbName" VALUE="<%= nameBean.getName()%>" ></TD>
<TD><B>Gender: </B></TD>
<TD><INPUT TYPE=TEXT NAME="tbGender" VALUE="<%= nameBean.getGender()%>" ></TD>
</TR>
<BR>
<TR>
<% if (userProfileBean.hasRole("FULL") ) { %>
<TD>
<INPUT TYPE=SUBMIT NAME="action" VALUE="Save Changes">
<% if (nameBean.getTxnType().equalsIgnoreCase("Update"))
{ %>
<INPUT TYPE=SUBMIT NAME="action" VALUE="Delete">
<% } %>
<INPUT TYPE=SUBMIT NAME="action" VALUE="Cancel">
</TD>
<% } %>

You could add a piece of JavaScript that print the warning, if a user changed the gender. The only thing you have to do then, is to enable this java script if the nameid is already set (then the name already exists, and every change should print the warning.) If the nameid is null, then it is a new name and you must not enable the java script warning functionality.

Related

Java Jstl foreach

I'm having a few problems with some JSTL code. The thing is that i'm trying to compare the id of the user that logged in with the id from the reservation class which has a foreign key of the user id.And after doing that i'm trying to populate the page with update links based on the reservation id.
Here's the code of what i tried but i can't find a way to make it work.
<table>
<tr>
<th>Reservation Id</th>
<th>Data CheckIn</th>
<th>Data CheckOut</th>
<th>Numar Persoane</th>
<th>Numar Camere</th>
<th>Action</th>
<%
List<ReservationBean> theReserv = (List<ReservationBean>) request.getAttribute("RESERVATION_LIST");
%>
<% int userId = (Integer) request.getSession().getAttribute("userId"); %>
<c:forEach var="tempReservation" items="${RESERVATION_LIST}">
<!-- set up a link for each res -->
<c:url var="tempLink" value="UserControllerServlet">
<c:param name="command" value="LOAD"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
<c:url var="deleteLink" value="UserControllerServlet">
<c:param name="command" value="DELETE"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
</c:forEach>
<% for(ReservationBean res : theReserv){
/* int userId = (Integer) request.getSession().getAttribute("userId"); */
if(userId == res.getUserId()){ %>
<tr>
<td> <%= res.getReservationId() %> </td>
<td> <%= res.getDataCheckin() %> </td>
<td> <%= res.getDataCheckout() %> </td>
<td> <%= res.getNrPersoane()%> </td>
<td> <%= res.getNrCamere() %> </td>
<td>Update
|
<a href="${deleteLink}"
onclick="if(!(confirm('Are you sure you want to delete this reservation?'))) return false">Delete</a>
</td>
</tr>
<%}%>
<%}%>
</tr>
</table>
In the second for i'm comparing the id of the user that logged in with the id of the user from the reservation class but of course the updated links will contain the last value of the first for and i don't know how to do what i did in pure java with JSTL to have only one for and get the correct values of the update link or delete ones.Do you guys have any clue on how to do that?
I've tried something like this
<table>
<tr>
<th>Reservation Id</th>
<th>Data CheckIn</th>
<th>Data CheckOut</th>
<th>Numar Persoane</th>
<th>Numar Camere</th>
<th>Action</th>
<%
List<ReservationBean> theReserv = (List<ReservationBean>) request.getAttribute("RESERVATION_LIST");
%>
<% int userId = (Integer) request.getSession().getAttribute("userId"); %>
<c:forEach var="tempReservation" items="${RESERVATION_LIST}">
<!-- set up a link for each res -->
<c:if test="${tempReservation.userId}" == userId >
<c:url var="tempLink" value="UserControllerServlet">
<c:param name="command" value="LOAD"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
<c:url var="deleteLink" value="UserControllerServlet">
<c:param name="command" value="DELETE"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
<tr>
<td>${tempReservation.reservationId}</td>
<td>${tempReservation.dataCheckin}</td>
<td>${tempReservation.dataCheckout}</td>
<td>${tempReservation.nrPersoane}</td>
<td>${tempReservation.nrCamere}</td>
<td>Update
|
<a href="${deleteLink}"
onclick="if(!(confirm('Are you sure you want to delete this student?'))) return false">Delete</a>
</td>
</tr>
</c:if>
</c:forEach>
But that if condition doesn't work or i don't know how to compare jstl code with java code
Edit2: I made it work! You had to compare it something like this
<c:if test="${tempReservation.userId == userId }">
Thanks for help!

Display results on the same JSP issue

I have a created a small Java web application. I have made use of DAO and a JSP file. My issue is that when I enter an isbn number in the input field, if the book is found in the database, it displays me "Book Found" and the corresponding details. But if I enter an isbn number in the input field, and if the book is not found in the db, it still displays me "Book Found" but with isbn number 0 and book title "null" which I don't want. In that case, it should display me only "Book not found".
Note that the problem lies mainly in the JSP page.
Here is my full code: http://pastebin.com/cTZy4w6V (using pastebin since the code is too long)
Here is the JSP code:
<jsp:useBean id = "bm" class="book.ManagerBook" scope = "session"/>
<h1> Welcome to ABC Library</h1>
<form>
<table>
<tr>
<td> Enter Details </td>
<td><input type="text" name="isbn"></td>
<td><input type="submit" name="find" value="find"></td>
</tr>
</table>
<input type="hidden" name="submitted" value="true">
</form>
<%
Boolean submitted = Boolean.parseBoolean(request.getParameter("submitted"));
if(submitted){
int isbn = Integer.parseInt(request.getParameter("isbn"));
Book b2 = bm.findBook(isbn);
%>
<table>
<tr>
<td colspan=2>
<h2>Book Found</h2>
</td>
</tr>
<tr>
<td><h3>ISBN</h3></td>
<td><h3>Title</h3></td>
</tr>
<tr>
<td><%= b2.getIsbn()%></td>
<td><%= b2.getTitle() %></td>
</tr>
</table>
<%}else if(!submitted){ %>
<h3> Book Not Found</h3>
<% } %>
If possible, don't use scriptlets in jsp. It's a bad practice.
Try something like this:
if(submitted){
int isbn = Integer.parseInt(request.getParameter("isbn"));
Book b2 = bm.findBook(isbn);
if (b2.getTitle() != null) {
%>
<table>
<tr>
<td colspan=2>
<h2>Book Found</h2>
</td>
</tr>
<tr>
<td><h3>ISBN</h3></td>
<td><h3>Title</h3></td>
</tr>
<tr>
<td><%= b2.getIsbn()%></td>
<td><%= b2.getTitle() %></td>
</tr>
</table>
<% } else { %>
<h3> Book Not Found</h3>
<% } %> }

Link to the correct item page by selecting the list of items in for loop

This is my jsp page that retrieve the list of items from database using for loop
<%
itemManager mgr = new itemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td> <img border="0" src="<%=items[i].getItemImage() %>" width="100" height="100">
</td>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemID()%>">
<br/>
<%=items[i].getItemDesc()%>
<br/>
Start Bid : <%=items[i].getStartBid()%>
<br/>
Buy it now : <%=items[i].getEndBid()%>
<br/>
Bidding close on : <%=items[i].getDuration()%>
<br/>
<input type="submit" value="View">
<%
}
%></table>
This is the jsp page that link to the item you selected previously
<table border="1" align="center">
<%
itemManager mgr = new itemManager();
Item items = mgr.getItem((Integer)session.getAttribute("ITEM_DATA"));
%>
<tr>
<td> <b> <%=items.getItemName() %></b> </td>
</tr>
</table>
This is the servlet to store the session of the selected item id and forward to the correct item jsp page.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
int id = Integer.parseInt(request.getParameter("itemID"));
session.setAttribute("ITEM_DATA",id);
RequestDispatcher rd = request.getRequestDispatcher("viewItem.jsp");
rd.forward(request, response);
}
However, after I clicked the view button. It keeps linking to the itemID = 1.
The URL dispalys "/ItemServlet?itemID=1&itemID=2" .
In fact, if I click on itemID=2 the URL should display like this:
"/ItemServlet?itemID=2"
As a result, how can I achieve this? Thanks in advance!
The problem in your code is you are using a single form and dynamically creating Input box inside the form. So all the input box will be having same name. That's why when you submit the form all the input box values are sent as request parameters. I just reduced some part in your code for better under standing. Take this as your code
<form action="item" method="get">
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
<%
}
%></table>
</form>
When you run this code and if you check the rendered HTML code it will be look
<form action="item" method="get">
<table>
<tr><td>
aaa
<input type="text" name="itemID" value="1">
<input type="submit" value="View"> </td></tr>
<tr>
<td>
bbb
<input type="text" name="itemID" value="2">
<input type="submit" value="View"> </td></tr>
<tr><td>
ccc
<input type="text" name="itemID" value="3">
<input type="submit" value="View"> </td></tr>
</table>
</form>
Here All the Input box having same name as "itemID". As a solution you can create the form inside the for loop, so that while submitting only one Input box value will be sent as request.
Create form inside your for loop like below code.
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<form action="item" method="get">
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
</form>
<%
}
%></table>
Hope This will help you.
change the name of text field dynamically.
And use getQueryString() in servlet to find the itemId name and value. by using EL.I hope this will help you

How to validate <aui:select> in a Liferay Form?

In View.jsp I created a form by using . there are some inputs and 3 select-box on it, actually I want to validate all of them. for inputs I used but I don't know how can I validate my
Actually I want to validate these three :
<aui:select id="birthday_day" name="birthday_day">
<aui:select id="birthday_month" name="birthday_month">
<aui:select id="birthday_year" name="birthday_year">
This is my view.jsp
<portlet:actionURL var="myUrl">
</portlet:actionURL>
<aui:form enctype="multipart/form-data" action="<%= myUrl %>" method="POST" name="fm">
<table border="0" bgcolor=#ccFDDEE>
<tr>
<td colspan="2" align="center"><b>Recruiment Form</b></td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td><b>First Name:<span>*</span></b></td>
<td>
<aui:input name="fname" type="text">
<aui:validator name="required"/>
</aui:input>
</td>
</tr>
<tr>
<td><b>Last Name:<span>*</span></b></td>
<td>
<aui:input name="lname" type="text">
<aui:validator name="required"/>
</aui:input>
</td>
</tr>
<tr>
<td><b>Father Name:<span>*</span></b></td>
<td>
<aui:input name="father_name" type="text">
<aui:validator name="required"/>
</aui:input>
</td>
</tr>
<tr>
<td><b>Birth Date:<span>*</span></b></td>
<td>
<aui:select id="birthday_day" name="<portlet:namespace/>birthday_day">
<aui:option value="0">روز</aui:option>
<%
for(int i=1;i<=31;i++) {
%>
<aui:option value="<%=i%>"><%=i%></aui:option>
<%
}
%>
</aui:select>
/
<aui:select id="birthday_month" name="birthday_month">
<aui:option value="0">ماه</aui:option>
<%
String[] monthBirthDay = {"فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند"};
for(int j=0;j<monthBirthDay.length;j++) {
%>
<aui:option value="<%=j+1%>"><%=monthBirthDay[j]%></aui:option>
<%
}
%>
</aui:select>
/
<aui:select id="birthday_year" name="birthday_year">
<aui:option value="0">سال</aui:option>
<%
String[] yearBirthDay = {"1370","1369","1368","1367","1366","1365","1364"};
for(int j=0;j<yearBirthDay.length;j++) {
%>
<aui:option value="<%=yearBirthDay[j]%>"><%=yearBirthDay[j]%></aui:option>
<%
}
%>
</aui:select>
</td>
</tr>
</table>
</aui:form>
How can I solve this issue??????
Please guide me.
<aui:select showEmptyOption="true" name="select hospital name" required="true">
</aui:select>
THis may help you.
just assign an id to the aui:select tag
<aui:select inlineField="true" label="xxx" name="xxx" id="xxxId">
and then you can get the value with this (javascript + AUI)
var selectedVal = A.one('#<portlet:namespace/>xxxId').val();
with the selected value you can perform any kind of validation.

Form Not Submitting after Jquery Validation

$('form#injuryReportSubmit').submit(function(){
if(($('form#injuryReportSubmit textarea.description').val().length <=0) || ($('form#injuryReportSubmit select.part').val()== "choose one...") ||($('form#injuryReportSubmit select.type').val() == "choose one...") || ($('form#injuryReportSubmit select.weather').val()=="choose one..."));
{
$('div#errorMessage').show();
return false;
}
});
The code above is used to validate a form before it submits. The problem is the form will not submit even when all the test are false. Can someone help?
the form is on a jsp and looks like
<form id ="injuryReportSubmit" method ="post" action="injuryReportingPage.html" >
<p class ="first" >Date Of Injury</p>
<p class ="second">Date Reported to Manager</p>
<input type="text" id="dateOfInjury" name="dateOfInjury">
<input type="text" id="dateReported" name ="dateReported">
<p class ="weather">Weather</p>
<p class ="injuryType">Injury Type</p>
<p class ="bodyPart">Body Part</p>
<p class ="time">Time Injury Occurred</p>
<p class ="description">Description</p>
<select class ="weather" name="weather">
<%if(InjuryReportController.getWeatherList() != null){ %>
<% for(Weather weather : InjuryReportController.getWeatherList()){%>
<option><%= weather.getWeatherCondition() %></option>
<%} }%>
<option >choose one...</option>
</select>
<select class ="type" name="injuryType">
<%if(InjuryReportController.getInjuryTypeList() != null){ %>
<% for(InjuryType injuryType : InjuryReportController.getInjuryTypeList()){%>
<option><%= injuryType.getInjuryTypeName() %></option>
<%} }%>
<option>choose one...</option>
</select>
<select class ="part" name="bodyPart">
<%if(InjuryReportController.getBodyPartList() != null){ %>
<% for(BodyPart bodyPart : InjuryReportController.getBodyPartList()){%>
<option><%= bodyPart.getBodyPartName() %></option>
<%} }%>
<option >choose one...</option>
</select>
<input type="text" id="timeP" name ="timeOfInjury" value="01:00 AM">
<textarea class ="description" rows="120" cols="670" name="description"></textarea>
<input id ="report" value="Submit Report" type ="submit">
</form>
Remove the semicolon from the end of line 5 of your code.
Currently what you've got is like this:
if (/*your conditions*/); // <- note the semicolon
{
...
return false;
}
This means that the block with the curly braces is not associated with the if statement and so will execute every time. Obviously then returning false every time cancels every submit.

Categories

Resources