Html 春季百里香叶+;文本区域
我用一个输入文本和一个Html 春季百里香叶+;文本区域,html,spring,textarea,thymeleaf,Html,Spring,Textarea,Thymeleaf,我用一个输入文本和一个textarea创建了表单。输入文本工作正常,但textarea甚至不显示: <div id="news" th:fragment="admin_panel"> <form method="POST" th:action="@{/addNews}" th:object="${news}" id="myform"> Tytuł: <input type="text" th:field="*{title}"/
textarea
创建了表单。输入文本工作正常,但textarea甚至不显示:
<div id="news" th:fragment="admin_panel">
<form method="POST" th:action="@{/addNews}" th:object="${news}" id="myform">
Tytuł:
<input type="text" th:field="*{title}"/>
<input type="submit" value="Wstaw"/>
</form>
<textarea name="news_content" rows="20" cols="80" th:field="${news.content}" form="myform">
...
</textarea>
</div>
蒂图什:
...
当我删除th:field
时,会显示textarea
,当我使用th:value
而不是th:field
时,也会显示,但不会将书面文本保存到news.content(news.title保存正常)
我不知道。。。我读了thymeleaf的参考资料,但找不到答案,所以请帮助好人 您必须使用选定的对象表达式
*{content}
并将textarea标记放在表单标记内
最后,它是关于结果表单中生成的name
属性的。名称需要与所选根对象th:object
中的propertyAccessor
对应。
该表单由spring处理(没有任何thymeleaf拦截)
关于spring集成的文档非常好:
他们说:
th:field属性的值必须是选择表达式(*{…}),这是有意义的,因为它们将在表单支持bean而不是上下文变量(或SpringMVC术语中的模型属性)上进行计算
编辑: 由于链接到该项目,修复非常简单:
- Thymeleaf 3.0.0.BETA03在textarea处理器中有一个bug,迁移到3.0.0.RELEASE解决了这个问题
- 此外,我还将textarea移动到表单元素中
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,在textarea中有th:field=${news.title}),它也可以正常工作。也许我应该用另一个表达式代替th:field
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
@Controller
@RequestMapping( "/admin" )
public class AdminController
{
@RequestMapping( method = GET )
public String admin( Model model )
{
model.addAttribute( new News() );
return "admin";
}
}
包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>
蒂图什:
...
我正在使用Thymeleaf3.0。也许这就是原因
在参考资料中,我读到:
th:field属性的行为不同,这取决于它是附加到、还是标记(也取决于标记的特定类型)
但我找不到在input和textarea中使用th:字段有什么区别 您不需要表单文本字段。通过表单id将textarea与表单链接就足够了
<textarea rows="8" cols="120" name="lines" form="usrform" th:text="${message}"></textarea>
<form method="POST" enctype="multipart/form-data" th:action="@{/}" id="usrform">
<button type="submit" name="action" value="submitlines">Submit</button>
</form>
当我通过textarea输入保存一些数据时(在执行保存到DB时处于干净状态),我的textarea输入工作正常,但在编辑表单的情况下(我的textarea输入应该显示来自模型属性书.description的预填充说明)为空,原因是th:value属性,我将其更改为th:field属性,它开始按预期工作
<textarea class="form-control" id="description" rows="5"
name="description"
placeholder="Description" th:field="${book.description}"
required="required"></textarea>
不工作:/。。。此外,我认为${news.content}在我们使用th:object=${news}时等于*{content}。还有其他的解决方案吗?事实上,我会说你甚至应该从thymeleaf得到一个例外,但那只是我的记忆。Th:字段“需要”对象表达式。可能您有一个输入错误/错误字符,并且表达式从未计算过?查看最终结果,并将其发布到此处,使用路径[/eniupage]抛出异常的上下文中Servlet[dispatcher]的th:fieldAbout exceptions:SEVERE:Servlet.service()[请求处理失败;嵌套异常为org.thymeleaf.exceptions.TemplateProcessingException:处理器'org.thymeleaf.spring4.processor.SpringTextareaFieldTagProcessor'执行期间出错(模板:“模板/片段”-第144行,第60列)]使用根本原因java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0使用更多详细信息更新了答案。----如果没有(简化)模型的代码、请求映射和生成的html表单,我们将无法进一步帮助您。好的,我添加了控制器类和域对象。