Cannot find any information on property in Java JSP page - java

In my Jsp page, I am getting the error :
Cannot find any information on property 'productList' in a bean of
type 'Smithd81.InventoryManager'
The InventoryManager class has a getProductList() method which returns a List of Product objects, which I need to access.
In my JSP:
<jsp:useBean id = "productManager" scope = "page" class = "Smithd81.InventoryManager" />
<jsp:getProperty name = "productManager" property = "productList" />
I thought I had this correct on the getProperty property name- starts in lower case and whatnot, which is the typical pitfall for this error, but I definitely have it spelled correctly.
Where I seem to get the error:
<c:forEach var="p" items="${productManager.productList}">
<div>
<form action="inventory" method="POST">
<label>
<span>UPC</span>
<input type="text" name="upc" value="${p.getUpc()}" readonly="readonly"/>
</label>
<label>
<span>Short Details</span>
<input type="text" name="shortDetails" value="${p.getShortDetails()}" />
</label>
<label>
<span>Long Details</span>
<input type="text" name="longDetails" value="${p.getLongDetails()}" />
</label>
<label>
<span>Price</span>
<input type="text" name="price" value="${p.getPrice()}" />
</label>
<label>
<span>Stock</span>
<input type="text" name="stock" value="${p.getStock()}" />
</label>
<input type="submit" name="button" value="Edit" />
<input type="submit" name="button" value="Delete" />
</form>
</div>
</c:forEach>
For clarification, Inside the InventoryManager Class, the method signature reads:
public static List getProductList() throws IOException, ClassNotFoundException {
try {
List<Product> productsList = new ArrayList<>(); //empty product list
Collection<Product> productsFromFile = CollectionFileStorageUtility.load(Product.class);//loads collection from file
productsList.addAll(productsFromFile);// adds all current products from file to the productList List.
return productsList;
} catch (IOException e) {
System.out.println("IOException: error accessing data file.");
return null;
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException: error accessing class.");
return null;
}
}

Refactor your package name to lowercase. Another case is that you are trying to call static method on object (your bean finally is object). So method getProductList() should be non-static.

Related

Why my form data are not intercepted in my mainController in Java Intellij

I get an Internal Server Error, status=500: java.lang.NumberFormatException
The reason seems to be that my form data, sent via name attribute is not intercepted by #RequestMapping or HttpServletRequest doesn't work..
Here I try to pass the data:
<form action="/tankbeurt" method="get" style="padding: 5px;">
<div style="padding: 10px;">
<label for="huidig">Huidige kilometerstand</label>
<input type="text" name="huidig" id="huidig"></div>
<div style="padding: 10px;">
<label for="vorig">Vorige kilometerstand</label>
<input type="text" name="vorig" id="vorig"></div>
<div style="padding: 10px;">
<label for="liter">Hoeveel liter heb je bijgetankt</label>
<input type="text" name="liter" id="liter"></div>
<div><input type="submit" value="Bereken Verbruik"></div>
And I try to intercept it in the mainController:
#Controller
public class MainController {
#RequestMapping("/tankbeurt")
public String gegevens(HttpServletRequest request, Model model){
int huidigeKm = Integer.parseInt("huidig");
System.out.println(huidigeKm);
int vorigeKm= Integer.parseInt("vorig");
double liter= Double.parseDouble("liter");
Tankbeurt tankinformatie = new Tankbeurt(vorigeKm,huidigeKm,liter);
model.addAttribute("informatieTanken",tankinformatie);
return "tankbeurt";
}
Thankyou for your help. I don't see the problem.
Integer.parseInt("huidig");
You are trying to parse string "huidig" to integer. That's the cause of exception. Try looking for some method that extracts property from the model, like "model.getAttribute("huidig")".

AMP(Accelerated Mobile Page) form development in AEM 6.1

I am trying to develop a AMP form in AEM 6.1 and trying to submit data to back end sling servlet to save the data into database.But I am getting error in return:
<body>
<form class="sample-form"
method="post"
action-xhr="/bin/rating.rest"
target="_top">
<input type="text"
name="name"
placeholder="Name..."
required>
<input type="email"
name="email"
placeholder="Email..."
required>
<input type="text"
name="title"
placeholder="Title..."
required>
<input type="textarea"
name="textarea"
placeholder="Feedback..."
required>
<fieldset class="rating">
<input name="rating" type="radio" id="rating5" value="5" on="change:rating.submit">
<label for="rating5" title="5 stars">☆</label>
<input name="rating" type="radio" id="rating4" value="4" on="change:rating.submit">
<label for="rating4" title="4 stars">☆</label>
<input name="rating" type="radio" id="rating3" value="3" on="change:rating.submit" checked="checked">
<label for="rating3" title="3 stars">☆</label>
<input name="rating" type="radio" id="rating2" value="2" on="change:rating.submit" >
<label for="rating2" title="2 stars">☆</label>
<input name="rating" type="radio" id="rating1" value="1" on="change:rating.submit">
<label for="rating1" title="1 stars">☆</label>
</fieldset>
<input type="submit"
value="Submit Feedback">
<div submit-success>
<template type="amp-mustache">
Success! Thanks {{name}} for trying the <code>amp-form</code> demo! Try to insert the word "error" as a name input in the form to see how <code>amp-form</code> handles errors.
</template>
</div>
<div submit-error>
<template type="amp-mustache">
Error! Thanks {{name}} for trying the <code>amp-form</code> demo with an error response.
</template>
</div>
</form>
</body>
Back End Code::::
#SlingServlet(paths="/bin/rating.rest", methods = "POST", metatype=true)
public class AmpRatingActionServlet extends SlingAllMethodsServlet {
protected void doPost(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,
IOException {
String jsonData = null;
String name;
String email;
String title;
String textarea;
String rating;
try
{
name = request.getParameter("name");
email = request.getParameter("email");
title = request.getParameter("title");
textarea = request.getParameter("textarea");
rating = request.getParameter("rating");
log.info(CLASSNAME+"::Inside Try Block:::::::name::::"+name+":::::email:::::"+email+":::::title:::::"+title+":::::textarea:::::"+textarea+":::::rating:::::"+rating);
}
}catch(Exception e) {
log.error(Error occurred in Servlet", e);
}
}
500
Message
javax.jcr.nodetype.ConstraintViolationException: No matching property definition: name = Test

How to pass multiple value as same name using HttpServletRequest(Spring Freamework)?

I have two input field in view
<input name="test[]" type="text" value="one">
<input name="test[]" type="text" value="two">
in controller
public String store(#Context HttpServletRequest request) {
// now i get only last value
String[] array = request.getPerameter("test");
}
in PHP i can value as array using same way.
you can use request.getParameterValues(test)
Please check oracle doc for more info and here is an example
<form name="myForm" th:action="#{/demo}" method="post"
class="stdform" id="triggerform">
<div class="form-group">
<label for="usr">test:</label>
<input name="test" type="text" class="form-control" />
<input name="test" type="text" class="form-control" />
<input name="test" type="text" class="form-control" />
<input name="test" type="text" class="form-control" />
<input name="test" type="text" class="form-control" />
<input name="test" type="text" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">Get Started</button>
</form>
#RequestMapping(value = "/demo", method = RequestMethod.POST)
public void getInboxMessage(HttpServletRequest request,Model model, #RequestParam("test") List<String> test) {
try {
logger.info("Welcome to getInboxMessage Method inside the MessageController ");
System.out.println(test);
}
catch(Exception e)
{
logger.info(e.getMessage());
}
}
This would be spring way of doing it .hope it helps

How to pass variable number of parameters from jsp page to the java controller

I have a jsp page and a controller with fillowing functionality:
java controller code:
#Controller
public class AddNewItemController {
#RequestMapping(value = "/newItem/submit", method = RequestMethod.POST)
public String getDataForInterval4(#RequestParam("itemId") String itemId, #RequestParam("product1SkusCnt") String product1SkusCnt, #RequestParam("itemName") String itemName, HttpServletRequest request) {
return "ItemSubmitted";
}
}
my main jsp file that submits to controller:
<center>
<table>
<tr>
<td>How many items do you have?</td>
<td> <input type="number" name="productsCnt" id="productsCnt" size="2" min="1" value="1" onchange="productCount()"/> </td>
</tr>
</table>
</center>
<form action="/newItem/submit" method="post">
<br /><br />
<div id="outerDivContainer">
<div id="product1Div" name="product1Div" >
<hr /> Product 1 Name: <input id="product1Name" /> Product 1 ID: <input id="product1ID" /> How many SKUs of Product 1? <input id="product1SkusCnt" type="number" size="2" min="1" value="1" onchange="skusCount(1)"/> <br /><br />
<div id="skusContainer1">
<div id="sku1Div">
SKU 1 Name: <input id="sku1"/>
</div> <br />
</div>
</div>
</div>
<hr />
<input type="submit" value="Submit" />
</form>
<script>
function productCount() {
console.log("onchange product");
document.getElementById('outerDivContainer').innerHTML = "";
var cnt = document.getElementById('productsCnt').value;
console.log("cnt="+cnt);
for (i=0;i<cnt;i++){
var newEl = document.createElement('div');
newEl.class='prodRow';
j=i+1;
newEl.innerHTML = '<div id="product'+j+'Div"><hr /> Product '+j+' Name: <input id="product'+j+'Name" /> Product '+j+' ID: <input id="product'+j+'ID" /> How many SKUs of Product '+j+'? <input id="product'
+j+'SkusCnt" type="number" size="2" min="1" value="1" onchange="skusCount('+
j+')" /> <br /><br /> <div id="skusContainer'+j+'"><div id="sku1Div"> SKU 1 Name: <input id="sku1"/></div> <br /> </div></div>';
document.getElementById('outerDivContainer').appendChild(newEl);
}
}
function skusCount(productId){
console.log("onchange skus, product id= "+productId+";");
var skusCnt = document.getElementById('product'+productId+'SkusCnt').value;
console.log("skusCnt="+skusCnt);
document.getElementById('skusContainer'+productId).innerHTML = "";
for (i=0;i<skusCnt;i++){
var newEl = document.createElement('div');
newEl.class='skuRow';
j=i+1;
newEl.innerHTML = '<div id="sku'+j+'Div">SKU '+j+' Name: <input id="sku'+j+'" /> </div> <br />';
document.getElementById('skusContainer'+productId).appendChild(newEl);
}
}
</script>
ItemSubmitted.jsp is just a jsp file that confirms successful item submission.
The problem is I don't know how many items will be passed to the controller, or how many skus each item might have.
What would be a suggested approach to this problem? Thanks in advance!
The answer to my question is much simpler than I expected. All I have to do is just know how many parameters I will pass and their names. I don't have to specify it in the controller as #RequestParam("itemId") String itemId, #RequestParam("product1SkusCnt") etc.
Instead I should only pass a request as an argument:
#RequestMapping(value = "/newItem/submit", method = RequestMethod.POST)
public String getDataForInterval4(HttpServletRequest request) { return "ItemSubmitted"}
I can call each of those passed parameters in the controller by doing the following:
request.getParameter("product"+i+"SkusCnt"))
Just need to make sure that I pass the total count of products also.
Hope this helps someone.

How to redirect to servlet when we have multiple buttons

I have 2 textboxes and 3 buttons and each button has specific action i.e. insert, update, delete.
But how to redirect it when specific button click ?
because i have used it in one form tag.
so what will it do when any button click ?
how it will get the action name ?
code :
<form action="Doaction" method="post">
First Name : <input type="text" id="fname" name="fname">
<br>
Last Name : <input type="text" id="lname" name="lname"><br>
<input type="button" value="Insert">
<input type="button" value="Update">
<input type="button" value="Delete">
</form>
any help please ?
in form : give name to buttons
First Name : <input type="text" id="fname" name="fname">
<br>
Last Name : <input type="text" id="lname" name="lname"><br>
<input type="button" value="Insert" name="button">
<input type="button" value="Update" name="button">
<input type="button" value="Delete" name="button">
</form>
String button_param = request.getParameter("button");
RequestDispatcher rd = null;
if(button_param .equals("Insert")
{
rd=request.getRequestDispatcher("InsertServlet");
}
else if(button_param .equals("Update"))
{
rd=request.getRequestDispatcher("UpdateServlet");
}
else if(button_param .equals("Delete"))
{
rd=request.getRequestDispatcher("DeleteServlet");
}
rd.forward(request, response);
you can create a common javascript function like submitForm('pass your action').
Here you can see that function has a parameter. you have to pass action based your requirement
for example :
<input type="button" value="Insert" onclick="submitForm('InsertURL');">
<input type="button" value="Update" onclick="submitForm('UpdateURL');">
Action function body like :
function submitForm(action){
//send Ajax Request to server...using action
}
Yes I have solved it.
I have given a common name to each button and in servlet i have first checked that from which button action call then as per it i have used if condition and inside it's block i have make a related code.
code :
<form action="Doreg" method="post">
First Name : <input type="text" id="fname" name="fname">
<br>
Last Name : <input type="text" id="lname" name="lname"><br>
<input type="submit" value="Insert" name="kb">
<input type="submit" value="Update" name="kb">
<input type="submit" value="Delete" name="kb">
</form>
java code :
String fname =request.getParameter("fname");
String lname =request.getParameter("lname");
String f = request.getParameter("kb");
if (f.equals("Insert"))
{
obj.connect();
String query ="insert into user(firstname,lastname) values('"+fname+"','"+lname+"')";
obj.passquery(query);
}
else if(f.equals("Delete"))
{
obj.connect();
String query ="delete user where firstname='"+fname+"' OR lastname='"+lname+"'";
obj.passquery(query);
}

Categories