I have Simple Spring form with employee personal info and Contact Info..I have 2 bean EmpPersonalInfo and EmpContactInfo,How do i bind 2 object with "command' and show in empform.
i got this error like Invalid property 'name' of bean class [java.util.HashMap]: Bean property 'name' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
empform
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form:form action="saveemp" method="post">
<table>
<tr> <td> Name</td>
<td> <form:input path="name"/></td></tr>
<tr> <td>Id </td>
<td><form:input path="id"/> </td></tr>
<tr><td> Current Address</td>
<td><form:input path="empcontactinfo.currentAddress"/> </td> </tr>
</table>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
Empcontroll.class
#RequestMapping("/empform")
public ModelAndView showForm()
{
Map<String,Object> modelmap=new HashMap<String,Object>();
modelmap.put("personalinfo",new EmpPersonalInfo() );
modelmap.put("contactinfo", new EmpContactInfo());
return new ModelAndView("empform","command",modelmap);
}
Path attribute puts value of input field into java properties using java beans convention to be used as #ModelAttribute in your controller method.
#RequestMapping(...)
public String saveEmp(#ModelAttribute("employee") EmpPersonalInfo employee) {
// ...
}
if you just want to display the values inside these two objects in that map, you could use,
<c:out value="${command['contactinfo'].contact}"/>
Related
I am trying to create a sample registration page with Spring MVC and JSP pages.
While opening the url on tomcat server, I am getting following error
root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'register' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(
I have a JSP register.jsp
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration</title>
</head>
<body>
<form:form action="/register/process" method="POST" modelAttribute="register">
<table style="text-align: center;">
<tr>
<td><form:label path="fname">First Name</form:label></td>
<td><form:input path="fname" name="fname"
id="fname" /></td>
</tr>
<tr>
<td><form:label path="lname">Last Name</form:label></td>
<td><form:input path="lname" name="lname" id="lname" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="CREATE AN ACCOUNT"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
I have a controller class UserController.java
package vnfhub.supplier.controller;
#Controller
public class UserController {
#RequestMapping(value = "/register", method = RequestMethod.GET)
public String getRegisterForm(Model model) {
model.addAttribute("register", new Register());
return "register";
}
#RequestMapping(value = "/register/process", method = RequestMethod.POST)
public String processRegistration(#ModelAttribute("register") Register register, BindingResult result) {
return "success";
}
}
and a success.jsp page
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success Form</title>
</head>
<body>
<font color="green"><h1>Hello</h1></font>
<h1>You have successfully registered</h1>
<font color="green"><h1>Welcome to Spring world !</h1></font>
</body>
</html>
I have tried many solution on stackoverflow.... but none of them worked.
I find your code okay so far as you given here. I mimic the situation with your code but unfortuantely found No Exception.
Things that you might have doing wrong is you are running some old build code in your tomcat. try to clean build and re-deploy in your container.
NB: one friendly suggestion. You are doing one thing wrong that is having action of your form to /register/process that will send the request to the container root (e.g. localhost:8080/register/process). And you will get 404 for that. You are not probably want that. register/process should be your URL and this will POST the request relative to your application-context. If your application context is something localhost:8080/test, this will send the request to localhost:8080/test/register/process
I'm creating a small shopping cart project where I get a list of products in a linked list from a servlet. Now, I am printing the values of the linkedlist in a table with an option for users to select the quantities they want.
Which ever has quantity selected should go on to the next page as an attribute
Here are few issues I am facing:
1: Since the list has more than 1 items, each item listed should have
input field. How to I dynamically change the input name for the
quantity which I can later use as an attribute.
2: The available quantity of items vary from product to product, how
to I set the max value of the quantity to the available stock.
3: If I can manage to get the values, how do I set all the attributes.
Is it in the for loop or outside the for loop?
Here is the code for the reference.Picture of what the page looks like
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%-- <%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %> --%>
<%#page import="shoppingcart.model.items.*,java.util.*" %>
<%List<ItemDetailsPojo> listp = (List<ItemDetailsPojo>) session.getAttribute("ItemsData");
%>
<%-- <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
<Center>
<h3 style="color: blue">Welcome To the World of Shopping</h3>
</Center>
<div align='right'>Logged in as:</div>
<div align="center">
<table border="10" cellpadding="5">
<caption>
<h2>List of Items</h2>
</caption>
<tr>
<th>ItemId</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Available</th>
<th>Quantity</th>
</tr>
<%for(int i=0;i<listp.size();i++){%>
<tr>
<td><%out.println(listp.get(i).getItemId());%></td>
<td><%out.println(listp.get(i).getItemName());%></td>
<td><%out.println(listp.get(i).getCategory());%></td>
<td><%out.println(listp.get(i).getPrice());%></td>
<td><%out.println(listp.get(i).getQuantity());%></td>
<% int number = listp.get(i).getQuantity(); %>
<td><input type="number" name="should dynamically change according to the size of the list" min="0" max="should change according the the quantity available"></td>
</tr>
<%} %>
</table>
</div>
</body>
</html>
Try this:
<td><input type="number" name="quantity<%=i%>" min="0" max="<%=listp.get(i).getQuantity()%>"></td>
and you need to set all the attributes outside the loop.
Edit your code like this
<%List<ItemDetailsPojo> listp = (List<ItemDetailsPojo>) session.getAttribute("ItemsData");
%>
<%-- <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
<Center>
<h3 style="color: blue">Welcome To the World of Shopping</h3>
</Center>
<div align='right'>Logged in as:</div>
<div align="center">
<table border="10" cellpadding="5">
<caption>
<h2>List of Items</h2>
</caption>
<tr>
<th>ItemId</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Available</th>
<th>Quantity</th>
</tr>
<%
String name;
for(int i=0;i<listp.size();i++){%>
<tr>
<td><%out.println(listp.get(i).getItemId());%></td>
<td><%out.println(listp.get(i).getItemName());%></td>
<td><%out.println(listp.get(i).getCategory());%></td>
<td><%out.println(listp.get(i).getPrice());%></td>
<td><%out.println(listp.get(i).getQuantity());%></td>
<% int number = listp.get(i).getQuantity();
name="quantity".concat(listp.get(i).getQuantity());
%>
<td><input type="number"name="<%=name%>" min="0" max="should change according the the quantity available"></td>
</tr>
<%} %>
</table>
</div>
</body>
</html>
This question already has an answer here:
How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
(1 answer)
Closed 6 years ago.
I want to perform multiple operation Like DELETE and UPDATE to do so I Need to send Data to Controller,
where I Am doing Mistake??
Following is my JSP page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:useBean id="TimeDetailBean" class="com.logic.bean.userBean" scope="application" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Manage Results</title>
</head>
<body>
<center>
<h1>The Employee_Info Results </h1>
<table>
<tr><th>Name</th><th>Last name</th><th>Password</th></tr>
<c:forEach items="${rows}" var="row">
<tr>
<td><input type="text" name="name" value=${row.NAME}></td>
<td><input type="text" name="lastname" value=${row.LASTNAME}></td>
<td><input type="text" name="password" value=${row.PASSWORD}></td>
<td>UPDATE</td>
<td>DELETE</td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
and below is the controller where I want The value of name ,lastname and password
#RequestMapping("/delete")
public ModelAndView Delete(HttpServletRequest request, HttpServletResponse response)
{
System.out.println("Delete Controller Executed");
userBean ub= new userBean();
Dao d= new Dao();
String name=request.getParameter("name");
String lastname=request.getParameter("lastname");
System.out.println("Name catch"+name);
System.out.println("Lastname catch"+lastname);
return new ModelAndView("deleteSuccess");
}//delete ends
Thanks in advance. .
In order to send the Data from JSP to Controller
Create a form with action (/delete) and method=POST
Create a Controller with the #RequestMapping("/delete") that point to form action
Use Request.getParameter("name") in Controller.
Now on submit button in the JSP Spring Servlet Dispatcher will the mapping form in the Controller class and send the data to the controller from JSP.
Let us know if more inforamtion needed I will share the sample example
Regards,
Pavan
I have the following page (Spring NVC) which works fine:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Replace Cards</title>
</head>
<body>
<h3>Replace Cards</h3>
<br/><br/>
Give the numbers of cards in your hand you want replace and hit the button<br/>
Valid numbers: 1 to 5, numbers must be separated by space<br/>
#NEW CODE#
<br/>
<sf:form action="execReplaceCards" commandName="cards">
<table>
<tr>
<td><sf:input path="playerName" type="text"/></td>
<td><sf:input path="numbers" type="text"/></td>
<td><input value="Replace" type="submit"/></td>
</tr>
</table>
</sf:form>
</body>
</html>
until I add the following piece of code before the form tag
(marked above as #NEW CODE#):
Here is your hand:
<br/><br/>
<c:out value="Hand name: "/>
<c:out value="${hand.name}"/><br/>
<c:forEach var="card" items="${hand.cards.card}">
<c:out value="Card:[${card.suit} : ${card.rank}]"/><br/>
</c:forEach>
<c:out value="${hand.value}"/>
<br/>
This snippet doesn't do anything except displaying the players hand for reference but since I add this part I've got request syntactically incorrect error. Request never reaches the controller. What's wrong, why is it happening?
I have a problem with JSP Bean's scope - Request. I have a page Index.jsp with jsp bean 'message', its scope is Request and a page result.jsp. When I send request to result.jsp from Index.jsp. My bean 'message' should keep its value but it doesn't now.
I tried with scope Session and my bean worked well. I search all questions about this problem but no answer can meet my question.
Here is my code:
file Index.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
String name = request.getParameter("name") == null ? "" :
request.getParameter("name");
int age = ( request.getParameter("age") == null ||
request.getParameter("age") == "") ? 0 :
Integer.parseInt(request.getParameter("age"));
%>
<h1>Nice to meet you</h1>
<form method="post" action="View/result.jsp">
<jsp:useBean id="message" class="com.java.Message" scope="request"/>
<jsp:setProperty name="message" property="message" value="Hello world!"/>
<label>Name: </label> <br>
<input type="text" name="name" placeholder = "Phan Dinh The"/> <br>
<label>Age: </label> <br>
<input type="number" name="age" placeholder = "25"/> <br>
<input type="checkbox" name="title"/> Senior <br>
<input type="radio" name="language" value="c#"/> C# <br>
<input type="radio" name="language" value="java"/> Java <br>
<br><br>
<jsp:include page="View/date.jsp" flush="true"/>
<input type="submit" value="submit"/>
<br><br>
</form>
<br><br>
</body>
</html>
file result.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.java.Message"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="message" class="com.java.Message" scope="request"/>
<jsp:getProperty name="message" property="message"/>
</body>
</html>
my class Message
package com.java;
public class Message {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String content) {
this.message = content;
}
}
I use Tomcat 8.0.23, Jsp version 2.3, Servlet API 3.1
When you use
<jsp:setProperty name="message" property="message" value="Hello world!"/>
in the index.jsp file, that property is scoped to the request of the index.jsp page. once the index jsp page returns to the client, that request is done. When you submit the form, a new request is created, and that is used for the result page generation. Thus when you are in the result.jsp code, there is no request scoped parameter named 'message'.
You could always put the message in an
<input type="hidden" name="message">Hello World</input>
field of the form, and retrieve it in the results.jsp that way.