Quantcast

Thymeleaf textarea

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Thymeleaf textarea

antofierares
Hello everyone, I'm having problems with binding the values from the textarea to my object.

Controller method :
@GetMapping(value="/addNews")
        public String addNewsGet(Model model,News news) {
                model.addAttribute("news", news);
                return FORM_MANAGER_PATH+"/addNewsForm";
        }

@PostMapping(value="/addNews")
        public String addNewsPost(Model model,@ModelAttribute("news") News news, Principal principal) {
                                System.out.println(news.getNews());
                return "redirect:/manager/listNews";
        }



Model class :

@DateTimeFormat(iso=ISO.DATE)
        private Date postDate = new Date();
       
        private String title;
        private String news;
       
        @ManyToOne
        @JoinColumn(name = "restaurant_id", nullable=false)
        private Restaurant restaurant;

//getters && setters

view :
<form th:action="@{/manager/addNews}" method="post">
<div class="form-group">
        <label>Insert news title</label>
        <input type="text" class="form-control" name="title" th:field="*{news.title}" placeholder="News title" />
</div>
<div class="form-group">
          <label>Insert news text</label>
           <textarea class="form-control" name="news" th:field="*{news.news}" placeholder="News text"></textarea>
           </div>
           <button type="submit" class="btn btn-default">Add news</button>
</form> 

exception :
2017-03-26 11:38:39.443  WARN 7976 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.manage.restaurant.domain.News'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'txtxtxt'; nested exception is java.lang.NumberFormatException: For input string: "txtxtxt"
2017-03-26 11:38:39.444  WARN 7976 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.manage.restaurant.domain.News'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'txtxtxt'; nested exception is java.lang.NumberFormatException: For input string: "txtxtxt"


Which could be the problem?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Thymeleaf textarea

migueldeblas
you must change:

<form th:action="@{/manager/addNews}" th:object="news" method="post">
<input type="text" class="form-control" name="title" th:field="*{title}" placeholder="News title" />
<textarea class="form-control" name="news" th:field="*{news}" placeholder="News text"></textarea>

Note: See Thymeleaf+Spring  ${...} != *{...}

Loading...