display SQL rows in table - spring

How can I display all rows from SQL database in JSP table?
In order to one row represent one 'td' tag in table. This code below displays only last row from my database.
CartController.java:
#RequestMapping(value="/cart.html", method = RequestMethod.GET)
public ModelAndView cartPage(Model model) throws SQLException, ClassNotFoundException {
PreparedStatement pst;
ResultSet rs;
Connection con;
con = db.getConnect();
pst = con.prepareStatement("SELECT * FROM onlineshop.cart");
rs = pst.executeQuery();
while (rs.next()) {
model.addAttribute("ID", rs.getString(1));
model.addAttribute("picture", rs.getString(2));
model.addAttribute("name", rs.getString(3));
model.addAttribute("company", rs.getString(4));
model.addAttribute("type", rs.getString(5));
model.addAttribute("price", rs.getString(6));
}
ModelAndView cart = new ModelAndView("Cart");
return cart;
}
and here is fragment of Cart.jsp:
<div style="padding-right: 40px">
<table border="1">
<tr>
<td>ID</td>
<td>Product</td>
<td>Name</td>
<td>Company</td>
<td>Type</td>
<td>Price</td>
<td>Action</td>
</tr>
<tr>
<td>${ID}</td>
<td><img src="${picture}"/></td>
<td>${name}</td>
<td>${company}</td>
<td>${type}</td>
<td>${price}</td>
<td></td>
</tr>
</table>
</div>
Error:
type Exception report
message An exception occurred processing JSP page /WEB-INF/Cart.jsp at line 74
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/Cart.jsp at line 74
71: </tr>
72: <c:forEach var="cart" items="carts">
73: <tr>
74: <td>${cart.getID()}</td>
75: <td><img src="${cart.getPicture()}"/></td>
76: <td>${cart.getName()}</td>
77: <td>${cart.getCompany()}</td>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.el.MethodNotFoundException: Method not found: class java.lang.String.getID()
javax.el.Util.findWrapper(Util.java:351)
javax.el.Util.findMethod(Util.java:213)
javax.el.BeanELResolver.invoke(BeanELResolver.java:156)
org.apache.jasper.el.JasperELResolver.invoke(JasperELResolver.java:147)
org.apache.el.parser.AstValue.getValue(AstValue.java:159)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943)
org.apache.jsp.WEB_002dINF.Cart_jsp._jspx_meth_c_005fforEach_005f0(Cart_jsp.java:237)
org.apache.jsp.WEB_002dINF.Cart_jsp._jspService(Cart_jsp.java:190)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.29 logs.

Because you're overriding cart information and obviously the last row wins the bet. You should maintain a List of Carts in your controller and push them to your view. Then in the view, iterate through them and put them in each row.
Suppose you have a Cart class like this:
public class Cart {
private String ID;
private String picture;
private String name;
private String company;
private String type;
private String price;
// getters and setters
}
Change your controller to create a List of Carts and add each Cart to the list:
...
List<Cart> carts = new ArrayList<>();
while (rs.next()) {
Cart cart = new Cart();
cart.setID(rs.getString(1));
cart.setPicture(rs.getString(2));
cart.setName(rs.getString(3));
cart.setCompany(rs.getString(4));
cart.setType(rs.getString(5));
cart.setPrice(rs.getString(6));
carts.add(cart);
}
model.addAttribute("carts", carts);
...
And in the view:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:forEach var="cart" items="carts">
<tr>
<td>${cart.getID()}</td>
<td><img src="${cart.getPicture()}"/></td>
<td>${cart.getName()}</td>
<td>${cart.getCompany()}</td>
<td>${cart.getType()}</td>
<td>${cart.getPrice()}</td>
<td></td>
</tr>
</c:forEach>

Related

Form validation return white label error page

