How to read MomgoDB data from HTML page using Sprinboot - java

This is my controller class read data method. I cant read spring data from requesting. when I do it from postman it works fine but cant read using html page.
#RequestMapping("/index")
public String ItemList(Model model) {
model.addAttribute("itemList", repository.findAll());
return "index";
}
Like this I try to read data using my index.html page
<h2>Medicine List</h2>
<form action="/index" method="POST">
<table>
<thead>
<tr>
<th>ID</th>
<th>MedicineName</th>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
<th>ExDate</th>
</tr>
</thead>
<tbody>
<tr th:each="item:${itemList}">
<td th:text="${item.id}"></td>
<td th:text="${item.MedicineName}"></td>
<td th:text="${item.Description}"></td>
<td th:text="${item.Quantity}"></td>
<td th:text="${item.Price}"></td>
<td th:text="${item.ExDate}"></td>
<td><form action="/delete/item:${itemList}" method="Delete"><input type="submit" value="Delete" /></form></td>
</tr>
</tbody>
</table>
</form>

Related

how to know which button is clicked in jsp foreach loop

I have a JSP page that's attached to a list of objects I need to loop through using a JSTL loop. Basically, I want it to display different information about the book and a submit input view (send to another jsp page to see more details about the book).
My question is how can I know which button was clicked and how can I recover the corresponding book.
<table class="table table-striped">
<thead>
<tr>
<th scope="col">N°</th>
<th scope="col">Title</th>
<th scope="col">ISBN</th>
<th scope="col">Author</th>
<th scope="col">Year</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"></th>
<c:set var="count" value="${1}" scope="request"/>
<c:forEach items= "${requestScope.booksList}" var="b">
<tr>
<td>${count }</td>
<td> ${b.getTitle() } </td>
<td> ${b.getISBN() } </td>
<td> ${b.getAuthor() } </td>
<td> ${b.getYear() } </td>
<td><input type="submit" value="View" name="View${count }"></td>
<td><input type="submit" value="Mark" name="Mark"></td>
</tr>
<c:set var="count" value="${count + 1}" scope="request" />
</c:forEach>
</tr>
</tbody>
</table>

Thymeleaf. How to hide element based on boolean parametr?

I have a table of reviews
<table class="table">
<tbody>
<tr th:each="review : ${reviewsForMovie}">
<td th:utext="${review.text}"></td>
</tr>
</tbody>
</table>
But I dont want to show review if review.isApproved == false
How do i do that?
<table class="table">
<tbody>
<tr th:each="review : ${reviewsForMovie}">
<td th:hidden="${!review.isApproved}" th:utext="${review.text}"></td>
</tr>
</tbody>
</table>

Neither BindingResult nor plain target object for bean name available as request attribute (Spring MVC)

Please help me. Im trying to use 2 Forms on 1 jsp. The first Form works just fine. When i try to submit the second form i get the IllegalStateException. I have already read a lot of things about this problem, but nothing could solve my current problem.
Form:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<head>
<%# include file="../views/common/imports.jsp" %>
<title>MARC</title>
</head>
<body onload="togglePieLines()">
<%# include file="../views/common/header.jsp"%>
<br>
<br>
<div class="standard-form">
<form:form commandName="unit" method="post" action="units" class="uk-form" id="unitForm">
<!-- Allgemeine Informationen -->
<table class="uk-table uk-table-condensed uk-text-small">
<thead>
<tr>
<th><spring:message code="units.generalInformation"/></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td class="uk-width-1-1 td-form-row"><spring:message code="units.id"/> * <form:errors path="unitId" class="uk-badge uk-badge-warning"/></td>
<td class="uk-width-1-1"><form:input path="unitId" class="uk-form-width-medium" /></td>
</tr>
<tr>
<td><spring:message code="units.name"/> * <form:errors path="name" class="uk-badge uk-badge-warning"/></td>
<td>
<form:input path="name" class="uk-form-width-medium" />
</td>
</tr>
<tr>
<td><spring:message code="units.country"/> * <form:errors path="country" class="uk-badge uk-badge-warning"/></td>
<td>
<div class="uk-form-row">
<form:select path="country" items="${countryList}" class="uk-form-width-medium" />
</div>
</td>
</tr>
<tr>
<td><spring:message code="units.bu"/></td>
<td><form:input path="bu" class="uk-form-width-medium" /></td>
</tr>
<tr>
<td><spring:message code="units.pie"/></td>
<td><form:checkbox id="pie" path="pie" onChange="togglePieLines()" /></td>
</tr>
<tr id="pieReason" style="display: none;">
<td><spring:message code="units.pieJustification"/></td>
<td>
<div class="uk-form-row">
<form:select path="pieReason" items="${pieReasonList}" class="uk-form-width-medium" />
</div>
</td>
</tr>
<tr id="pieSince" style="display: none;">
<td><spring:message code="units.pieSince"/></td>
<td>
<div class="uk-form-icon">
<i class="uk-icon-calendar"></i>
<form:input path="pieSince" data-uk-datepicker="{format:'DD.MM.YYYY'}" class="uk-form-width-medium" />
</div>
</td>
</tr>
</tbody>
</table>
<!-- Anwendungsparameter -->
<table class="uk-table uk-table-condensed uk-text-small">
<thead>
<tr>
<th><spring:message code="units.applicationParameters"/></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td class="uk-width-1-1"><spring:message code="units.legalForm"/></td>
<td class="uk-width-1-1">
<div class="uk-form-row">
<form:select path="legalForm" items="${legalFormList}" class="uk-form-width-medium" />
</div>
</td>
</tr>
<tr>
<td><spring:message code="units.group"/></td>
<td><form:checkbox path="consolidationType" /></td>
</tr>
</tbody>
</table>
<!-- Verantwortlichkeiten -->
<table class="uk-table uk-table-condensed uk-text-small">
<thead>
<tr>
<th><spring:message code="units.responsibilities"/></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td class="uk-width-1-1"><spring:message code="units.responsibleUnit"/></td>
<td class="uk-width-1-1">
<div class="uk-form-row">
<form:select path="responsibleUnit" items="${userList}" class="uk-form-width-medium" />
</div>
</td>
</tr>
<tr>
<td><spring:message code="units.responsibleRotation"/></td>
<td>
<div class="uk-form-row">
<form:select path="responsibleRotation" items="${userList}" class="uk-form-width-medium" />
</div>
</td>
</tr>
<tr>
<td><spring:message code="units.responsibleNonAudit"/></td>
<td>
<div class="uk-form-row">
<form:select path="responsibleNonAudit" items="${userList}" class="uk-form-width-medium" />
</div>
</td>
</tr>
</tbody>
</table>
<div class="input-center">
<button class="uk-button" type="submit"><i class="uk-icon-save"></i> <spring:message code="button.save"/></button>
<button class="uk-button" type="reset"><i class="uk-icon-undo"></i> <spring:message code="button.reset"/></button>
<a class="uk-button" href="<%=request.getContextPath()%>/unitsOverview"><i class="uk-icon-arrow-left"></i> <spring:message code="button.back"/></a>
<br>
</div>
</form:form>
<c:if test="${not empty showParentUnits}">
<form:form commandName="parentUnit" method="post" action="parentUnits" class="uk-form">
<!-- Zuordnung übergeordneter Einheiten -->
<table class="uk-table uk-table-condensed uk-text-small">
<thead>
<tr>
<th colspan="6"><spring:message code="units.parentsAssignment"/></th>
</tr>
</thead>
<tbody>
<tr>
<td class="uk-text-left"> <spring:message code="units.parent"/></td>
<td class="uk-text-center"><spring:message code="units.share"/></td>
<td class="uk-text-center"><spring:message code="units.usb"/></td>
<td class="uk-text-center"><spring:message code="units.fullLiability"/></td>
<td class="uk-text-center"><spring:message code="units.consolidation"/></td>
<td class="uk-text-center"></td>
<tr>
<tr>
<td>
<div class="uk-form-row">
<form:select path="parentUnitId" items="${unitList}" class="uk-form-width-medium" />
</div>
</td>
<td class="uk-text-center"><input type="text" class="uk-form-width-mini"></td>
<td class="uk-text-center"><input type="text" class="uk-form-width-mini"></td>
<td class="uk-text-center"><input type="checkbox"></td>
<td class="uk-text-center"><input type="checkbox"></td>
<td class="uk-text-center"><button class="uk-button" type="submit"><i class="uk-icon-plus"></i></button></td>
</tr>
</tbody>
</table>
</form:form>
</c:if>
</div>
</body>
<script>
function togglePieLines() {
if ($('#pie').prop('checked')) {
$('#pieReason').fadeIn(400);
$('#pieSince').fadeIn(400);
}
else {
$('#pieReason').fadeOut(400);
$('#pieSince').fadeOut(400);
}
}
</script>
</html>
Controller:
package de.amana.marc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import de.amana.marc.model.ParentUnit;
import de.amana.marc.model.Unit;
import de.amana.marc.service.ParentUnitService;
import de.amana.marc.service.UnitService;
#Controller
public class UnitsController
{
private UnitService unitService;
#Autowired(required=true)
#Qualifier(value="unitService")
public void setUnitService(UnitService us){
this.unitService = us;
}
private ParentUnitService parentUnitService;
#Autowired(required=true)
#Qualifier(value="parentUnitService")
public void setParentUnitService(ParentUnitService pus){
this.parentUnitService = pus;
}
#RequestMapping(value = "/units", method = RequestMethod.GET)
public String unitForm(Model model, HttpServletRequest request, HttpServletResponse response) {
model = this.unitService.fillFormSelectBoxes(model);
// Formobjekte
model.addAttribute("unit", new Unit());
model.addAttribute("parentUnit", new ParentUnit());
return "units";
}
#RequestMapping(value="/units", method =RequestMethod.POST)
public String unitFormSubmit(#Valid #ModelAttribute Unit unit, BindingResult result, Model model) {
model = this.unitService.fillFormSelectBoxes(model);
//Validierung
if (result.hasErrors()) {
return "units";
}
else {
// Speichern in DB
this.unitService.addUnit(unit);
// Zuordnung übergeordneter Gesellschaften einblenden
model.addAttribute("showParentUnits", true);
// Form neu befüllen
model.addAttribute("unit", unit);
model.addAttribute("unitList", this.unitService.listUnits());
ParentUnit newPu = new ParentUnit();
newPu.setUnitId(unit.getUnitId());
model.addAttribute("parentUnit", newPu);
return "units";
}
}
#RequestMapping(value="/parentUnits", method =RequestMethod.POST)
public String parentUnitFormSubmit(#Valid #ModelAttribute ParentUnit parentUnit, BindingResult result, Model model) {
// Formobjekt bearbeiten vor Speichern
ParentUnit newPu = new ParentUnit();
model = this.unitService.fillFormSelectBoxes(model);
return "units";
}
}
Full Stacktrace:
Sometimes the Stacktrace changes to:
This is kinda wierd, because i should be in submit and i have no clue why the first form has the problem
Could you try to use attribute "modelAttribute" on your first form like this and use it in your controller Parameter like the same name:
<form:form commandName="unit" method="post" action="units" class="uk-form" id="unitForm" modelAttribute="unit">
and your second form also the same name in the Controller Parameter:
<form:form commandName="parentUnit" method="post" action="parentUnits" class="uk-form" modelAttribute="parentUnit">
Be sure to add the same name as added attribute in your model to access in your success .jsp

Setting HTML content to JTextPane messes up System.out redirection

I am using the MessageConsole class to redirect System.out and System.err to a JTextPane named jMessageConsoleTextPane.
I am configuring it in my constructor like this:
jMessageConsoleTextPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
MessageConsole mc = new MessageConsole(jMessageConsoleTextPane, true);
mc.redirectOut(null, null);
mc.redirectErr(Color.RED, null);
mc.setMessageLines(100);
and also in NetBeans I have it set to contentType text/html.
It prints everything fine with System.out and System.err but here comes my problem. I try to set the text of jMessageConsoleTextPane to an html table (which contains two other html tables) that I have created based on some variables, so I do:
String htmlTable = WordCounting.getHtmlTable(wordsArrays, hashtagsArrays);
jMessageConsoleTextPane.setText(htmlTable);
Here is how the htmlTable String can look like (taken straight from the debugger):
<html>
<body>
<table>
<tr>
<td>
<table border="1">
<th colspan="3">Words used most</th>
<tr>
<td>1</td>
<td>day</td>
<td>3720</td>
</tr>
<tr>
<td>2</td>
<td>good</td>
<td>3354</td>
</tr>
<tr>
<td>3</td>
<td>love</td>
<td>2689</td>
</tr>
<tr>
<td>4</td>
<td>time</td>
<td>2372</td>
</tr>
<tr>
<td>5</td>
<td>got</td>
<td>1897</td>
</tr>
<tr>
<td>6</td>
<td>lot</td>
<td>1831</td>
</tr>
<tr>
<td>7</td>
<td>know</td>
<td>1801</td>
</tr>
<tr>
<td>8</td>
<td>photo</td>
<td>1772</td>
</tr>
<tr>
<td>9</td>
<td>girl</td>
<td>1755</td>
</tr>
<tr>
<td>10</td>
<td>life</td>
<td>1754</td>
</tr>
</table>
</td>
<td>
<table border="1">
<th colspan="3">Hashtags used most</th>
<tr>
<td>1</td>
<td>win</td>
<td>136</td>
</tr>
<tr>
<td>2</td>
<td>panjaforpunjab</td>
<td>105</td>
</tr>
<tr>
<td>3</td>
<td>aaronto600k</td>
<td>100</td>
</tr>
<tr>
<td>4</td>
<td>rt</td>
<td>89</td>
</tr>
<tr>
<td>5</td>
<td>giveaway</td>
<td>85</td>
</tr>
<tr>
<td>6</td>
<td>cfc</td>
<td>70</td>
</tr>
<tr>
<td>7</td>
<td>whybeinarelationshipwhen</td>
<td>65</td>
</tr>
<tr>
<td>8</td>
<td>retweet</td>
<td>64</td>
</tr>
<tr>
<td>9</td>
<td>gameinsight</td>
<td>64</td>
</tr>
<tr>
<td>10</td>
<td>rhoareunion</td>
<td>57</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Here is how it looks in the jMessageConsoleTextPane:
Then I try to use System.out which should print bellow text:
[23:19:32] Getting driver...
[23:19:32] Connecting to database...
[23:19:32] Executing query...
5.6.16
and this happens:
Basically everything is getting added on the last cell of the second table. Even if I do jMessageConsoleTextPane.setText("") there is a sole table cell remaining like this (top left):
So what is going on? Why is there a cell left and how do I fix it?
The MessageConsole class was not designed to be using with HTML. All the class does is add the text to the end of the Document, which in your case appears to be the last cell of the table.
You can try adding an empty HTML tag to the Document when you first create the HTML. Then maybe the text will be added to this tag instead of the table cell.
Maybe something like:
</table>
<p></p>
</body>
This all depends on how the Document parses the text that is inserted into the Document.

