Retrieve value from checkbox - java

I have jsp code like this
<c:forEach items="${requestScope.XX}" var="x">
<tr>
<td><input type="checkbox" value="${x.xID}" name="x"></td>
<td> ${x.name}</td>
</tr>
</c:forEach>
I am not able to retrieve value of checkbox in servlets.
My servlet code is here :
String xId=request.getParameter("x");
May i know where i am going wrong?
Requirement is that only one checkbox is checked. So no need of array in servlets

does it shows more than one checkbox with name x ? as it is in c:forEach if so then
String xId=request.getParameter("x");
will take value of first checkbox every time.

Use HttpServletRequest#getParameterValues() instead.
String[] checked = request.getParameterValues("x");
// ...

Related

Java- determine which radio button selected

I am dynamically filling a table which contains a radio button in each row. How can I go about determining in which row the radio button has been selected using Java?
<form action='ReceptionManagerController' method='POST'>
<table >
<tr style="text-align:center">
<th>Check-In</th>
<th>Check-Out</th>
<th>Type</th>
<th>Assign Room?</th>
</tr>
<c:forEach var="item" items="${unassignedBookings}">
<tr>
<td>${item.checkIn}</td>
<td>${item.checkOut}</td>
<td>${item.size}</td>
<td><input type="radio" name="checked"></td>
<td><input type="hidden" name="id${count}"></td>
<td><input type="hidden" name="checkIn${count}" value="${item.checkIn}"></td>
<td><input type="hidden" name="checkOut${count}" value="${item.checkOut}"></td>
<td><input type="hidden" name="type${count}" value="${item.size}"></td>
<c:set var="count" value="${count + 1}" scope="page"/>
</tr>
</c:forEach>
</table>
<input type="submit" name="action" value="Assign Booking"/>
</form>
If I give each radio button the same name, then only one can be selected at a time. However, this stops me from determining uniquely which button has been pressed.
Any suggestions?
Thank you for your help.
I had a similar problem last year while working on a struts 1.3 based web application. You can refer to the this link. I hope it will help you. If still its not clear to you, let me put some try on it.
You can take an ArrayList of your bean type(Using Generics) having the setter and getter for it and keep the appropriate values for all the bean objects and add all those objects one by one in your ArrayList. After that iterate the ArrayList in your JSP to display the properties along with radio button. Rest of all is explained in given link.
Note: Your bean class must have a property with proper getter/setter for the radio button.
By the form that I can see, Check-Box seems to be appropriate choice rather than Radio Button. As far as capturing the selection is concerned, you can follow same strategy that you are doing for rest of the form elements, suffix count value to radio-Button or check-box to give them uniqueness.

to retrieve data from database and show it with tha radio buttons with radio botton containing the data value instruts 1.2

In struts 1.2, how can i highlight radio button according to my database value?I am using Form bean to set other text field values.
<tr>
<td align = "right" nowrap>SEX:</td>
<td align= "right" nowrap> <html:radio property="sex" value="male"/>MALE
<html:radio property="sex" value="female"/>FEMALE</td>
</tr>
I am using form bean an following java code:stuform.setSex((String)tempmap.get("SEX"));
here stuform is object of StudentForm class and tempmap contain data from select query.
You can use an if statement, I am using jstl if statement but you can use tag too.
<c:if test="${myForm.sex=='female'}">
<html:radio property="sex" value="female"/><span style="color: red;">FEMALE</span></td>
</c:if>

Retrieving value of visible element using java

