An error happened during template parsing(springboot+thymeleaf) - spring-boot

I provide all the things but i donot know why I am getting this type
of issue
And use right thymeleaf syntax but it through a null exception
I got below type of error
Wed Sep 08 21:03:20 IST 2021
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/page.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/page.html]")
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:366)
page.html
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="/fragments/head"></head>
<body>
<nav th:replace="/fragments/nav :: nav-front"></nav>
<div class="container">
<div class="row">
<div class="col"></div>
<div col="7" th:utext="${page.content}"></div>
<div class="col"></div>
</div>
</div>
<div th:replace="/fragments/footer"></div>
</body>
</html>
PagesController.kt
package com.nilmani.cmsshopingcart.controller
import com.nilmani.cmsshopingcart.model.Page
import com.nilmani.cmsshopingcart.repository.PageRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
#Controller
#RequestMapping("/")
class PagesController {
#Autowired
private lateinit var pageRepository: PageRepository
#GetMapping
fun home(model: Model?):String?{
val page: Page?= pageRepository.findBySlug("home")
if (model != null) {
model.addAttribute("page",page)
}
return "page"
}
}
pageRepository.kt
package com.nilmani.cmsshopingcart.repository
import com.nilmani.cmsshopingcart.model.Page
import org.springframework.data.jpa.repository.JpaRepository
interface PageRepository : JpaRepository<Page,Int> {
fun findBySlug(slug: String?): Page?
fun findBySlugAndIdNot(slug: String?, id: Int?): Page?
fun findAllByOrderBySortingAsc(): List<Page?>?
}
page.kt
package com.nilmani.cmsshopingcart.model
import javax.persistence.*
import javax.validation.constraints.Size
#Entity
#Table(name = "pages")
data class Page(
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
val id:Int=0,
#Size(min=2,message = "Title must be four character")
val title:String="",
#Column(nullable = false)
var slug:String="",
#Size(min=4,message = "Content must contain four character")
#Column(nullable = false)
val content:String="",
var sorting:Int=0
)
I got this type of response when i call localhost:8080 in my browser
Wed Sep 08 21:03:20 IST 2021
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/page.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/page.html]")
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:366)
at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:190)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1397)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1142)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
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:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "page.content" (template: "page" - line 9, col 22)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 48 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "page.content" (template: "page" - line 9, col 22)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:292)
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)
at org.thymeleaf.standard.processor.StandardUtextTagProcessor.doProcess(StandardUtextTagProcessor.java:87)
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314)
at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304)
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:278)
at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(OutputExpressionInlinePreProcessorHandler.java:186)
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:124)
at org.attoparser.HtmlElement.handleOpenElementEnd(HtmlElement.java:109)
at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(HtmlMarkupHandler.java:297)
at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(MarkupEventProcessorHandler.java:402)
at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:159)
at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301)
... 50 more
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'content' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51)
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:92)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:112)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:337)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265)
... 69 more

The error in the stack trace shows:
Property or field 'content' cannot be found on null
Which means you are trying to call content on something, but that something is null.
The only line in your Thymeleaf template that seems to match this is:
<div col="7" th:utext="${page.content}"></div>
So it means the page model attribute is null.
The controller puts stuff in the model, so we need to look there why this is null:
val page: Page?= pageRepository.findBySlug("home")
if (model != null) {
model.addAttribute("page",page)
}
My guess is that page is null here because there is nothing for slug home in your database?

Related

Getting values from a view to a controller, then to another view

