关于异常:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/eniupage] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring4.processor.SpringTextareaFieldTagProcessor' (template: "templates/fragments" - line 144, col 60)] with root cause
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
在我的表单中,有文本输入和文本区域,如您所见。news.title已经保存成功,但是news.content没有。当我替换这些参数进行测试时(在文本输入中使用news.content,在文本区域中使用th:field = $ {news.title}),它也可以正常工作。也许我应该使用另一个表达式而不是th:field?
News.java
package eniupage.domain;
public class News
{
private String title;
private String content;
private Date date;
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content)
{
this.content = content;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}
HomeController.java
package eniupage.web;
@Controller
@RequestMapping( "/" )
public class HomeController
{
@Autowired
AddNewsService addNewsService;
@RequestMapping( method = GET )
public String home( Model model )
{
model.addAttribute( "newses", addNewsService.getNewses() );
return "home";
}
@RequestMapping( value = "/addNews", method = POST )
public String addNews( News news )
{
addNewsService.addNews( news );
return "redirect:/";
}
}
AdminController.java
@Controller
@RequestMapping( "/admin" )
public class AdminController
{
@RequestMapping( method = GET )
public String admin( Model model )
{
model.addAttribute( new News() );
return "admin";
}
}
没有HTML表单的结果,因为它甚至没有显示在div中。只有文本输入和提交按钮。
编辑后的HTML:
<form action="#" method = "POST" th:action="@{/addNews}" th:object = "${news}" id = "myform">
Tytuł: <input type = "text" th:field = "*{title}" />
<input type = "submit" value = "Add" /></br>
<textarea rows = "20" cols = "80" th:field = "*{content}" form = "myform" >... </textarea>
</form>
我正在使用
thymeleaf 3.0
。也许这就是原因?
我在参考资料中读到:
根据所附加的标签类型(以及特定类型的标签)th:field
属性的行为会有所不同。
但我找不到在输入和文本区域中使用
th:field
之间的差异是什么。
th:field="*{content}"
,你就没问题了。 - Martin Frey