habuma / spring-in-action-5-samples

Home for example code from Spring in Action 5.
Apache License 2.0
1.21k stars 1.04k forks source link

chapter 3 jpa #107

Open jim-michael opened 3 years ago

jim-michael commented 3 years ago

@PostMapping public String processDesign( @Valid Taco taco, Errors errors, @ModelAttribute Order order) {

      System.out.println("you are in posting"+ taco.getName());

    if (errors.hasErrors()) {

        return "design";
    }
    System.out.println("something is wrwng"+taco.getName());
    Taco saved= tacoRepo.save(taco);     <====================         i get the error when i add this line
    //Taco saved = tacoRepo.save(taco);
      order.addDesign(saved);
      System.out.println(saved.getName());
    return "home";

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Aug 19 23:29:46 EDT 2021 There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/design.html]") org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/design.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:1396) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1141) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1080) 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.doPost(FrameworkServlet.java:909) at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) 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.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96) 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:202) 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:143) 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:374) 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:1707) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:832) Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "design" - line 59, col 25) 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) ... 52 more Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "design" - line 59, col 25) at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) 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.handleStandaloneElement(ProcessorTemplateHandler.java:918) at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleStandaloneElementEnd(TemplateHandlerAdapterMarkupHandler.java:260) at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleStandaloneElementEnd(InlinedOutputExpressionMarkupHandler.java:256) at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleStandaloneElementEnd(OutputExpressionInlinePreProcessorHandler.java:169) at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleStandaloneElementEnd(InlinedOutputExpressionMarkupHandler.java:104) at org.attoparser.HtmlElement.handleStandaloneElementEnd(HtmlElement.java:79) at org.attoparser.HtmlMarkupHandler.handleStandaloneElementEnd(HtmlMarkupHandler.java:241) at org.attoparser.MarkupEventProcessorHandler.handleStandaloneElementEnd(MarkupEventProcessorHandler.java:327) at org.attoparser.ParsingElementMarkupUtil.parseStandaloneElement(ParsingElementMarkupUtil.java:96) at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:706) at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301) ... 54 more Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'design' available as request attribute at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153) at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:253) at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227) at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174) at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ... 67 more

lsuman83 commented 3 years ago

check your design.html file line 59

zmbnz commented 1 year ago

i have same error. i appreciate u guide me

zmbnz commented 1 year ago

`package sia.foood;

import lombok.extern.slf4j.Slf4j; import org.springframework.boot.Banner; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import sia.foood.Ingredient.Type;

import java.util.Arrays; import java.util.List; import java.util.stream.Collectors;

@Controller @Slf4j @SessionAttributes("foodOrder") @RequestMapping("/design") public class FoodController {

@ModelAttribute
public void addIngredientToModel(Model model){
    List<Ingredient> ingredients = Arrays.asList(new Ingredient("wt","water", Type.DRINK),
            new Ingredient("cc", "coca", Type.DRINK),
            new Ingredient("dgh", "doogh", Type.DRINK),
            new Ingredient("bn", "bean", Type.BEANS),
            new Ingredient("ot", "oat", Type.BEANS),
            new Ingredient("on", "onion", Type.VEGGIE),
            new Ingredient("pt", "potato", Type.VEGGIE),
            new Ingredient("tm", "tomato", Type.VEGGIE),
            new Ingredient("fsh", "fish", Type.PROTEIN),
            new Ingredient("mt", "meat", Type.PROTEIN),
            new Ingredient("ic", "ice cream", Type.DESSERT),
            new Ingredient("fl", "faloode", Type.DESSERT),
            new Ingredient("ck", "cake", Type.DESSERT),
            new Ingredient("eg", "egg", Type.PROTEIN)
    );
    Type[] types = Ingredient.Type.values();
    for(Type t : types) {
        model.addAttribute(t.toString().toLowerCase(), filterIngredientByType(ingredients, t));
    }
}

@ModelAttribute(name="food")
public Food food(){
    return new Food();
}

@ModelAttribute(name="foodOrder")
public FoodOrder order(){
    return new FoodOrder();
}

@GetMapping
public String showDesignForm(){
    return "home";

    // return "design";
}

@PostMapping
public String showDesignForm1(Food food, @ModelAttribute FoodOrder foodOrder){
    foodOrder.addFood(food);
    log.info("processing food: {}",food);
    return "redirect:/orders/current";
}

private Iterable<Ingredient> filterIngredientByType(List<Ingredient> ingredients , Type type){
    return ingredients.stream().filter(x->x.getType().equals(type)).collect(Collectors.toList());
}

}

`

`package sia.foood;

import lombok.Data;

import java.util.List;

@Data public class Food { private String name; private List ingredients; }

`

zmbnz commented 1 year ago

view home is here: ` <!DOCTYPE html> <html lang="en" xmlns:th="https://www.thymeleaf.org/" xmlns="http://www.w3.org/1999/xhtml">

Title

welcome ...

design your drink

drink
` ![view](https://github.com/habuma/spring-in-action-5-samples/assets/142815246/37a1e6c8-2187-4310-a37b-6d805d78a450)
zmbnz commented 1 year ago

interesting point is, when i put the forms members like checkbox n button out of the form tag, it shows all ingredients correctly. but when i put them between form tag i have white label error page, type=Internal Server Error, status=500

design

zmbnz commented 1 year ago

i checked more. this link helped me https://stackoverflow.com/questions/53743806/error-during-execution-of-processor-org-thymeleaf-spring5-processor-springinput

i changed this line of code th:field = "{food.ingredients}" to field = "{food.ingredients}" it worked by this change.