My index.html lists some reviews, when pressing on the title of one, my controller should get that review's id and use a template view to show only that review's info; it should work with any id/review. I'm searching the internet for 2 hours now and still can't find that syntax I need to achieve this.
This is my current controller:
#GetMapping("/{id}")
public String showSpecificItem(#PathVariable(name="id") Integer id, Model model){
Optional<Review> rev = rev_repo.findById(id);
model.addAttribute("reviews", rev);
return "index-specific";
}
This is the index part with the title:
<div class="container py-5" th:switch="${reviews}"
th:each="review : ${reviews}">
<div class="row">
<div class="col-lg-8 mx-auto">
<ul class="list-group shadow">
<li class="list-group-item">
<div class="media align-items-lg-center flex-column flex-lg-row p-3"
style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">
<div class="media-body order-2 order-lg-1">
<form th:action="#{|/${review.id}|}" method="get">
<button type="submit" th:text="${review.title}"
style="color: #100029; text-decoration: none; font-size: 25px; background:none; border: none; margin: 0; padding: 0; cursor: pointer;"></button>
</form>
This is part of the template:
<div class="profile-card">
<div class="text-center profile-back"
style="background-color: rgba(255, 199, 0, 0); width: 70%; margin-top: 10%; margin-left: 15%; height: 100px;"></div>
<h2 class="border rounded profile-name"
style="background-color: #ffc700; color: #100029; width: 432px;" th:text="${reviews.title}"></h2>
<p class="border rounded profile-bio"
style="background-color: #ffffff; color: #100029; margin-left: 15%; width: 70%;" th:text="${reviews.content}"></p>
<div></div>
</div>
Part of the Review entity(rest is just constr, getters and setter):
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#NotEmpty(message="All fields must be completed!")
#Column(name = "title")
private String title;
#NotEmpty(message="All fields must be completed!")
#Column(name = "content")
private String content;
#NotNull(message="All fields must be completed!")
#Column(name = "score")
private Integer score;
#NotEmpty(message="All fields must be completed!")
#Column(name = "restaurant")
private String restaurant;
And here's the repository, don't know if it's useful or not:
#Repository
public interface ReviewRepository extends CrudRepository <Review, Integer> {
Optional<Review> findById(Integer id);
}
This are the errors I'm getting:
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/index-specific.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/index-specific.html]")
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362)
at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)
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.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
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.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
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.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
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.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:541)
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:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "reviews.title" (template: "index-specific" - line 23, col 69)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 48 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "reviews.title" (template: "index-specific" - line 23, col 69)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)
at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144)
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314)
at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205)
at org.thymeleaf.engine.Model.process(Model.java:282)
at org.thymeleaf.engine.Model.process(Model.java:290)
at org.thymeleaf.engine.IteratedGatheringModelProcessable.processIterationModel(IteratedGatheringModelProcessable.java:367)
at org.thymeleaf.engine.IteratedGatheringModelProcessable.process(IteratedGatheringModelProcessable.java:221)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleCloseElement(ProcessorTemplateHandler.java:1640)
at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleCloseElementEnd(TemplateHandlerAdapterMarkupHandler.java:388)
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleCloseElementEnd(InlinedOutputExpressionMarkupHandler.java:322)
at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleCloseElementEnd(OutputExpressionInlinePreProcessorHandler.java:220)
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleCloseElementEnd(InlinedOutputExpressionMarkupHandler.java:164)
at org.attoparser.HtmlElement.handleCloseElementEnd(HtmlElement.java:169)
at org.attoparser.HtmlMarkupHandler.handleCloseElementEnd(HtmlMarkupHandler.java:412)
at org.attoparser.MarkupEventProcessorHandler.handleCloseElementEnd(MarkupEventProcessorHandler.java:473)
at org.attoparser.ParsingElementMarkupUtil.parseCloseElement(ParsingElementMarkupUtil.java:201)
at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:725)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301)
... 50 more
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'title' cannot be found on object of type 'java.util.Optional' - maybe not public or not valid?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51)
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:92)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:112)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:337)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263)
... 75 more
Can someone please help me? I'm very new to this and I have no idea what I'm doing here.
At least it shows me the good id up there...
the url when pressed on a title
You pass Optional object to model, but I need to pass the exact value object.
Try this:
rev.ifPresent(reviewsValue -> model.addAttribute("reviews", reviewsValue))
also, You need to remember about fallback action when rev is empty.
eg if (rev.isEmpty()) return "empty-index"

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

display SQL rows in table

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>

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)

Resources