所以我的团队有一个jsf并且我们使用EL语句读取输入框的值,工作正常但默认情况下我们将值设置为应该填充到字段中的值。如何在仍然使EL语句工作的同时为输入设置默认值。 示例:
<h:inputText value="#{registrationBean.firstName}" styleClass="formEle"/>
我试过
<h:inputText value="#{registrationBean.firstName}First Name" styleClass="formEle"/>
但是当我们提交时,这会破坏连接。 有什么想法吗?
答案 0 :(得分:14)
你正在寻找一种被称为“水印”或“占位符”的东西。在较低级别,这些天通常由HTML5 placeholder
属性:
<input type="text" placeholder="First Name" />
这当然仅适用于支持HTML5的浏览器,然后仅适用于支持新HTML5属性的JSF组件。标准JSF <h:inputText>
本身不支持此功能。您需要寻找支持此功能的第三方组件库。其中包括PrimeFaces及其<p:watermark>
:
<h:inputText id="firstName" value="#{registrationBean.firstName}" />
<p:watermark for="firstName" value="First Name" />
如果由于某些原因使用PrimeFaces不是一个选项,你需要自己重新发明它自定义组件和/或自定义JS / jQuery的味道,就像<p:watermark>
正在做的那样覆盖。
答案 1 :(得分:2)
您可以在PostConstrut阶段在您的辅助bean中定义<h:inputText>
的默认值。
@ManagedBean(name="registrationBean")
public class RegistrationBean{
private String firstName;
@PostConstruct
public void init(){
firstName = "your default value";
}
}
页面创建后h:inputText
中显示的值将与您在辅助bean中定义的值相同。
没有任何第三方图书馆,它可以帮助某人。
答案 2 :(得分:1)
在加载为HTML5插入占位符属性后,可以通过JavaScript修改JSF呈现的HTML标记:
<script>
window.onload = function() {
document.getElementById('email').setAttribute("placeholder", "Email");
document.getElementById('pwd').setAttribute("placeholder", "Password");
}
</script>
答案 3 :(得分:-1)
如果您决定使用PrimeFaces,则无需使用水印,请参阅以下示例:
<h:form>
<p:panel id="panel" header="Client Details" style="margin-bottom:10px;">
<p:messages id="messages" />
<h:panelGrid columns="2">
<h:outputLabel for="firstName" value="First Name" />
<p:inputText id="firstName" value="#{clientlistpagebean.selectedFieldClient.firstName}" required="true" />
<p:outputLabel for="lastName" value="Last Name" />
<p:inputText id="lastName" value="#{clientlistpagebean.selectedFieldClient.lastName}" required="true" />
</h:panelGrid>
</p:panel>
<p:commandButton value="Submit" update="panel" style="margin-right:20px;" />
</h:form>