Web Flow problems from GET Jsp to a POST JSP (Form) - Spring MVC Annotated

i have a JSP with hyperlink
<table>
<tr>
<td>Product Name : </td>
<td>${product.name}</td>
</tr>
<tr>
<td>Description:</td>
<td>${product.description}</td>
</tr>
<tr>
<td>Price:</td>
<td>${product.price}</td>
</tr>
<tr><td> </td></tr>
<tr>
<td>
Add to shopping basket
</td>
</tr>
<tr><td> </td></tr>
<tr><td> </td></tr>
<tr>
<td>
<table>
<tr>
<td>Return to Home Page</td>
<td> </td>
<td>Logout
(<security:authentication property="principal.username" />)
</td>
</tr>
</table>
</td>
</tr>
And the controller
#Controller
#SessionAttributes("basket")
public class ShopBasketController {
private BasketManager basketManager;
private CustomerManager customerManager;
private CategoryManager categoryManager;
#Autowired
public ShopBasketController(BasketManager basketManager, CustomerManager customerManager, CategoryManager categoryManager) {
this.basketManager = basketManager;
this.customerManager = customerManager;
this.categoryManager = categoryManager;
}
#RequestMapping(value="/basketItems", method=RequestMethod.POST)
public String removeProduct(#ModelAttribute("basket") Basket basket, BindingResult bindingResult, Model model) {
Basket newBasket = ShoppingBasketUtils.removeFromBasket(basket, basketManager);
basketManager.update(newBasket);
model.addAttribute("basket",newBasket);
model.addAttribute("customer", "Sonx"+" Nkuks");
model.addAttribute("totalItems", basketManager.getTotalNumberOfItems(basket));
model.addAttribute("totalPrice", ShoppingBasketUtils.currencyFormat(basketManager.getTotalProductPrice(basket)));
return "basketItems";
}
#RequestMapping("/populateBasket")
public String populateBasket(#RequestParam("code") String productCode, #RequestParam("name") String categoryName, Model model) {
Customer customer = customerManager.getCustomer("Sonx", "Nkuks");
if(customer != null) {
Basket shopBasket = ShoppingBasketUtils.addToBasket(productCode, categoryManager.getCategory(categoryName),
basketManager.getBasket(customer.getReferenceNumber()), basketManager);
basketManager.update(shopBasket);
model.addAttribute("basket",shopBasket);
model.addAttribute("customer", customer.getFirstName()+" "+customer.getLastName());
model.addAttribute("totalItems", basketManager.getTotalNumberOfItems(shopBasket));
model.addAttribute("totalPrice", ShoppingBasketUtils.currencyFormat(basketManager.getTotalProductPrice(shopBasket)));
return "basketItems";
}
model.addAttribute("customer", "test".concat(" test"));
return "/error";
}
}
Then the form ...
<form:form commandName="basket">
<table>
<tr>
<td>
<table>
<tr>
<td>Customer Name : </td>
<td>${customer}</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="600" border="1" cellspacing="0" cellpadding="2"
border="0">
<thead>
<tr>
<td>Products:</td>
</tr>
<tr>
<td>Product Name</td>
<td>Product Code</td>
<td>Description</td>
<td>Price</td>
<td>Remove</td>
</tr>
</thead>
<tbody>
<c:forEach items="${basket.products}" var="product">
<tr>
<td>${product.name}</td>
<td>${product.productCode}</td>
<td>${product.description}</td>
<td>${product.price}</td>
<td><form:checkbox path="removeItemCodes" value="${product.productCode}" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>Total Price</td>
<td> </td>
<td>${totalPrice}</td>
</tr>
<tr>
<td>Total Items</td>
<td> </td>
<td>${totalItems}</td>
</tr>
</table>
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td><input type="submit" value="Remove Items" /></td>
</tr>
<tr><td> </td></tr>
<tr><td> </td></tr>
<tr>
<td>
<table>
<tr>
<td>Return to Home Page</td>
<td> </td>
<td>Logout
(<security:authentication property="principal.username" />)
</td>
</tr>
</table>
</td>
</tr>
</table>
When i press the link from the first JSP "" the controller succesfully execute the method populateBasket and loads the Form. But when i submit the form, i want it to call the POST method (basketItems)... But it doesn't, pressng the submit button always executes the GET method (populateBasket) .. This doesn't happen if i load the form directly from the index page, it loads successfully . Problem is when coming from that JSP ?
If you want the form to submit to a different URL from the one that was used to retrieve the page, you need to explicitly set the action on it. Otherwise Spring is going to just fill it in with the current URL.

Categories