Use doPost with link - java

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>

Related

How to pass arguments to a java api from html code using play framework?

I want to pass arguments from HTML view to Java API in play framework.
I've written the following code. What changes should be done to successfully pass the arguments from Html view to API in java controller?.
When i tried passing it as #w.username and #w.email or by keeping '#w.username' or #w.username i've got error.
#import model.User
#(users: List[User])
<html>
<body>
<h1>Users</h1>
<table>
<thead>
<th>User Name</th>
<th>Email</th>
<th>Status</th>
</thead>
<tbody>
#for(w <- users) {
<tr>
<td>#w.username</td>
<td>#w.email</td>
<td>#w.status</td>
<td>
<form action="#routes.UserController.getUser(w.username,w.email)">
<input type = "submit" value="View Deatils">
</form>
</td>
</tr>
}
</tbody>
</table>
</body>
</html>
It must work just like you wrote it:
#routes.UserController.getUser(w.username,w.email)

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.

How to retrieve the data of <s:radio> within <s:iterator> tag into Struts2's action

I need to do something in webpage like this :
a bunch of rows that display questions and choices , the choices are made up of radio buttions.
I don't konw how to get these radio button's data into my java code.
This is my jsp code:
<s:iterator value="questionsList" status="status" var="questionsList">
<tr>
<td><s:property value="title"/></td>
</tr>
<tr>
<td>
<s:radio list="{'A','B','C','D'}" name="[%{#status.index}]answer" theme="simple"/>
</td>
</tr>
</s:iterator>
I've tried
private List answer = new ArrayList();
answer = (List)request.getAttribute("[%{#status.index}]answer");
but it doesn't work.
If you have other means to do this please tell me, thank you!
You can provide indexed property names for the lists you want to get in Java
<s:iterator value="questionsList" status="status">
<tr>
<td><s:property value="title"/></td>
</tr>
<tr>
<td>
<s:radio list="{'A','B','C','D'}" name="questionsList[%{#status.index}].answer" theme="simple"/>
</td>
</tr>
</s:iterator>
Voila! Your question list will be populated with indexes provided by status.index variable.

How to edit fields of a table?

I have a table to show a long list of items, I am wondering how I can edit the fields and submit the form to update them?
<form name="edit" method="POST" action="edit">
<table border="4">
<tbody>
<c:forEach items="${basket.items}" var="item">
<tr>
<td>
<input name="item.id" value="${item.id}"/>
</td>
<td>
<input label="Price" value="${item.product.price}"/>
<br/>
</td>
</tr>
</c:forEach>
</tbody>
</table>
this is a new one
<input id="edit" type="submit" name="edit" value="Edit"/>
</form>
You are using Struts2, with JSTL and EL instead of Struts Tags and OGNL... is there a particular reason that forces you to drop most of the framework mechanics ?
That said, your inputs aren't valid (no type specified) and the "this is a new one" sentence in the HTML seems to indicate the willing to insert a new row, instead of editing the existing entres. Your description and your code seem to ask two different things... to insert a new one, just make a call to another method of the action (or another action) called "add" instead of "edit", sending one single element and adding it to the collection. No need to use AJAX here...
If instead, the question is really:
how I can edit the fields and submit the form to update them ?
this is the way:
<s:form method="POST" action="edit">
<table border="4">
<tbody>
<s:iterator value="basket.items" var="item" status="ctr">
<tr>
<td>
<s:textfield name="item[%{#ctr.index}].id" />
</td>
<td>
<s:textfield name="item[%{#ctr.index}].product.price" />
</td>
</tr>
</s:iterator>
</tbody>
</table>
<s:submit value="Edit"/>
</form>
I would suggest you make an AJAX call using jquery to update the new one. And then in the success handler you can append the new line to the existing table. Before doing that you need to give your table proper ids so that its easier to use JQUERY.
var newLine = document.createElement("tr");
var cellName = document.createElement("td");
$(cellName).text("itemId");
$(newLine).append(cellName);
// similarly create other td's
$("#modelTable").append(newLine);// replace modelTable by the id of your table

Spring MVC Error Messages

Spring MVC Error Messages
Hello, Spring Fellows,
I have a form that is validated by the Spring Validation once submitted. Each field on the form may contain multiple errors messages if validation fails, so error messages are displayed below the field, not next to it. Here's the code snippet.
<tr>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
Note that there is a star at the end of the path value to indicate that all error messages for the name must be displayed.
As you can see, the problem is that, if there is no error message, there will be an extra row on the page that looks out of place to the user. The code above is an overly simplied version, so the actual code has a lot more stuff in it, which prevents me from moving the <form:errors> tag inside the tag containing the field.
Is there a way to find out if there is any message associated to a given path on the JSP level? Basically, I would like to do the following:
<c:if test="${what do I write here?}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
Thanks!
You can do something like this (notice that bind is from spring taglib):
<spring:bind path = "name*">
<c:if test="${status.error}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
</spring:bind>
I solved your problem by doing this:
<table>
<form:errors path="firstName">
<tr>
<td colspan="2">
<form:errors path="firstName"/>
</td>
</tr>
</form:errors>
<tr>
<td><form:label path="firstName"><spring:message code="helloworld.label.firstName"/></form:label></td>
<td><form:input path="firstName"/></td>
</tr>
</table>
errors tag body will be evaluated only if there are errors on the path.
The simplest answer is to not use tables for page layout. Utilizing div tags alleviates this problem altogether since divs are completely dimensionless if set to hidden.

Categories