richfaces 的信息提示(尽可能多的方式)

大家如果有什么好的方法,分享出来哦

jsf 本身很强大,很多功能都实现了,程序员只需要调用它的组件,BUT虽然jsf有很多组件,有时的确不满足当前需求,比如信息提示,下面列出最常用的3种。
1)直接alert (简单了吧,嘿嘿)
2)用模态窗口来输出信息 (这个有点难)
3)在页面的一个专门的div用ajax来输出信息

jsf从后台传给前台的信息有两种方式,在本文中用第二种方式
a)先把信息设置到jsf的Bean中,再在jsf页面中用el取出来就是
b)用jsf的message组件
后台

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("保存成功"));

页面

<rich:messages ></rich:messages>

好了,现在开始用richfaces来实现信息提示
通过自己的理解加g.cn 加 baidu.com 其实richfaces里用alert是最难的,所以把alert放在最后吧,倒是2),3)实现起来比较方便
1)先来模态窗口

首先是userBean

@ManagedBean(name = "userBean")
@SessionScoped
public class UserBean implements Serializable {
private String name;
private String password;

public void login(ActionEvent event){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(name + " login 成功"));
}

public String getName() {
return name;
}

public void setName(String newValue) {
name = newValue;
}

public String getPassword() {
return password;
}

public void setPassword(String newValue) {
password = newValue;
}
}

然后是页面

<body>
<rich:modalPanel id="messagePanel" width="350" height="100" >
<f:facet name="header">
<h:panelGroup>
<h:outputText value="Modal Panel"></h:outputText>
</h:panelGroup>
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/images/close.png" styleClass="hidelink" id="hidelink"/>
<rich:componentControl for="messagePanel" attachTo="hidelink" operation="hide" event="onclick"/>
</h:panelGroup>
</f:facet>
<h:panelGroup >
<rich:messages ></rich:messages>
</h:panelGroup>
</rich:modalPanel>
<h:form>
<h3>Please enter your name and password.</h3>
<table>
<tr>
<td>Name:</td>
<td><h:inputText value="#{userBean.name}"/></td>
</tr>
<tr>
<td>Password:</td>
</tr>
</table>
<p><a4j:commandButton value="Login" actionListener="#{userBean.login}" reRender="messagePanel" oncomplete="#{rich:component('messagePanel')}.show()"></a4j:commandButton></p>
</h:form>
</body>

效果图如下

[img]http://dl.iteye.com/upload/attachment/371564/5888dceb-9f68-3568-81f7-83ac12a5be70.jpg[/img]

but如果每个按钮都写oncomplete="#{rich:component('messagePanel')}.show()"成这样就太复杂了所以就有了升级版

<body>
<h:panelGroup id="messageGroup" >
<rich:modalPanel id="messagePanel" width="350" height="100" showWhenRendered="true" rendered="#{!empty facesContext.maximumSeverity}">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="Modal Panel"></h:outputText>
</h:panelGroup>
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/images/close.png" styleClass="hidelink" id="hidelink"/>
<rich:componentControl for="messagePanel" attachTo="hidelink" operation="hide" event="onclick"/>
</h:panelGroup>
</f:facet>
<h:panelGroup >
<rich:messages ></rich:messages>
</h:panelGroup>
</rich:modalPanel>
</h:panelGroup>
<h:form>
<h3>Please enter your name and password.</h3>
<table>
<tr>
<td>Name:</td>
<td><h:inputText value="#{userBean.name}"/></td>
</tr>
<tr>
<td>Password:</td>
</tr>
</table>
<p><a4j:commandButton value="Login" actionListener="#{userBean.login}" reRender="messageGroup" ></a4j:commandButton></p>
</h:form>
</body>

2)专门的div用ajax来输出信息,这个在richfaces里简直是太简单了

<body>
<h:form>
<h3>Please enter your name and password.</h3>
<table>
<tr>
<td>Name:</td>
<td><h:inputText value="#{userBean.name}"/></td>
</tr>
<tr>
<td>Password:</td>
</tr>
</table>
<p><a4j:commandButton value="ajaxLogin" actionListener="#{userBean.login}" ></a4j:commandButton></p>
<h:panelGroup >
<rich:messages ></rich:messages>
</h:panelGroup>
</h:form>
</body>

效果图为

[img]http://dl.iteye.com/upload/attachment/371607/bd01cff6-5631-3777-be07-c0754c454172.jpg[/img]
先就写到这吧有时间了再写
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值