处理 primefaces 文本区域提交值中的换行符
我有一个 primefaces 文本区域。用户只需在其中输入多行并提交表单即可。在我的支持 bean 中,值在换行符处分割。每行都经过验证。有效行将从字符串中删除,无效行被保留,并再次发送给用户。
拆分代码:
StringTokenizer tokens=new StringTokenizer(value,"\n\r");
用无效行重新填充值:
valueStrBldr.append(invalidLine).append("\n\r");
问题是,当值重新加载到文本区域时,它有很多不需要的换行符和空行。它是否必须对依赖于平台的换行符执行某些操作?
当我删除 \r 时,问题解决了,但值没有正确分割——我的意思是为什么不一致?
I have a primefaces textarea. User simply enters multiple lines in it and submits the form. In my backing bean, value is split at line-breaks. Each line is validated. The valid lines are removed from the string and invalid lines are kept, and again sent to the user.
Split code:
StringTokenizer tokens=new StringTokenizer(value,"\n\r");
Re-fill value with invalid lines:
valueStrBldr.append(invalidLine).append("\n\r");
The problem is that when value is reloaded to text area it has a lot of unwanted line breaks and empty lines. Does it have to do something with platform dependent line breaks?
When I remove \r , the problem is solved, but then the value is not split correctly -- I mean why inconsistency?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用
BufferedReader#readLine()
而不是读取换行符上的行。这涵盖了所有可能的平台特定换行符,例如\r\n
、\r
和\n
,您无需担心(请注意\n\r
不是有效的换行序列)。写行的时候,我建议使用
\r\n
。它应该在所有客户端(网络浏览器)中正常工作。I suggest to use
BufferedReader#readLine()
instead to read lines on linebreaks. This covers all possible platform specific linebreaks such as\r\n
,\r
and\n
without that you need to worry about (please note that\n\r
is not a valid linebreak sequence).When writing lines, I suggest to use
\r\n
. It should work fine in all clients (webbrowsers).我认为它应该是'\r\n'(相反的顺序)。如果我没记错的话 \r 是一行的结尾,而 \n 是一个新行。
无论如何,尝试打印输入的 ascii 代码,然后查看它。尝试识别该模式。这听起来很暴力,但你会发现,在 3-4 行之后,你就知道发生了什么。
I think it should be '\r\n' (reversed order). If I remember it right \r is the end of a line, and \n is a new line.
anyway, try just printing out the ascii code of the input, and look at it. try recognizing the pattern. This sound brute force, but you will see that after 3-4 lines you have an idea of what is happening.