I have two elements with name price1 [BTW I know that having duplicate IDs is against standards, is this the same with NAME?]
<TR CLASS="Blocks" id="p_priceKILO" style="display: none ;">
<TD>Price:</TD>
<TD><INPUT TYPE="text" name="price1">$/kilo</TD>
</TR>
<TR CLASS="Blocks" id="p_pricePOUND" style="display: none ;">
<TD>Price:</TD>
<TD><INPUT TYPE="text" name="price1">$/pound</TD>
</TR>
Only one of these rows will be visible at one time (using javascript)
I use the following java code to retrieve price1
public PricePosition(HttpServletRequest request) {
this.price1=StringFunctions.StringToDouble(request
.getParameter("PRICE1"));
...
Is there any neat way to retrieve only the visible element?
I have a workaround - calling them price1a and price1b and retrieving the correct one based on my knowledge of which one is visible, but I wondered if there was another way.
You'll have to use JS again: when displaying a row, rename the inner corresponding input to displayedPrice for example, and get this parameter server-side.
When hiding a row, don't forget to rename it back.

How to iterate over <td> tags with condition using jsoup

I am able get all text with in tags but I want to access only specific td tags.
Eg.I want to get data of second cell text whose first cell html contains attribute
a name="manufacturer"
or Content.I am using Jsoup.
<tabel>
<tr>
<td><a name="Manufacturer"></a>manufacturer</td>
<td>happiness</td>
</tr>
<td>manuf</td>
<td>hap</td>
</tr>
<tr>
<td>tents</td>
<td>acd</td>
</tr>
<tr>
<td><a name="Content"></a>Contents</td>
<td>abcd</td>
</tr>
</tabel>
I am using the code ..
doc.select("a[name=Manufacturer]");
..but its giving me the reference of cell one ,I need to go to cell two get cell two text
You need to use selector like [attr=value]: elements with attribute value, e.g. [width=500].
Take a look at official documentation Selector Syntax

Updating the value of a iterator tag in struts2

I have a jsp in which I have a drop down as
<s:select name="newQuestion.CertificationId" list="certificationList"
listKey="certificationId" listValue="certificationName"
headerKey="" headerValue="Select Certification"
label="Certification Name"
onchange="getQuestionsList(this.value)" />
When the dropdown value changes I can getQuestionsList. In the javascript function I submit to an action class where I modify the value of a questionList which is displayed in my JSP via an iterator.
The values of the questionList contain all questions and when I select a value from the above drop down I need to populate only those questions which belong to the id selected in the drop down. (I query the DB to load the questions in action class.)
Initially when the page is loaded I have all questions in questionList but after selecting a value from drop down I have the updated questions in the action class.
For displaying the values of question list I use a iterator tag
<div id="questionDetails" class="registrationDetails" style="display: none;">
<span><b>Question List</b></span>
<br>
<table class="registrationDetailsTable">
<tr class="tabledataheader">
<td>Question Id</td>
<td>Question Description</td>
</tr>
<s:iterator value="questionList">
<tr class="tabledatarow">
<td><s:property value="questionId" /></td>
<td><s:property value="questionDesc" /></td>
</tr>
</s:iterator>
</table>
</div>
The div is initially hidden and on select of a value in drop down I need to display the values of questionList which is taking old values as the page is not reloaded.
When I again come back to this jsp I am not seeing the new value as it is not getting updated.
Any heads up please
Why don't you try Struts2 Jquery Plugin. Here is the showcase with code.
What you need is
<sj:select ...> tag.
Let me know if you still need an example.
You would need to make few changes to the code. Let me list them down:
Move the following part in your initial JSP to another jsp, say questionList.jsp. The below part should be the only thing which should be inside the newly created jsp.
<span><b>Question List</b></span>
<br>
<table class="registrationDetailsTable">
<tr class="tabledataheader">
<td>Question Id</td>
<td>Question Description</td>
</tr>
<s:iterator value="questionList">
<tr class="tabledatarow">
<td><s:property value="questionId" /></td>
<td><s:property value="questionDesc" /></td>
</tr>
</s:iterator>
</table>
In your main page, you would need to replace the above code part with
<jsp:include page="questionList.jsp" flush="true"/>
Now deploy and see if everything is in place as before.
In your getQuestionsList() javascript function, make an ajax call to another action mapping say showQuestionList.action?certification=12. Here, 12 will be the id of the certification, which you will handle in the action. If you use certification as an action property, add a variable with the same name with getter and setter methods.
In your action method say showQuestionList(), retrieve the questionList and assign to the action variable.
On return of SUCCESS of the above method, the result should send only questionList.jsp. This will have the part to list just the necessary fields.
In the getQuestionsList() javascript function, after the ajax call being success, get the data and put it into the div with id questionDetails.
Show the div.

Categories