I have a form which uses a JSF h:inputTextarea
like so:
index.xhtml:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>
<div id="ContentFrame">
<h:form enctype="multipart/form-data">
<h:inputTextarea
id="termineesText"
value="#{controller.text}"/>
<h:commandButton
value="Upload and Continue"
type = "submit"
action="#{controller.respond}"/>
</h:form>
</div>
</h:body>
</html>
result.xhtml:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>
<div id="ContentFrame">
<h:form enctype="multipart/form-data">
<h:inputTextarea
id="termineesText"
value="#{controller.text}"/>
<h:outputText value="#{controller.containsNewline}"/>
</h:form>
</div>
</h:body>
</html>
With this backing bean:
controller.java:
package main;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named("controller")
@SessionScoped
public class Controller implements Serializable {
private static final long serialVersionUID = 1L;
private String text;
private boolean containsNewline = false;
public String respond() {
return "result.xhtml";
}
public void setText(String value) {
if (value.contains("\n") || value.contains("\r")) {
containsNewline = true;
}
text = value;
}
public String getText() {return text;}
public String getContainsNewline()
{
return String.valueOf(containsNewline);
}
}
When the user inputs text over many lines (by pressing enter) and submits the form the newline characters disappear in the text area in result.xhtml
. For example if the user enters:
abc
abc
The result.xhtml
's text area has it as "abcabc", And the output text evaluates to false
. The newline characters seem to get lost when the form is submitted to the server, which can be seen using the containsNewLine
boolean in the controller.
I have read many posts about why newlines aren't shown on a web browser when the text comes from the server side, but my case is the other way around, for some reason new lines are not getting transferred from the Textarea to the backing bean.
What I have tried
I have tried:
<pre><h:inputTextarea id="termineesText" styleClass="termineesTextInput" onchange="enableDisableSubmitBtn()" value="#{controller.termineesText}"/></pre>
And in CSS:
.termineesTextInput { white-space: pre; }
Technology I am using
JSF 2.2 with CDI 1.2 running on Websphere Application Server, and I have done most of my testing using a Chrome incognito window.
How do I get newlines to transfer to the backing property?