I tried to do a form validation using spring boot 2.2 and thymeleaf like the documentation example (https://spring.io/guides/gs/validating-form-input/) but I have a white label error page when wrong value are submitted when I should have a binding of these errors in my form view. How to fix that ?
My controller action code:
#Controller
public class TodoController {
#RequestMapping(value = "/todo/create", method = RequestMethod.GET)
public String tplTodoCreate(Model model) {
TodoForm todoForm = new TodoForm();
return "v-todo-create";
}
#RequestMapping(value = "/todo/create", method = RequestMethod.POST)
public String tplTodoCreatePost(#ModelAttribute(name="formTodo") #Valid TodoForm todoForm, RedirectAttributes redirAttrs, BindingResult result) {
if(result.hasErrors()) {
return "v-todo-create";
}
todoRepository.save(todoForm);
redirAttrs.addFlashAttribute("msgNotices", "Todo task created successfuly.");
return "redirect:/todos";
}
}
and the view:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="tpl-default">
<head>
<title th:text="#{ctl-todo.create.meta.title}">Create New Todo</title>
<link th:href="#{/css/todo/create.css}" rel="stylesheet" />
</head>
<body>
<div id="wrapper" layout:fragment="content">
<h2 class="page-header" th:text="#{ctl-todo.create.00001strid}">Add Todo</h2>
<form name="todo" method="post" action="#" th:action="#{/todo/create}" th:object="${todoForm}" class="my-form-class">
<div id="form">
<div>
<label for="form_name" class="required" th:text="#{ctl-todo.create.00002strid}">Name</label>
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></p>
<input type="text" th:field="*{name}" id="form_name" name="name" min="1" max="30" required="required" class="form-control" style="margin-bottom:15px;" />
</div>
<div>
<button type="submit" id="form_save" name="save" class="btn btn-primary" style="margin-bottom:15px;" th:text="#{ctl-todo.create.00013strid}">Create Todo</button>
</div>
</div>
</form>
<hr />
<a class="btn btn-default" href="/todos"><span class="glyphicon glyphicon-floppy-remove"></span> <span th:text="#{ctl-todo.create.00014strid}">Back to todos list</span></a>
</div>
</body>
</html>
The stacktrace is:
2020-01-06 21:47:30.780 WARN 12140 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'formTodo' on field 'name': rejected value [a]; codes [Size.formTodo.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [formTodo.name,name]; arguments []; default message [name],30,2]; default message [size must be between 2 and 30]]
Whiteblabel error page is:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Jan 07 10:08:07 GMT+01:00 2020
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='formTodo'. Error count: 1
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'formTodo' on field 'name': rejected value [a]; codes [Size.formTodo.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [formTodo.name,name]; arguments []; default message [name],30,2]; default message [size must be between 2 and 30]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:164)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
Typicaly the name field have min attribute equals to 1 but the TodoForm entity have a Size constraint with min=2 and max=30. When I test to submit a value with only one character, the good error is detected but that return the error as a white label page rather than the view with the error.
EDIT: Probleme solved. It's caused by the order of parameter into the tplTodoCreatePost() method and the #ModelAttribute(name="formTodo"). The method signature was modifed as public String tplTodoCreatePost(#Valid TodoForm todoForm, BindingResult result, RedirectAttributes redirAttrs)
I would try to add a parameter to the model. But it's quite diffcult to troubleshoot without a stack trace. Could you please attach it to your post? Also the rest of the Controller, with GET method mapping could be helpful here.
EDIT:
I think the problem is the order of the parameters of the tplTodoCreatePost method. Please try to move BindingResult before Model, as below:
public String tplTodoCreatePost(#Valid TodoForm todoForm, BindingResult result, #ModelAttribute(name="formTodo"), RedirectAttributes redirAttrs, ) {
// method body
}
For information, I have remove the #ModelAttribute(name = "formTodo") because the error isn't displayed when I use it.
#RequestMapping(value = "/todo/create", method = RequestMethod.POST)
public String tplTodoCreatePost(#Valid TodoForm todoForm, BindingResult result, RedirectAttributes redirAttrs) {
if(result.hasErrors()) {
return "v-todo-create";
}
todoRepository.save(todoForm);
redirAttrs.addFlashAttribute("msgNotices", "Todo task created successfuly.");
return "redirect:/todos";
}

I have NullException on login

