Spring Thymeleaf + Textarea

3

我创建了一个包含一个输入框和一个 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 时,textarea 也会显示出来,但是不会将文本保存到 news.content(news.title 可以正常保存)。
我完全没有头绪...我已经阅读了 Thymeleaf 的参考文献,但找不到答案,请好心人帮忙!
4个回答

3

当我通过textarea输入框保存数据时,它能正常工作(在干净的状态下,当我执行向数据库保存操作),但在编辑表单中(这里我的textarea输入框应该显示来自模型属性book.description的预填充描述)却是空白的。原因是th:value属性,我将其更改为th:field属性后,它开始按预期工作。

<textarea class="form-control" id="description" rows="5"
                              name="description"
                              placeholder="Description" th:field="${book.description}"
                              required="required"></textarea>

2
你必须使用选定的对象表达式*{content}并将textarea标签放在form标签内!
最终的目的是生成的表单中的name属性。名称需要对应于选定根对象th:object中的propertyAccessor
表单由Spring处理(没有任何Thymeleaf拦截)。
关于Spring集成的文档非常好:http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html 他们说:
th:field属性的值必须是选择表达式(*{...}),这是有道理的,因为它们将在表单支持bean上计算,而不是在上下文变量(或Spring MVC术语中的模型属性)上计算。
编辑: 感谢项目链接,修复很容易:
  • Thymeleaf 3.0.0.BETA03在textarea处理器中存在一个错误,移动到3.0.0.RELEASE可以解决此问题
  • 此外,我已将textarea移至form元素内部。

无法工作 :/ ... 另外,在我看来,当我们使用 th:object = ${news} 时,${news.content} 等于 *{content}。还有其他解决方案吗? - Bambelal
事实上,我甚至会建议您从thymeleaf中获取异常,但这只是我的记忆。Th:field需要一个对象表达式。可能您有拼写错误/误字符,因此该表达式永远不会被评估?查看最终结果并在此处发布,使用th:field。 - Martin Frey
关于异常:SEVERE:在路径为[/ eniupage]的上下文中,Servlet [dispatcher]的servlet.service()抛出了异常[请求处理失败;嵌套异常为org.thymeleaf.exceptions.TemplateProcessingException:执行处理器'org.thymeleaf.spring4.processor.SpringTextareaFieldTagProcessor'时出错(模板:“templates / fragments”-第144行,第60列 )],根本原因是java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0。 - Bambelal
没有你(简化后的)模型代码、请求映射和生成的HTML表单,我们将无法为您提供进一步的帮助。 - Martin Frey
啊,现在我明白了 :) 你的文本区域不在表单内!至少在你在这里发布的片段中是这样。将其移动到内部并使用 th:field="*{content}",你就没问题了。 - Martin Frey
显示剩余5条评论

1
您不需要表单文本字段。将textarea与表单通过表单id链接即可。
<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>

和控制器:

@RequestMapping(value="/", method=RequestMethod.POST, params="action=submitlines")
public String handleForm(
        @RequestParam("lines") String input,
        RedirectAttributes redirectAttributes) {
}

0
关于异常:
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之间的差异是什么。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接