Different calls from the same JSP page - java

In a JSP page I use one form to get the entire DB and another form (and the related servlet request and the specific sort command). Once I sort a field, the form with the whole DB become empty, so the two Forms are not working one after another, as I expected.
For the moment I am not sure whether it's a HTML problem or something else, so I put just a second button to get a refresh. However, I assume there must be a smarter & simpler way using jQuery.
Thank you in advance!
<table cellpadding="10">
<form name="Refresh" action="AllPost">
<input type="submit" value="Refresh " /> ¦
</form>
<form name="F" action="SortedPost" id="formular" method="post">
<input type="submit" value="Sort: " />
<select name="Titl">
<c:forEach items="${AllPost}" var="p">
<option> ${p.Item} </option>
</c:forEach>
</select>
</form>
</table>
<table>
<c:forEach items="${SortedPost}" var="p">
<tr>
<td>ID: ${p.id}</td>
<td>bla bla : ${p.blabla}</td>
</tr>
</c:forEach>
</table>
<table>
<c:forEach items="${AllPost}" var="p">
<tr>
<td>${p.id}</td>
<td>bla bla : ${p.blabla}</td>
</tr>
</c:forEach>
</table>

Related

Checking html hidden value in jstl <c:if>

I have one html input hidden
<input type="hidden" id="todayRunJnID" name="todayRunJnID" value="some value"/>
and also have a table using jstl <c:forEach> in the same jsp page
<c:forEach items="${todaysRunJnList}" var="todayRunJn">
<tr>
<td>
<c:out value="${todayRunJn.uploadedBy}" />
</td>
<td>
<c:out value="${todayRunJn.startDateTime}" />
</td>
<td>
<c:out value="${todayRunJn.status}" />
</td>
</tr>
</c:forEach>
I would like to check the input hidden value includes in <c:forEach> items like this.
<c:forEach items="${todaysRunJnList}" var="todayRunJn">
<c:if test="${todayRunJn.id == input hidden value}">
<tr>
...
</tr>
</c:if>
</c:forEach>
Is it possible? According to the requirement, I don't want to call servlet again. The input hidden value come from jquery function.So this value is dynamic.
$(".runJnId").click(function() {
var valueId = $(this).data('id');
$("#todayRunJnID").val(valueId);
});

Thyme leaf-SpringBoot-Targetting Map.Entry within Map property of Form-bound object

Can't find any example or way to do this, and finally registered to this forum for help!
My application is a Questionnaire - I display questions and users select answer from a drop down list.
I have two Model Attributes
1) a list of Question objects "questionList" is variable name in the Model
2) salesResponse is the variable name of the other attribute - its an object of SalesResponse class.
SalesResponse has a property called "responses" which is a TreeMap. It has a default constructor, and setter/getter for "responses".
On POST, the key should be the question's ID, and the value should be the value of option selected.
So, should be TreeMap. This is the Map I'd like to be populated in my salesResponse object when user does a POST.
My controller loads these two objects into the context...I'm not sure how to write the form! Here is roughly what I need:
<form action="#" th:action="#{/ehc}" th:object="${salesResponse}"
method="post">
<table>
<tr th:each="question: ${questionList}">
<td th:text="${question.questionText}" th:value="${question.ID}"
th:name=${..what??..}
th:field="*{THIS SHOULD BECOME the KEY in responses TreeMap}"></td>
<td><select th:field="*{THIS SHOULD BECOME THE VALUE in responses TreeMap}">
<option th:text="${question.option1}" th:value="2"></option>
<option th:text="${question.option2}" th:value="4"></option>
<option th:text="${question.option3}" th:value="6"></option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="reset" /></td>
</tr>
</table>
</form>
How should this be written? Will Spring be smart enough to populate the responses TreeMap, One Map.Entry at a time?
EDIT: I tried this, and it didn't work (Got this exception), but maybe it gives someone inspiration to solve this!
java.lang.NumberFormatException: For input string: "iterStat.index"
Trial code:
<form action="#" th:action="#{/ehc}" th:object="${salesResponse}"
method="post">
<table>
<tr th:each="question,iterStat: ${questionList}">
<td><input type="number" readonly="readonly"
th:text="${question.questionText}" th:name="${responses}"
th:value="${question.ID}"
th:field="*{responses[iterStat.index].key}"/></td>
<td><select th:field="*{responses[iterStat.index].value}">
<option th:text="${question.option1}" th:value="2"></option>
<option th:text="${question.option2}" th:value="4"></option>
<option th:text="${question.option3}" th:value="6"></option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="reset" /></td>
</tr>
</table>
</form>
After a lot of sleepless nights, I've got this working, and documenting it so that someone else trying this would benefit:
Here is the code from the view:
<form action="#" th:action="#{/ehc}" th:object="${salesResponse}"
method="post">
<table>
<tr th:each="entry,iterStat: ${responses}">
<td th:text="${salesResponse.questionList[iterStat.index].questionText}"></td>
<td><select th:name="'responses[' + ${entry.key} +']'">
<!-- <td><select th:field="*{responses[__${entry.key}__]}">-->
<option selected="selected" th:text="${salesResponse.questionList[iterStat.index].option1}"
th:value="2" ></option>
<option th:text="${salesResponse.questionList[iterStat.index].option2}"
th:value="4" ></option>
<option th:text="${salesResponse.questionList[iterStat.index].option3}"
th:value="6" ></option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="reset" /></td>
</tr>
</table>
</form>
The key factor was getting the select tag correct. When the HTML is rendered, you can see "responses[n]" in the select's name field. That essentially refers to the 'Key' of the resulting map and the value field is the corresponding Value.
Now, having done this, I did notice that when I used "th:field" my responses getter was been called 300 times for just 15 entries in the Map. I replaced the th:field with a th:name to generate the same html, and now its been called 15 times only. Tried searching for a reason online, but haven't found anything. Right now through, I'm happy to move on since spending so much time on this roadblock!

Access to java list of object and with js or extjs

I want to work on a list of object with javascript (i can use extjs too in my project). You can see that I have two list in my jsp (modelList and categoryList) forwarded by my controller. and I want to access data into my two list. Is it possible ?
my jsp :
<table class="form">
<tr>
<td class="label"><a class="required"></a><spring:message code="serviceModel.category" /></td>
<td class="value">
<form:select path="category" onchange="javascript:App.reloadUserNumberOnCategoryChangeForWorkflow(this, ${categoryList}, ${modelList}) ;App.removeError(this)>
<form:option label="" value="" />
<form:options items="${categoryList}" itemLabel="description" itemValue="description" />
</form:select>
<form:errors path="category" cssClass="errormsg" />
</td>
</tr>
<tr>
<td class="label"><a class="required"></a><spring:message code="service.userNumber" /></td>
<td class="value">
<form:select path="userNumber">
<form:option label="" value="" />
<form:options items="${modelList}" itemLabel="description" itemValue="description" />
</form:select>
<form:errors path="userNumber" cssClass="errormsg" />
</td>
</tr>
</table>
my controller :
uiModel.addAttribute(WebConstants.CATEGORY_LIST, catList);
uiModel.addAttribute(WebConstants.MODEL_LIST, modelList);
You could create a JSON service that turned your java object into a JSON array, and then retrieve that list from the server with an AJAX call.
Spring, Struts, etc. have built in ways to do this, or you could roll your own with a good 'ol servlet and a JSON library (you should be able to find one at json.org).
Not directly, no.
The only way this would be possible would be by using the View to write JavaScript code that would define the object.
In your case, it would be something like
<script type="text/javascript">
<c:if test="${not empty modelList}">
var modelList = [
<c:forEach var="model" items="${modelList}">
{
description: '<c:out value="${model.description}"/>',
otherProperty: '<c:out value="${model.otherProperty}">'
},
</c:forEach>
]
</c:if>
</script>

SpringMVC - Send nested form in jsp

I have a form (addPassForm) which is nested in another form (addStoreFrom) in the JSP page. How can I send the addPassForm form to the controller?
When I try to send the addPassForm form, addStoreFrom form sends instead.
<s:url value="/addStore" var="urlAddStore"/>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<table border="1">
<tbody>
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</tbody>
</table>
</form:form>
It is just because nested forms in not a valid HTML pattern. The browser simply ignore the inner <form></form> tags and sees only one form. Reference : Is it valid to have a html form inside another html form?
It is not a JSP problem (nor a Java one !), but only a incorrect HTML problem. You must use successive forms instead of nested forms (or user javascript as other suggested)
Example with successive forms :
<s:url value="/addStore" var="urlAddStore"/>
<table border="1">
<tbody>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</form:form>
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
</tbody>
</table>
Explicitly call submit() function in javascript.Add a function and bind it to onclick of add button.<input type="button" value="Add" onclick="submitAddPassForm()"/>.In javasrcipt simply use:
function submitAddPassForm(){
$('#addPassForm').submit();
}
You can just make a ajax post call instead of a form submit

Ho to get the HTTP input field value and use it to do some tests using c:if

I'm having a Spring MVC application here. When menu page is opened i'm putting two model attributes: menuList and orderList. menuList is filled with infos from database and orderList is empty list that i want to be full-filled when the client is going to submit the dynamic form. I know how to bind all the products from the menu in the order list but that would be waste... of many things. What i need to do is to check if the value or length of the dynamically appended input field "pieces", next to each and every product, is greater than 0. How can i do that?
<c:url value="/menu/order" var="orderPath"/>
<form:form action="${orderPath}" method="POST" modelAttribute="orderList">
<table>
<tr>
<th>Name</th>
<th>Code</th>
<th>Price</th>
<th>Description</th>
<th>Calories</th>
<th>Image</th>
<th>Pieces</th>
</tr>
<c:set var="index" value="0" scope="page" />
<c:forEach items="${menuList}" var="menu" varStatus="i">
<tr>
<td>${menu.name}</td>
<td>${menu.code}</td>
<td>${menu.price}</td>
<td>${menu.description}</td>
<td>${menu.calories}</td>
<td><img src="<c:url value="/images/${menu.image}" />" alt="" width=120></td>
<td><input type="text" name="pieces"></td>
<!-- i need to take the "pieces" input field value here and check it if,
lets say, the length is greater than 0 (or the value is greater than 0)
and to make data binding only in those situations
I saw something like <c:if test="${param.pieces}>0"> but that's not working
or maybe i'm doing some kind of mistake, i don't know
-->
<!--start: this code should be executed if the condition is evaluated to true -->
<form:hidden path="orderList[${index}].orderID"/>
<form:hidden path="orderList[${index}].menuID" value="${menu.menuID}"/>
<form:hidden path="orderList[${index}].pieces" value="**value-of-pieces-input-field**"/>
<c:set var="index" value="${index + 1}" scope="page" />
<!--end -->
</tr>
</c:forEach>
<tr>
<td colspan="2">
<input type="submit" value="Make an order"/>
</td>
</tr>
</table>
</form:form>
You're close.
<c:if test="${param.pieces > 0 }">
<form:hidden path=.../>
</c:if>

Categories