JSF 所有消息标记
2018-02-20 13:34 更新
JSF教程 - JSF所有消息标记
h:messages标记在与UI元素对应的一个地方显示所有消息。
以下JSF标记
<h:messages style="color:red;margin:8px;" />
如果输入的用户名超过20个字符,输入的密码小于5个字符。
<ul style="color:red;margin:8px;"> <li> UserName: Validation Error: Length is greater than allowable maximum of "20" </li> <li> Password: Validation Error: Length is less than allowable minimum of "5" </li> </ul>
标签属性
| 属性 | 描述 |
|---|---|
| id | 标签的标识 |
| binding | 引用在backing bean中使用的组件 |
| rendered | 布尔值; false将抑制渲染 |
| styleClass | 级联样式表(CSS)类名称 |
| for | 显示消息的组件ID |
| errorClass | 应用于错误消息的CSS类 |
| errorStyle | CSS样式应用于错误消息 |
| fatalClass | CSS类应用于致命消息 |
| fatalStyle | CSS样式应用于致命消息 |
| globalOnly | 仅显示全局消息的指令。 默认值:false |
| infoClass | CSS类应用于信息消息 |
| infoStyle | CSS样式应用于信息消息 |
| layout | 消息布局规范:表或列表 |
| showDetail | 确定是否显示邮件详细信息的布尔值。h:messages的默认值为false,h:message的默认值为true |
| showSummary | 确定是否显示消息摘要的布尔值。h:messages的默认值为true,h:message的值为false |
| tooltip | 一个布尔值,用于设置是否在工具提示中呈现消息详细信息;仅当showDetail和showSummary为true时才会呈现工具提示 |
| warnClass | 警告消息的CSS类 |
| warnStyle | CSS样式的警告消息 |
| style | 内联样式信息 |
| title | 用于辅助功能的标题。 浏览器通常为标题的值创建工具提示 |
例子
以下代码来自demo.xhtml。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:body>
<h:form>
<h:messages style="color:red;margin:8px;" />
<br />
<h:panelGrid columns="3">
Enter your username :
<h:inputText id="username" value="#{user.username}"
size="20" required="true"
label="UserName" >
<f:validateLength minimum="5" maximum="10" />
</h:inputText>
<h:message for="username" style="color:red" />
Enter your age :
<h:inputText id="age" value="#{user.age}"
size="20" required="true"
label="Age" >
<f:validateLongRange for="age" minimum="1" maximum="200" />
</h:inputText>
<h:message for="age" style="color:red" />
</h:panelGrid>
<h:commandButton value="Submit" action="result" />
</h:form>
</h:body>
</html>
下面的代码来自UserBean.java。
package cn.w3cschool.common;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{
private static final long serialVersionUID = 1L;
public String username;
public int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
以下代码来自result.xhtml。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
>
<h:body>
Username : #{user.username}
<br />
Age : #{user.age}
</h:body>
</html>
下载 Message_Tag.zip运行
将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹,并运行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成启动后,在浏览器地址栏中键入以下URL。
http://localhost:8080/simple-webapp/demo.xhtml
以上内容是否对您有帮助:

免费 AI IDE


更多建议: