Build dynamic checkboxes - jstl

Hey guys I have about 8 fieldSets and Im iterating over a list. I want to fill up the checkboxes based off a value of each iteration for ex.
<c:if test="${detBean.groupName == 'HEADER_DATA}">
*Add that checkbox to that fieldset and so on...
<c:forEach var="detBean" items="${detFields}">
Display Name -- ${detBean.displayName}
Field Name -- ${detBean.fieldName}
Group Name -- ${detBean.groupName}
</c:forEach>
<tr>
<td>
<div id="displayFields" style="display:block;">
<fieldset class="det">
<legend>Header Data</legend>
<input type="checkbox" name="${detBean.displayName}
" value="${detBean.displayName}
">${detBean.displayName}
</input>
</fieldset>
<fieldset class="det">
<legend>Materiel Data</legend>
<input type="checkbox" name="${detBean.displayName}
" value="${detBean.displayName}
">${detBean.displayName}
</input>
<br/>
</fieldset>
</td>
</tr>
Thankx

This worked:
<div id="displayFields" style="display:block;">
<fieldset class="det">
<legend>Header Data</legend>
<c:forEach var="detBean" items="${detFields}">
<c:if test="${detBean.groupName == 'HEADER_DATA'}">
<input type="checkbox" name="${detBean.displayName}" value="${detBean.displayName}">${detBean.displayName}</input>
<br/>
</c:if>
</c:forEach>
</fieldset>
<fieldset class="det">
<legend>Materiel Data</legend>
<c:forEach var="detBean" items="${detFields}">
<c:if test="${detBean.groupName == 'MATERIEL_DATA'}">
<input type="checkbox" name="${detBean.displayName}" value="${detBean.displayName}">${detBean.displayName}</input>
<br/>
</c:if>
</c:forEach>
</fieldset>

Related

Spring MVC editable table (Thymeleaf)

I have 2 entities - Invoice and InvoiceProduct. Invoice class has a field List of InvoiceProduct because Invoice has #OneToMany mapping on InvoiceProduct.
I want to send the List of InvoiceProduct to Thymeleaf with with editable fields with a Submit button. When I click on Submit button, the new edited List should be returned to the controller. However, I am able to send the list to the view, but when I send the edited list to controller, it is being passed as 'null'.
update-invoice.html:
<form action="#" th:action="#{/api/invoice/saveInvoice}" th:object="${invoice}" method="POST">
<table class="table table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th>PID</th>
<th>Quantity</th>
<th>Unit Price</th>
</tr>
</thead>
<tbody>
<tr th:each="temp : ${invoice?.invoiceProducts}" >
<td><input name="invoiceProducts[${tempStat.index}].productId" th:value="${temp.productId}"/></td>
<td><input name="invoiceProducts[${tempStat.index}].quantity" th:value="${temp.quantity}"/></td>
<td><input name="invoiceProducts[${tempStat.index}].price" th:value="${temp.price}"/></td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-info col-2">Submit</button>
</form>
InvoiceController:
#PostMapping("/saveInvoice")
public String postInvoice(#ModelAttribute("invoice") Invoice invoice) {
logger.info("invoice products " + invoice.getInvoiceProducts());
return "dummy";
}
What else do I need to add so that my update-invoice.html passes the list to my controller ?
You can try something like below as an example for thymleaf:-
<form method="post" th:action="#{/users/}" th:object="${userInfo}" class="col card p-3 mb-5">
<div class="form-group">
<label for="firstName">First Name</label>
<input id="firstName" placeholder="Enter First Name" required type="text" th:field="*{firstName}"
class="form-control"/>
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input id="lastName" placeholder="Enter Last Name" required type="text" th:field="*{lastName}"
class="form-control"/>
</div>
<div class="form-group">
<label for="role">Role</label>
<select id="role" required th:field="*{role}" class="form-control ">
<option value="" hidden>Select a role</option>
<option th:each="role : ${T(com.springhow.examples.springboot.thymeleaf.domain.entities.Role).values()}"
th:value="${role}"
th:text="${role}">
</option>
</select>
</div>
<input type="submit" class="btn btn-primary" value="Create User">
</form>
and into Controller:-
#RequestMapping(value = "/", method = RequestMethod.POST)
public String createUser(Model model, #ModelAttribute UserInfo userInfo) {
UserInfo user = userService.createUser(userInfo);
return "redirect:/users/";
}

Button can't save first item when clicked

In a foreach loop, I have more than one item and each item has a button called "save". However, when I click on the first "save" button, it saves only the last item in my database.
#foreach($product_dr as $product)
<tr>
<td style="font-size: 20px;color: #101010">
<div class="form-group">
<input type="hidden" name="title" value="{{$product->title}}">
<label for="inputText3" class="col-form-label"></label>
{{$product->title}}
</div>
</td>
<td style="font-size: 20px;color: #2ec551">
<div class="form-group">
<input id="inputText4" VALUE="{{$product->price}}" type="text" name="price" class="form-control" placeholder="Amount" style="width: 100px">
</div>
</td>
<td style="font-size: 20px;color: #2ec551">
<div class="form-group">
<input id="inputText4" VALUE="1" type="number" name="amount" class="form-control" placeholder="Amount" style="width: 100px">
</div>
</td>
<td style="font-size: 20px;color: #2ec551">
<div class="form-group">
<input type="hidden" name="table" value="{{$table[0]->number}}">
<input type="hidden" name="table_number" value="{{$table[0]->number}}">
<label for="inputText3" class="col-form-label"></label>
{{$table[0]->number}}
</div>
</td>
<td>
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12 ">
<button class="btn btn-success" type="submit">Save</button>
</div>
</td>
</tr>
#endforeach
The name of the variables must be an array as such:
<input type="hidden" name="title[]" value="{{$product->title}}">
This way you will have an array on your backed that you can iterate. If not, you will only get one value for "title"

Is it possible to control loop(forEach) on click of button on JSP page?

I'm trying to create a question/answer quiz where there are multiple choice answers(in radio buttons) per question.
I want to display One question and it's relevant set of answers at a time. How do I iterate over the list with control so on click it would display the next question and answers?
<form action="/addStuResponse" method="post">
<c:forEach items="${qlist}" var="question" varStatus="loopCounter">
<input type="hidden" name="quesSet" value="${question.quesId}">
<textarea rows="3" cols="5" readonly="readonly">${question.quesText}</textarea>
<c:forEach items="${anslist}" var="answer">
<c:if test="${answer.questions.quesId == question.quesId}">
<input type="radio" name="response" value="${answer.answer}">
</c:if>
</c:forEach>
</c:forEach>
<button type="submit" >Next</button>
</form>
Keep a flag isCurrentQuestionon each question. Set first question flag to true and rest of the questions flag to false. Onclick of next button change the isCurrentQuestion flag to false for first question and make the second question as true. and continue like this until the last question.
Modify your code as below:
<form action="/addStuResponse" method="post">
<c:forEach items="${qlist}" var="question" varStatus="loopCounter">
<c:if test="${question.isCurrentQuestion == true}">
<input type="hidden" name="quesSet" value="${question.quesId}">
<textarea rows="3" cols="5" readonly="readonly">${question.quesText}</textarea>
<c:forEach items="${anslist}" var="answer">
<c:if test="${answer.questions.quesId == question.quesId}">
<input type="radio" name="response" value="${answer.answer}">
</c:if>
</c:forEach>
</c:if>
</c:forEach>
<button type="submit" >Next</button>
</form>

How do I assign an object to a member class in JSP

I am building a tour application. I have bookings which point to a tour modality but I haven't been able to assign the book modality when adding a new booking using JSLT.
Here's my JSP code:
<c:forEach items="${modalities}" var="modality">
<form:form method="POST" action="/book_tour" modelAttribute="booking">
<p>${modality.name}</p>
<jsp:setProperty name="booking" property="tourModality" value="${modality}"></jsp:setProperty>
<input name="modality" type="hidden" value="${modality}">
<div class="form-group">
<form:label path="calendarDate">Escoja una fecha</form:label>
<div class='input-group date' id='datetimepicker'>
<form:input path="calendarDate" type='text' class="form-control"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<div class="input-group">
<form:label path="calendarDate">Hora: </form:label>
<form:select path="calendarDate">
<c:forEach items="${modality.tourHours}" var="tourHour">
<option>${tourHour.timeSlot}</option>
</c:forEach>
</form:select>
</div>
</div>
<input type="submit" class="btn btn-success" value="Reservar"/>
</form:form>

Spring mvc include one single 'form' for different c:url

I am working on a spring-mvc application. In the application, there are various types of events, for which the user can register. As for registration, there is a standard formula with details like firstName, email, address, etc. I am just giving different c:url values depending which event it is.
I was wondering if it is possible to include the form(not the c:url part) in another jsp file, and just call jsp:include. This would serve me very well as when the form needs to be styled, updated, etc, then I need to update only one file. I am including how the form looks. Kindly have a look. Thank
eventregistration.jsp :
<c:url var="addAction" value="/student/add/seminar-e-commerce" ></c:url>
<form:form class="seminar-form" action="${addAction}" commandName="students">
<table>
<form:hidden path="studentid"/>
<label>Firstname/LastName <span class="color-red">*</span></label>
<form:input path="firstname" type="text" class="span6 border-radius-none" />
<span> </span>
<form:input path="lastname" type="text" class="span6 border-radius-none pull-right" />
<label>Email-Address <span class="color-red">*</span></label>
<form:input path="emailadress" type="text" class="span12 border-radius-none" />
<label>Street Name/Number <span class="color-red">*</span></label>
<form:input path="address1" type="text" class="span10 border-radius-none" />
<span> </span>
<form:input path="address2" type="text" class="span2 border-radius-none pull-right" />
<label>City/PLZ <span class="color-red">*</span></label>
<form:input path="city" type="text" class="span10 border-radius-none" />
<span> </span>
<form:input path="plz" type="text" value="" class="span2 border-radius-none pull-right" />
<label>Handy Number <span class="color-red"></span></label>
<form:input path="handynumber" type="text" value="" class="span12 border-radius-none" />
<label path="newsletterpermission">Want to subscribe newsletter?</label>
<form:radiobutton path="newsletterpermission" class="form-control" value="true"/>Yes
<form:radiobutton style="margin-left:15px" path="newsletterpermission" class="form-control" value="false"/>No
<br/>
<br/>
<c:if test="${empty students.firstname}">
<input class="btn-u" type="submit"
value="<spring:message text="Register for event"/>" />
</c:if>
</table>
</form:form>
Any pointers would be nice. Thanks a lot.
You can achieve this with an entrypoint/method in your controller with a Pathvarible of action like below
#RequestMapping(value="/student/{action}", method = RequestMethod.POST)
public String EntryPoint(#PathVaribale String action, ModelMap model) {
switch (action) {
case "add":
// delegate to addMethod
break;
case "activate":
// delegate to activateMethod
break;
case "register":
// delegate to registerMethod
break;
default:
///Unsurported operation
}
return "index";
}
and you can keep the c:url , remember to set up the model.action attribute on a controller method responsible for setting up the form bean, for your case students bean or make it more general
<c:url var="action" value="/student/model.action/" ></c:url>

Resources