来自的 JSF 奇怪消息
我使用 inputText 来获取用户的输入
<h:inputText id="firstName" value="#{beanManager.currentUser.firstName }" required="true" style="font-size: 15px"></h:inputText>
,然后添加了一个消息标签
<h:message for="firstName"></h:message>
,但是当 inputtext 为空并且我按提交时,我收到此错误消息:
> j_id_jsp_1382885624_1:ID: Validation Error: Value is required.
如何仅将错误消息显示为这样?
> Validation Error: Value is required.
这是代码:
<f:view>
<h:form>
<h:panelGrid border="1" columns="3" style="width: 643px; ">
<h:outputText value="First Name" style="font-size: 25px; font-weight: bold"></h:outputText>
<h:outputText value="Last Name" style="font-size: 25px; font-weight: bold"></h:outputText>
<h:outputText value="ID" style="font-size: 25px; font-weight: bold"></h:outputText>
<h:inputText id="firstName" value="#{beanManager.currentUser.firstName }" required="true" requiredMessage="Enter a Valid First Name" style="font-size: 15px"></h:inputText>
<h:inputText id="LastName" value="#{beanManager.currentUser.lastName }" required="true" requiredMessage="Enter a Valid Last Name" style="font-size: 15px"></h:inputText>
<h:inputText id="ID" value="#{beanManager.currentUser.ID}" required="true" requiredMessage="Enter a Valid ID" style="font-size: 15px">
</h:inputText>
<h:form><h:commandButton action="#{beanManager.save }" value="Save Changes" style="height: 30px; width: 100px"></h:commandButton>
</h:form>
</h:panelGrid>
<h:commandLink action="backtopersonalview" value="Back To Users" style="height: 30px; width: 100px">
</h:commandLink>
</h:form>
</f:view>
I'm using inputText to get input from a user
<h:inputText id="firstName" value="#{beanManager.currentUser.firstName }" required="true" style="font-size: 15px"></h:inputText>
and then I've added a message tag
<h:message for="firstName"></h:message>
but when the inputtext is blank and I press submit I get this error message:
> j_id_jsp_1382885624_1:ID: Validation Error: Value is required.
how can I display the error message only as this?
> Validation Error: Value is required.
here is the code:
<f:view>
<h:form>
<h:panelGrid border="1" columns="3" style="width: 643px; ">
<h:outputText value="First Name" style="font-size: 25px; font-weight: bold"></h:outputText>
<h:outputText value="Last Name" style="font-size: 25px; font-weight: bold"></h:outputText>
<h:outputText value="ID" style="font-size: 25px; font-weight: bold"></h:outputText>
<h:inputText id="firstName" value="#{beanManager.currentUser.firstName }" required="true" requiredMessage="Enter a Valid First Name" style="font-size: 15px"></h:inputText>
<h:inputText id="LastName" value="#{beanManager.currentUser.lastName }" required="true" requiredMessage="Enter a Valid Last Name" style="font-size: 15px"></h:inputText>
<h:inputText id="ID" value="#{beanManager.currentUser.ID}" required="true" requiredMessage="Enter a Valid ID" style="font-size: 15px">
</h:inputText>
<h:form><h:commandButton action="#{beanManager.save }" value="Save Changes" style="height: 30px; width: 100px"></h:commandButton>
</h:form>
</h:panelGrid>
<h:commandLink action="backtopersonalview" value="Back To Users" style="height: 30px; width: 100px">
</h:commandLink>
</h:form>
</f:view>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的。
对于当前消息,我可以说它由
form id
、item id
和default message
组成。如果您想更改默认消息,可以使用消息包来完成此操作,您必须在
faces-config.xml
中定义该消息包。并更改 javax.faces.component.UIInput.REQUIRED 的值。
例如,您的消息包
language.properties
可能包含:大多数
JSF
实现都带有以下默认消息,我已经 复制自Mojarra
:还可以使用
requiredMessage
属性为单个项目定义必需的消息。那么您的
应该如下所示:Yes, it's possible.
To the current message, I can say that it consists out of
form id
,item id
anddefault message
.If you want to change the default message, you could do this with a message bundle, which you have to define in your
faces-config.xml
.And change the value for
javax.faces.component.UIInput.REQUIRED
.For example your message bundle
language.properties
could contain:Mostly all
JSF
implementations comes with the following default message, which I've copied fromMojarra
:It's also possible to define a required message to an single item with the
requiredMessage
attribute. Then your<h:inputText />
should look like this: