Spring mvc controller pass list of values to view - java

I have a page will show a table of data(from database) on start and I am creating multiple fields for searching/filtering purpose.
My controller:
#RequestMapping(value = "/country", method = RequestMethod.GET)
public ModelAndView getCountryList(Model model) {
List<Country> countryList = countryService.getCountryList();
model.addAttribute("dateTime", todayDate());
model.addAttribute("pageTitle", "<a href=\"/pgwrefund\" />COUNTRY</a>");
return new ModelAndView("country", "countryList", countryList);
}
#RequestMapping(value="/filterCountry")
public ModelAndView getFilteredCountryList(#RequestParam String countrycode,#RequestParam String countryname,Model model){
List<Country> filteredCountryList = countryService.getFilteredCountryList(countrycode, countryname);
model.addAttribute("dateTime", todayDate());
model.addAttribute("pageTitle", "<a href=\"/pgwrefund\" />COUNTRY</a>");
System.out.println("List country:"+filteredCountryList.size() +"\nValue:"+ filteredCountryList);
return new ModelAndView("countrytemplate","filteredCountryList",filteredCountryList);
}
My View
<tbody>
<c:forEach var="country" items="${countryList}" varStatus="loopStatus">
<tr class="tbodydata ${loopStatus.index % 2 == 0 ? 'even' : 'odd'}">
<td>${country.countryCode}</td>
<td>${country.countryName}</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</c:forEach>
</tbody>
My filtered table view:
<c:forEach var="country" items="${filteredCountryList}" varStatus="loopStatus">
<tr class="tbodydata ${loopStatus.index % 2 == 0 ? 'even' : 'odd'}">
<td class="firsttd">${country.countryCode}</td>
<td class="secondtd">${country.countryName}</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</c:forEach>
So now, What I have done here is, first the i enter values to all the field and filter. The view pass the values to controller and it will filter using a query and return back to the view BUT with another table(foreach). Basically, I am replacing the filtered data with the original data. Then problems came up, most of the functions wont work including update and delete. My goal is, my country page able to filter the table data and stay at the same page. Any help will be appreciated.

Related

Populating inner table inside another table with Thymeleaf

I have DTO class:
private LocalDate date;
private List<String> firstShift;
private List<String> secondShift;
private List<String> thirdShift;
With getters, setters and toString for every field.
I'm creating this table for shift schedule calendar:
My Thymeleaf:
<table class = "table table-striped table-hover" id = "schedule_table">
<thead>
<tr>
<th class = "th_schedule_table">Date</th>
<th class = "th_schedule_table">First Shift</th>
<th class = "th_schedule_table">Second Shift</th>
<th class = "th_schedule_table">Third Shift</th>
</tr>
</thead>
<tbody>
<tr th:each = "calendar_node : ${calendarNodeList}">
<td th:text = "${calendar_node.date}"></td>
<td>
<table>
<tbody>
<tr th:each = "employee, i : ${firstShift}">
<td th:text = "${firstShift.[i]}"></td>
</tr>
</tbody>
</table>
</td>
<td>
<table>
<tbody>
<tr th:each = "employee: ${secondShift}">
<td th:text = "${employee}"></td>
</tr>
</tbody>
</table>
</td>
<td>C</td>
</tr>
</tbody>
</table>
The idea behind this: CalendarNode.date generates row for parent table, for each date. That works fine.
Inside every row, in a cell I have second table that should show list of employees who works on that date in that shift. If I have calendar_node object in one row, I'm using his "firstShift" field to generated rows for second inner table.
The problem is that I'm getting empty table. I checked my back-end and I have two employees for first date (18th July), first shift, one employee for second shift, but none is shown. I tried a lot of different syntax for Thymeleaf, none of it works. I guess I did Thymeleaf wrong?
UPDATE:
Example of data that has been passed to web page via model object:
If firstShift is a field of calendar_node, then you need to actually address it as such in the template:
${calendar_node.firstShift} instead of ${firstShift}

Pull array from table back into controller to create or update new array

So I have a table generated using EL that is requested from a Java class
<table id="medTable" class="goldTable">
<thead>
<tr id="titles">
<td>Resident SSN</td>
<td>Medication ID</td>
<td>Quantity Received</td>
</tr>
</thead>
<c:forEach var="item" items="${allMeds}">
<tr>
<td <!--onclick="listenForDoubleClick(this);" onblur="this.contentEditable=false;"-->${item.resSSN}</td>
<td <!--onclick="listenForDoubleClick(this);" onblur="this.contentEditable=false;"-->${item.medID}</td>
<td onclick="listenForDoubleClick(this);" onblur="this.contentEditable=false;">${item.quantity}</td>
</tr>
</c:forEach>
</table>
The problem, as you can see is the quantity of this table is editable for correction purposes. Now the issue is I don't know how to receive this new information inside my controller to create a new, updated array...This is what my controller looks like for the "update" action...
} else if (request.getParameter("update") != null) {
String[] residentSSN = request.getParameterValues("resSSN");
//pull from html into multiple arrays
//push back to html, but content uneditable
String[] medicationID = request.getParameterValues("medID");
String[] quantity = request.getParameterValues("quantity");
//int quantityNum = Integer.parseInt(quantity);
//allMeds = new ArrayList<OrderedMeds>(residentSSN, medicationID, quantityNum);*/
url = "/ReceiveMeds.jsp";
}
This is what I've tried and clearly it doesn't work. Please help!

