JSF消息FacesMessage的使用

来源:互联网 发布:网络曝光是什么意思 编辑:程序博客网 时间:2024/06/11 22:11

在JSF生命周期中,任何组件都可以创建一个JSF消息,这个消息通常由FacesContext来添加,FacesContext会自动维护应用的消息队列,当JSF生命周期结束时,视图页面只要使用简单的<h:messages>或者<h:message>标签即可输出这些消息。

JSF支持4种级别的消息:
一般消息:FacesMessage.SEVERITY_INFO
警告消息:FacesMessage.SEVERITY_WARNING
错误消息:FacesMessage.SEVERITY_ERROR
致命错误:FacesMessage.SEVERITY_FATAL
如果要使用JSF消息,可以通过构造方法创建一个FacesMessage对象:
FacesMessage(severity, summary, detail)
severty:指定消息的级别
summary:指定消息的摘要
detail:指定消息的详细信息
下面来看一个注册的例子,如果密码的长度少于6位就在结果页面中提示错误:

首先是编写托管Bean UserBean:

public class UserBean {    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    /**   * 在setPassword方法中给输入的password进行判断并使用addMessage方法添加错误信息   * @param password   */    public void setPassword(String password) {        if(password.length() < 6){            FacesContext.getCurrentInstance().addMessage("password", new FacesMessage(FacesMessage.SEVERITY_FATAL, "密码错误", "密码长度必须大于6位数!"));        }        this.password = password;    }    /**   * 注册的方法,这里简单的返回success视图   * @return   */    public String regist(){        return "success";    }}

其中regist是需要使用的表单提交方法,这里只是简单的返回了seccess视图,即是结果页面,在setPassword方法中对password进行了判断,并在符合条件时添加错误信息到FacesContext中。

接下来在faces-config.xml中配置托管Bean:

<!-- 配置backing-bean --><managed-bean>    <managed-bean-name>userBean</managed-bean-name>    <managed-bean-class>com.itzhai.JSFMessage.UserBean</managed-bean-class>    <managed-bean-scope>request</managed-bean-scope></managed-bean>

接下来是编写register.jsp注册页面:

<f:view>    <h3>用户注册</h3>    <h:form id="loginForm">        用户名:<h:inputText value="#{userBean.username }"></h:inputText>        密码:<h:inputText id="password" value="#{userBean.password }"></h:inputText>        <h:commandButton action="#{userBean.regist }" value="登录"></h:commandButton>    </h:form></f:view>

最后是编写show.jsp结果页面:

<f:view>    <h:messages />注册结果<br />用户名:<h:outputText value="#{userBean.username }" /><br />密码:<h:outputText value="#{userBean.password }" /></f:view>

这里使用<h:message>标签输出了错误信息。

最后设置页面导航规则:

<navigation-rule>    <!-- 导航规则的输入页面 -->    <from-view-id>/JSFMessage/register.jsp</from-view-id>    <navigation-case>        <from-outcome>success</from-outcome>        <to-view-id>/JSFMessage/show.jsp</to-view-id>    </navigation-case></navigation-rule>

这样就可以在结果页面中输出错误信息了。

为了使得错误时不跳转到错误页面而是在注册页面显示信息,可以在setPassword()方法中跑出异常,这时应用将不会激发<h:commandButton>组件上的action属性绑定的userBean.regist方法,而是直接返回register.jsp页面,我们可以在setPassword()方法中这样处理:

public void setPassword(String password) {    if(password.length() < 6){        throw new RuntimeException("用户密码必须不少于6位!");    }    this.password = password;}

修改register.jsp页面

<f:view>    <h3>用户注册</h3>    <!-- 输出组件上的所有异常 -->    <h:messages />    <h:form id="loginForm">        用户名:<h:inputText value="#{userBean.username }"></h:inputText>        密码:<h:inputText id="password" value="#{userBean.password }"></h:inputText>        <!-- 输出password字段关联的异常信息 -->        <h:message for="password" />        <h:commandButton action="#{userBean.regist }" value="登录"></h:commandButton>    </h:form></f:view>

这样就可以在注册页面显示错误信息了。

除了文章中有特别说明,均为IT宅原创文章,转载请以链接形式注明出处。
本文链接:http://www.itzhai.com/jsf-notes-jsf-facesmessage-to-use-the-message.html
0 0
原创粉丝点击