JSP with a HTML table and need to update the table - java

I have a JSP with a table and inside the cells, I have buttons. My requirement is to fetch the cell contents from with in the same jsp for a period of some ms. Essentially the cell contents mean the button name and its value. Then I need to pass these values to another function to process. My jsp snippet is:
<div> <table id="rooms" border="50" width="50%" cellspacing="20" cellpadding="10" style="float:center">
<tbody>
<tr>
<td><input type="submit" value="abc" name="room" /></td>
<td><input type="submit" value="def" name="room" /></td>
</tr>
<tr>
<td><input type="submit" value="ghi" name="room" /></td>
<td><input type="submit" value="jkl" name="room" /></td>
</tr></tbody>
<%
response.setIntHeader("Refresh", 5);
String[] room = request.getParameterValues("room");
try{
System.out.println(room[0]);
}
catch(Exception e){
System.out.println("Error");
}
%>
I am getting Error because of null value. I can do this in javascript but I want only JSP code. Other forums I have seen jquery and ajax but still I prefer if there is a JSP only way.
I need help as to where I am doing this wrong. Any pointers?

Related

Use doPost with link

I am working on a Java-Project with html/ftl and so on...
I have a ftl file with a list like this:
<table id="availableHOs">
<tr>
<th>#</th>
<th>Street</th>
<th>Town</th>
<th>Capacity</th>
</tr>
<#list availableOffers as ho>
<tr>
<td>${ho.id}</td>
<td>${ho.addressData.street}</td>
<td>${ho.addressData.town}</td>
<td>${ho.capacity}</td>
</tr>
</#list>
</table>
(That is a template which we got from the university.)
My problem is that the Link in the table (with the title "Make Booking" uses a GET and not a POST. Could somebody help me to change this to a POST?
I only have an example with a submit button and a form:
<form method="POST" action="guestgui?action=projekteSuchenU">
But I dont know how I would use this in the table.
In summary, I would like to have a table / list where I have a link for each line that works with POST
I am abolut new in this topic, so please sorry for my bad explanation!
Thank you!
Add form instead tag 'a':
<table id="availableHOs">
<tr>
<th>#</th>
<th>Street</th>
<th>Town</th>
<th>Capacity</th>
</tr>
<#list availableOffers as ho>
<tr>
<td>
<form method="POST" action="guestgui?action=selectHolidayOffer&hid=${ho.id}">
<input type="submit" value="${ho.id}"/>
</form>
</td>
<td>${ho.addressData.street}</td>
<td>${ho.addressData.town}</td>
<td>${ho.capacity}</td>
</tr>
</#list>
</table>

adding an icon menu from another jsp file as a column to a datatable in liferay

There are 2 tables here:
The table below is using purely container to display populate the tables from database whereas the one above is using datatable.
However i wish to duplicate the last column of displaying a icon menu for edit and delete actions which is contained inside actions.jsp inside the table above.
This is my partial code in view.jsp displaying the datatables.
<%
List<Guestbook> guestbookList = GuestbookLocalServiceUtil.getGuestbooks(scopeGroupId);
request.setAttribute("guestbookList", guestbookList);
%>
<table id="gbdb" class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${guestbookList}" var="guestbook">
<tr>
<td>${guestbook.name}</td>
<td>${guestbook.status}</td>
<td><jsp:include page="${actions.jsp}" flush="true" /></td>
</tr>
</c:forEach>
</tbody>
</table>
However as you can see my function of diplaying actions.jsp is not similar to the method for using container where i can just referenced the path using
<td> <liferay-ui:search-container-column-jsp
align="right"
path="/guestbookadminportlet/guestbook_actions.jsp" /></td>
Please help me with the correct way of displaying by referencing to actions.jsp
You should not include whole jsp as an action column if using jQuery data-table,
Let's assume you need to add Delete book action so your td should be replace by following code.
<td>
<portlet:actionURL name="<%=DELETE_BOOK%>" var="deleteBookUrl">
<portlet:param name="bookId" value="${book.id}"/>
</portlet:actionURL>
<input type="button" onclick="location.href='${deleteBookUrl}'" value="Delete"/>
</td>
Hope this will help you.

Selenium Webdriver - Unticking all the chekboxes in a complicated table

I am having some problems currently trying to untick checkboxes in an iframe. The situation is it is currently possible to set some checkboxes to default ticked and some not. I need 1 specific checkbox ticked, so the sensible thing to do is run a loop that iterates through all the checkboxes and unchecks them all.
Here is where I am running into issues. I will post a sample of the HTML that the checkboxes are contained in. (This isn't mine so I can't edit the HTML unfortunately).
This is how the example looks in a situation where there are 3 different types of checkbox in the iframe.
<fieldset id="testing">
<legend>testing</legend>
<table>
<tbody>
<tr>
<td class="EXAMPLE">
<table id="CHECKBOXTYPE1">
<tbody>
<tr>
<td style="vertical-align:top;white-space:nowrap;" title="">
<input id="CHECKBOXTYPE1-01" type="checkbox" value="on" onclick="DOES STUFF;"/>
</td>
<td style="vertical-align:top;white-space:nowrap;" title="">TITLE1</td>
</tr>
<tr>
<td/>
<td id="CHECKBOXTYPE2-01" style="display:none;white-space:nowrap;vertical-align:top;padding:0px;">
<table>
<tbody>
<tr>
<td style="vertical-align:top;" colspan="3">
<select id="field" style="width:100%;">
<option value="1">STUFF1 </option>
<option value="2">STUFF2 </option>
<option value="3">STUFF3 </option>
<option value="4">STUFF4 </option>
</select>
</td>
<td style="vertical-align:bottom;padding-left:6px;" rowspan="2">
<textarea id="CHECKBOXTYPE2-01-COMMENTS" cols="50" rows="2" style="margin:0px;height:50px;" type="text" onclick="DOES STUFF">Please Insert Notes...</textarea>
</td>
</tr>
<tr>
<td style="vertical-align:bottom;">
<input type="CHECKBOXTYPE2-01-BUTTON" onclick="DOES STUFF" value="<" style="height:100%;width:32px;"/>
</td>
<td style="vertical-align:bottom;">
<input id="CHECKBOXTYPE2-01-INPUT" type="input" readonly="" style="width:112px;"/>
</td>
<td style="vertical-align:bottom;">
<input type="CHECKBOXTYPE2-01-BUTTON" onclick="DOES STUFF" style="width:32px;height:100%;" value=">"/>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="field_label">
<table id="CHECKBOXTYPE3">
<tbody>
<tr>
<td style="vertical-align:top;">
<input id="CHECKBOXTYPE3-01" type="checkbox" title="" onclick="DOES STUFF"/>
</td>
<td style="vertical-align:top;" title="">CHECKBOX NAME</td>
<td style="vertical-align:top;">
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
The code I have attempted to iterate is
try{
for(int i=0; i < 30; i++){
WebElement relCheckBoxes = driver.findElement(By.xpath("html/body/div[3]/fieldset/table/tbody/tr[i]/td/table/tbody/tr/td[1]"));
if(relCheckBoxes.isSelected()){
relCheckBoxes.click();
}
}
}
catch(Exception e){
System.out.printf("didn't work");
}
Obviously this is not the most optimised piece of code, but right now I'm just struggling to find something that works :\ I just want to run through the checkboxes, turn off all of them, then turn on the one that I need after.
Thank you.
If you want to uncheck all the checkboxes use the following code. It is much efficient!
//Get the complex table
WebElement mainTable = driver.findElement(By.xpath("html/body/div[3]/fieldset/table"));
//Find all the input tags inside the mainTable and save it to a list
List<WebElement> checkBoxes = mainTable.findElements(By.tagName("input"));
//iterate through the list of checkboxes and if checked, uncheck them
for (WebElement checkbox : checkBoxes) {
if (checkbox.isSelected()) {
checkbox.click();
}
}
I dont see any frame inside your code. If there is a frame use the below code 1st
//switch to the frame
driver.switchTo().frame("framename/index");
Hope this helps you :)
There are a couple of problems here:
1) Your XPath is incorrect. You have:
"html/body/div[3]/fieldset/table/tbody/tr[i]/td/table/tbody/tr/td[1]"
Instead, it should be:
"html/body/div[3]/fieldset/table/tbody/tr[" + i + "]/td/table/tbody/tr/td[1]"
Otherwise, you're just looking for a table row with a non-numerical index 30 times!
2) XPath indices are 1-based rather than 0-based (crazy, I know). Since your loop starts with i=0, it starts off by trying to find the non-existent zeroth element. findElement throws an exception when it cannot locate an element that matches the search criterion, so the loop ends immediately. Try starting the loop with i=1 instead.

On refreshing jsp page got submited again

I have created simple jsp servlet project on that when i submitting jsp form it insert data to specified table but after that when i refersh same jsp form get submitted and same data inserted to table..
ItemUnit.jsp
<form method="POST" action='ItemUnitHandler' name="frmAddUser">
<input type="hidden" name="action" value="insert" />
<table style="width:95%;margin-top:70px;" align="center">
<tr>
<td style="width:10%"> </td>
<td style="width:30%" align="right">Item Unit :</td>
<td style="width:2%"> </td>
<td style="width:40%" align="left"><input type="text" name="itemUnitName" style="width:200px;" /></td>
<td style="width:18%"> </td>
</tr>
<tr><td colspan="5"> </td></tr>
<tr>
<td colspan="5" align="center">
<input type="submit" class="button-2" value="Insert"></input>
<input type="reset" class="button-2" value="Reset"></input>
</td>
</tr>
</table>
</form>
Servlet post method code..
String action = request.getParameter("action");
System.out.println("action :action action : "+action);
if(action == null)
{
redirect = "ItemUnit.jsp";
}
else if(action.equalsIgnoreCase("insert"))
{
ItemUnit objItemUnit = new ItemUnit();
// System.out.println("request.getParameter : "+request.getParameter("itemUnitName"));
objItemUnit.setItemUnit(request.getParameter("itemUnitName"));
dao.addItemUnit(objItemUnit);
redirect = "ItemUnit.jsp";
}
please help me out from this problem...
Ideally, you should land on a static result page, instead of displaying the input form again. The idea is to force the browser to switch to a new URL so that a refresh will not re-trigger another data update. This is the simplest fix.
But if you prefer to use the same screen, you should use a hidden token. You must record some state since you want to differentiate 2 requests from one another: first and second request (the refresh).
Within your logic, plant a token the first time you display the input form. On any incoming request from the same session, check if you have used the token already and use it to determine if this is a second-time request. The diagram below will illustrate it further.
Setup
Processing

Spring MVC - Submit an Object from JSP

I have a JSP that presents a list of customers (ArrayList searchResults). I want to be able to pick one of those, and submit it to a Spring MVC controller. However, it appears that I cannot pass the selected object, only a property of it, such as customerId. I really need to pass the entire object.
Is there a standard way to do this in Spring 3.x?
<c:forEach items="${searchResults}" var="searchResult">
<tr>
<td><c:out value="${searchResult.customerId}" /></td>
<td><c:out value="${searchResult.firstName}" /></td>
<td><c:out value="${searchResult.lastName}" /></td>
<td>
<form method="POST" ACTION="./customercare">
<input type="SUBMIT" value="Select This Customer"/>
<input type="hidden" name ="searchResult" value="${searchResult}"/>
</form>
</td>
</tr>
</c:forEach>
You can use Spring's form taglib instead of plain <form> to post back to a Spring MVC Controller and then it will Bind the values back to the model you specify.
<form:form method="post" action="addContact.html">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
...
#RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(#ModelAttribute("contact")
Contact contact, BindingResult result) {
See this Post: http://viralpatel.net/blogs/spring-3-mvc-handling-forms/
You might want to consider giving id to each and nested tags to differentiate between row that you want to POST
<c:forEach items="${searchResults}" var="searchResult">
<tr>
....
<form:form id="${searchResults.key}-form" method="POST" ACTION="./customercare">
<form:input id="${searchResults.key}-btn" type="SUBMIT" value="Select This Customer"/>
<form:input id="${searchResults.key}-hidden" type="hidden" name ="${searchResults.key}" value="searchResult['${searchResults.key}']"/>
</form:form>
</tr>
On the backend side you will have to write the controller as suggested by #PatBurke
You could have 1 form per customer with a lot of hidden inputs in. When that customer is selected you can POST that form. Spring can then bind all the hidden inputs to your customer object.
(Generally, I would just send the id only and load the customer info, as an entity, from a Database. However, I assume you must have a good reason for not wanting to do this)

Categories