I'm trying to make a login, first time on mvc
I have this error on a proyect for university and I'm not able to see the error throw debug, I can't figure out why. When the debugger gets to line :
Empleado empleado = serviceEmpleado.correctLogin(loginFormDni, loginForm.getPassword());
on controller LoginController method checkLogin it just gives me the exception.
type Informe de Excepción
mensaje Request processing failed; nested exception is java.lang.NullPointerException
descripción El servidor encontró un error interno que hizo que no pudiera rellenar este requerimiento.
excepción
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
causa raíz
java.lang.NullPointerException
germanAcosta.electronicaDonPepe.controller.LoginController.checkLogin(LoginController.java:36)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
nota La traza completa de la causa de este error se encuentra en los archivos de diario de Apache Tomcat/8.0.36.
class LoginController
package germanAcosta.electronicaDonPepe.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import germanAcosta.electronicaDonPepe.DTO.Login;
import germanAcosta.electronicaDonPepe.dominio.Empleado;
import germanAcosta.electronicaDonPepe.service.ServicioException;
#Controller
//#SessionAttributes("resultado")
public class LoginController {
public ServiceEmpleado serviceEmpleado;
#RequestMapping("/login")
public String showLogin(Model model,
#ModelAttribute("resultado") String resultado, #ModelAttribute("error") String error) {
Login login = new Login();
model.addAttribute("login", login);
model.addAttribute("resultado", resultado);
model.addAttribute("error", error);
return "login";
}
#RequestMapping(value = "/login/check", method = RequestMethod.POST)
public String checkLogin(#ModelAttribute("login") Login loginForm, Model model, RedirectAttributes ra) {
try {
Integer loginFormDni = loginForm.getDni();
Empleado empleado = serviceEmpleado.correctLogin(loginFormDni, loginForm.getPassword());
model.addAttribute("loginForm", loginForm);
ra.addFlashAttribute("resultado", "El login es correcto bienvenido " + empleado.getNombre() + ".");
return "redirect:/login";
} catch (ServicioException e) {
ra.addFlashAttribute("resultado", e.getMessage());
return "redirect:/login";
} catch (Exception e) {
ra.addFlashAttribute("error", e.getCause()+ "error");
ra.addFlashAttribute("resultado", "El resultado no es el esperado");
return "redirect:/login";
}
}
}
Class ServiceEmpleado
package germanAcosta.electronicaDonPepe.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import germanAcosta.electronicaDonPepe.dominio.Empleado;
import germanAcosta.electronicaDonPepe.repository.IEmpleadoDAO;
import germanAcosta.electronicaDonPepe.service.ServicioException;
#Service
public class ServiceEmpleado {
#Autowired
private IEmpleadoDAO empleadoDAO;
public Empleado correctLogin(Integer dni, String password) throws ServicioException {
Empleado empleado = this.empleadoDAO.getById(dni);
if (empleado == null) {
throw new ServicioException("El usuario no existe");
} else if (empleado.getPassword() != password) {
throw new ServicioException("El password es incorrecto, por favor reintente nuevamente");
} else {
return empleado;
}
}
}
Login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<!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>Electronica Don Pepe</title>
</head>
<body>
<h1>login.jsp</h1>
<br>
<sf:form action="${pageContext.request.contextPath}/login/check"
method="post" commandName="login">
<table>
<tr>
<td><sf:label path="dni">Ingrese su dni</sf:label></td>
<td><sf:input path="dni" type="text" /></td>
</tr>
<tr>
<td><sf:label path="password">Ingrese su contraseña</sf:label></td>
<td><sf:input path="password" type="text" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Ingresar"></td>
</tr>
</table>
</sf:form>
<c:out value="${resultado}"></c:out>
<c:out value="${loginForm.password}"></c:out>
</body>
</html>
Thanks in advance
In LoginController class, public ServiceEmpleado serviceEmpleado; is not autowired. It is not injected with any instance, hence it is null.
Use
#Autowired
public ServiceEmpleado serviceEmpleado;
I use #Autowired on my ServiceEmpleado as #Yuva suggested, but I realized that the annotation it's for an interface and not for a class. So I changed the ServiceEmpleado name to ServiceEmpleadoImpl and add an interface implementation to that class called IServiceEmpleado.
Then I add #Transactional to ServiceEmpleadoImpl and configured the bean on my Config.java
That resolved the problem. Thanks

ModelAttribute not working with lists in spring

I want to bind a List using ModelAttribute. The list contains objects of type Transaction each of which contains transactionid (type int). This is my controller code:
#RequestMapping(value = "/approvecreditdebit.do", method = RequestMethod.POST)
public ModelAndView doActions(HttpServletRequest request,
#ModelAttribute("clc") Clc transactionList, BindingResult result,
ModelMap model) {
/*
* switch (action) { case "approve":
*/
System.out.println("Obj = " + transactionList.getClass());
System.out.println("Val = " + transactionList.getTransactionList());
Users users = new Users();
return new ModelAndView("internal","internal",users);
}
This is my jsp code:
<form:form action="approvecreditdebit.do" method="POST"
modelAttribute="clc">
<table border="1">
<tr>
<th>no</th>
<th>ID</th>
</tr>
<c:forEach items="${clc.transactionList}"
var="transaction" varStatus="status">
<tr>
<td>${status.index}</td>
<td><input name = "transaction[${status.index}].transactionId"
value="${transaction.transactionId}" /></td>
</tr>
</c:forEach>
</table>
<br>
<br>
<center>
<input type="submit" value="approve" />
</center>
</form:form>
This is the Clc class:
public class Clc{
private List<Transaction> transactionList;
public List<Transaction> getTransactionList() {
return transactionList;
}
public void setTransactionList(List<Transaction> transactionList) {
this.transactionList = transactionList;
}
}
The value of transactionList is not being set to the values received from the form. I receive the following error:
Request processing failed; nested exception is java.lang.NullPointerException
I tried searching for the solution on google and got a lot of solutions from stackoverflow but none of them seem to work.
Try something like this (notice the use of <form:input>). I just tried it on a simple Spring MVC app and it works (list is not null and has the values from the form when I try to access it in my POST method).
<c:forEach var="transaction" varStatus="status" items="${clc.transactionList}">
<tr>
<td>${status.index}</td>
<td><form:input path="transactionList[${status.index}].id" /></td>
</tr>
</c:forEach>

Neither BindingResult nor plain target object for bean name 'contact' available as request attribute

I am trying to create a simple project using spring mvc +maven+ jpa hibernate.
My jsp page is :
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div>
<form:form action="contact.html" method="post" commandName="contact" >
<div>
<label for="firstname">First Name</label>
<form:input path="firstname"/>
<form:errors path="firstname" cssClass="error"/>
</div>
<div>
<label for="lastname">Last Name</label>
<form:input path="lastname"/>
<form:errors path="lastname" cssClass="error"/>
</div>
<div>
<label for="telephone">Telephone</label>
<form:input path="telephone"/>
<form:errors path="telephone" cssClass="error"/>
</div>
<div>
<label for="email">Email</label>
<form:input path="email"/>
<form:errors path="email" cssClass="error"/>
</div>
<div>
<form:button name="submit" value="submit">Add Contact</form:button>
</div>
My contact.html controller :
package com.corasent.contacts.controllers;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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 org.springframework.web.servlet.ModelAndView;
import com.corasent.contacts.form.Contact;
import com.corasent.contacts.service.ContactService;
#Controller
#RequestMapping("contact.html")
public class ContactManagerController {
#Autowired
ContactService cs;
#RequestMapping(method = RequestMethod.GET)
public String contactController()
{
System.out.println("In controller");
ModelMap map=new ModelMap();
Contact contact=new Contact();
map.addAttribute("contact", contact);
return "contacts";
}
#RequestMapping(method=RequestMethod.POST)
public ModelAndView getInfo(#ModelAttribute Contact contact, BindingResult result, HttpServletRequest req)
{
ModelAndView mv=new ModelAndView("contacts");
String name=contact.getFirstname();
System.out.println("name is="+name);
cs.validate(contact, result);
if(result.hasErrors())
{
return mv ;
}
System.out.println("in post method");
return mv;
}
public ContactService getCs() {
return cs;
}
public void setCs(ContactService cs) {
this.cs = cs;
}
}
I know that the commandName in jsp is the bean that has to be passed to the controller. the same bean is initiated in controller but seems to be not working. its giving me the above error. i am also pasting the stackTrace.
ERROR: org.springframework.web.servlet.tags.form.InputTag - Neither BindingResult nor plain target object for bean name 'contact' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contact' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:152)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:143)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:127)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.WEB_002dINF.views.jsp.contacts_jsp._jspx_meth_form_005finput_005f0(contacts_jsp.java:272)
at org.apache.jsp.WEB_002dINF.views.jsp.contacts_jsp._jspService(contacts_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
12 Apr, 2013 12:36:53 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/contacts] threw exception [An exception occurred processing JSP page /WEB-INF/views/jsp/contacts.jsp at line 10
7:
8: <div>
9: <label for="firstname">First Name</label>
10: <form:input path="firstname"/>
11: <form:errors path="firstname" cssClass="error"/>
12: </div>
Thanks in advance
Try with:
#RequestMapping(method = RequestMethod.GET)
public String contactController(Model model)
{
System.out.println("In controller");
// ModelMap map=new ModelMap();
Contact contact=new Contact();
model.addAttribute("contact", contact);
return "contacts";
}
In your code, you're creating a ModelMap, but it's useless because is being destroyed at the end of the method. Passing the model as a parameter, spring will use it later to fill your JSP.
You have an example here.
Try something like this:
#RequestMapping(method = RequestMethod.GET)
public ModelAndView contactController()
{
System.out.println("In controller");
// First attribute is view name, second model name and last model object
return new ModelAndView("contacts","contact", new Contact());
}
Also, in your form i would change attribute commandName by modelAttribute, i think i have read that the first one is a kind of deprecated (don't fully trust me here)

Can't configure <form:input> within <foreach> tag

I am using the Spring form tag library in my JSP. My command object contains a List and i want to make bindings for each object of the list in my JSP. But STS gives me an Exception that tells that:
Bean property 'testList[0]' is not readable or has an invalid getter
method: Does the return type of the getter match the parameter type of
the setter?]
Here is full Exception code:
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/mvc] threw exception [org.springframework.beans.NotReadablePropertyException: Invalid property 'testList[0]' of bean class [java.util.ArrayList]: Bean property 'testList[0]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?] with root cause
org.springframework.beans.NotReadablePropertyException: Invalid property 'testList[0]' of bean class [java.util.ArrayList]: Bean property 'testList[0]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:729)
at org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:576)
at org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:553)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:719)
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:147)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
at org.apache.jsp.WEB_002dINF.views.test_jsp._jspx_meth_form_005finput_005f0(test_jsp.java:217)
at org.apache.jsp.WEB_002dINF.views.test_jsp._jspx_meth_c_005fforEach_005f0(test_jsp.java:167)
at org.apache.jsp.WEB_002dINF.views.test_jsp._jspx_meth_form_005fform_005f0(test_jsp.java:122)
at org.apache.jsp.WEB_002dINF.views.test_jsp._jspService(test_jsp.java:82)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Here is my .jsp page :
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello!
</h1>
<form:form action="saveStudent" method="POST" modelAttribute = "testList">
<c:forEach items="${testList}" var="test" varStatus="pStatus">
Test Question: <form:input path = "testList[${pStatus.index}].testQuestion" value = "${test.testQuestion}"/>
Option one: <form:input path = "testList[${pStatus.index}].optionOne" value= "${test.optionOne}"/>
Option two: <form:input path = "testList[${pStatus.index}].optionTwo" value= "${test.optionTwo}"/>
Option three: <form:input path = "testList[${pStatus.index}].optionThree" value= "${test.optionThree}"/>
</c:forEach>
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
Here is my method code:
#RequestMapping(value = "/test", method = RequestMethod.GET)
public String redactTest(Model model, HttpServletRequest request) {
List<Test> testList = db.getTests();
model.addAttribute("testList", testList);
return "test";
}
What i'm doing wrong? Any suggestions? Thanks!
OK, i have a solution: first i've added TestWrapper class which contains List with get and set:
public class TestWrapper {
private List<Test> testList = null;
public TestWrapper(List<Test> testList) {
this.testList = testList;
}
public void setTestList(List<Test> testList) {
this.testList = testList;
}
public List<Test> getTestList() {
return testList;
}
}
Then i've changed my method code:
#RequestMapping(value = "/test", method = RequestMethod.GET)
public String redactTest(Model model, HttpServletRequest request) {
List<Test> testList = db.getTests();
TestWrapper testWrapper = new TestWrapper(testList);
model.addAttribute("testWrapper", testWrapper);
return "test";
}
and after i've changed my .jsp:
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello!
</h1>
<form:form action="saveStudent" method="POST" modelAttribute = "testWrapper">
<c:forEach items="${testWrapper.testList}" var="test" varStatus="i">
Test Question: <form:input path = "testList[${i.index}].testQuestion" value = "${test.testQuestion}"/>
Option one: <form:input path = "testList[${i.index}].optionOne" value= "${test.optionOne}"/>
Option two: <form:input path = "testList[${i.index}].optionTwo" value= "${test.optionTwo}"/>
Option three: <form:input path = "testList[${i.index}].optionThree" value= "${test.optionThree}"/>
</c:forEach>
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
And now it works! Hope it will help someone :)

Resources