How can i go to next line inside the th:text in Thymleaf?

I m encountering a problem with the '\n' or in thymleaf the problem is i need to enter to a new line. I have iteretate inside the inside of cell. I m getting the content but just in one line and i want one tweet under the other inside the cell. and i saw as well some similiar topic but it is not working in my case.
This is what i m getting:
How should i manage it to go in the next line inside one cell.
The Thymleaf code:
<tr>
<td th:text="${user.id}">1</td>
<td th:text="${user.getUsername()}">Hamdo</td>
<span th:each="tweet : ${tweets}">
<td th:text="${tweet.content} " ><br/>
<br/>
</td>
</span>
The controller :
#GetMapping({"", "/", "/index", "/index.html"})
public String followers(Principal principal, Model model) {
User user=userService.getUser(principal.getName());
model.addAttribute("tweets",
tweetService.tweetsFromUser(principal.getName()));
model.addAttribute("user",user);
return "index";
}
The service class:
private List<TweetDTO> tweetsFromUser(User user) {
return tweetRepository.findAllByAuthor(user).stream().map(TweetDTO::new).collect(toList());
}
You don't need the extra span... just loop inside of the <td />, like this:
<tr>
<td th:text="${user.id}">1</td>
<td th:text="${user.username}">Hamdo</td>
<td>
<p th:each="tweet : ${tweets}" th:text="${tweet.content}" />
</td>
</tr>

How to return a single data from a row from a table in html?

I have a table that stores the product in the cart of a user. Each row has a button that enables the user to remove a single product from the cart of a user. Here is the code snippet of my html.
<form action="${pageContext.request.contextPath}/customer/removeProduct" method="post">
<input type="hidden" name="page" value="${page}">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<c:forEach items="${productsInCart}" var="product"
varStatus="status">
<input type="hidden" class="form-control" name="upc"
value="${product.getUpc()}">
<tr class="warning">
<td>${product.getName()}</td>
<td>${product.getQuantity()}</td>
<td style="color: green;">₱ ${product.getTotal()}</td>
<td><input type="submit" class="btn btn-warning btn-xs"
value="Remove from cart"></td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
Here is the output.
Product in cart table
The value I want to get is the UPC of a product which is stored in a hidden input type field. However, when the 'remove from cart' button is clicked, it returns all the UPC of the product that is in the cart. Here is the code snippet of the controller.
#RequestMapping(value="/customer/removeProduct", method=RequestMethod.POST)
public String removeProduct(#RequestParam(value = "upc") String upc, HttpServletRequest request){
System.out.println(upc);
// customerService.removeProductInCart(customer.getCustomer(request).getCartId(), upc);
return "customer_home";
}
You may want to create form on every iteration to get the specific upc on each row instead of wrapping your whole table.

get map from jsp-form spring

I am new in Spring MVC, and I have a problem.
I'm sending to FORM LinkedHashMap, and it's showing great.
model.addAttribute("resultForm", resultForm);
Part of my jsp:
<c:forEach items="${resultForm}" var="resultMap" varStatus="status">
<tr id="tableRow" class="table-row">
<td>
${resultMap.key}
</td>
<td>
<select name="resultMap['${resultMap.key}']" class="espa-config-select">
<option selected value ="${resultMap.value}">${resultMap.value}</option>
<c:forEach items="${mainParams}" var="item2">
<c:if test="${item2.key == resultMap.key}">
<c:forEach items="${item2.value}" var = "q">
<c:if test="${resultMap.value != q}">
<option value="${q}"> ${q} </option>
</c:if>
</c:forEach>
</c:if>
</c:forEach>
</select>
</td>
</tr>
</c:forEach>
Now I need to get it back
Here is part of Controller
#RequestMapping( value = "espa/update", method = RequestMethod.POST )
public String save(#ModelAttribute("resultForm") LinkedHashMap<String,String> resultForm) {
System.out.println("resultMap post "+resultForm.toString());
if (resultForm!=null){
//resultForm.put("Port", port);
espaService.setConfiguration(selectedDevice, resultForm);
LOG.debug("Saving configuration: {} /nPort{}",resultForm, selectedDevice);
}
return "redirect:/espa";
}
But it is empty!
How can I fix it?
In your select you are using the name "resultMap". The name attribute needs to correlate to the Model Attribute, which you are called "resultForm".

Categories

Resources