【发布时间】:2021-04-25 05:38:34
【问题描述】:
我有一个带有 textarea 标签的 html 页面。
textarea(和相应的 postgres db 字段)接受 1000 个字符。
如果我创建一个包含 1000 个字符(包括单词之间的空格)的单个段落的文本并单击保存,它会正确保存到数据库。
但是如果我用换行符替换一个字符,点击保存时会出现以下错误:
org.postgresql.util.PSQLException: ERROR: value too long for type character varying(1000)
错误发生在进入控制器方法之前。
为什么会这样?
问题不在于接受换行符。
那是因为如果字符总数远远少于限制(比如说总共 900 个),我可以插入几个换行符,这样就可以节省了。
所以无法理解为什么在接近字符限制时接受换行符会发生错误。
这里是代码。
<textarea type="text" name="description" maxlength="1000" th:text="${descBind.description}" cols="55" rows="16"></textarea>
</br> <input type="submit" name="btnSave" value="Save" />
型号
@Entity
public class Desc implements Serializable {
private static final long serialVersionUID = 1L;
@Column(length = 1000)
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
控制器
@RequestMapping(value = "descriptionpage", params = "btnSave", method = RequestMethod.POST)
public ModelAndView save(@ModelAttribute Desc desc, RedirectAttributes attributes) {
//code to update the db
ModelAndView modelAndView = new ModelAndView("descriptionpage.html");
modelAndView.addObject("descBind", desc);
return modelAndView;
}
【问题讨论】:
-
Line breaks are characters, too。每个换行符都会将文本的长度增加一个或多个字符。
-
有道理。问题是如何防止应用程序在这种情况下抛出错误?当用户插入换行符时,似乎 textarea maxlength 属性验证在 textarea 中无法正常工作。
-
我无法复制该问题。在您的问题中使用 textarea,如果我输入 999 个字符的可见文本,那么我可以输入 1 个换行符(任何地方) - 之后我无法输入更多数据 - 可见字符、空格、换行符、制表符等。跨度>
-
对,您不能输入更多数据。这很好用。但是,当您单击保存时,它会抛出我提到的错误并且永远不会进入控制器方法。而如果你不添加换行符,它将进入控制器方法并正常保存到数据库。
-
好的 - 这是我的错误 - 我误解了这个问题。
标签: java html spring spring-mvc thymeleaf