加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

jsf中的ajax技术

发布时间:2020-12-16 01:32:15 所属栏目:百科 来源:网络整理
导读:在“jsf2入门demo”基础上使用ajax index.xhtml !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

在“jsf2入门demo”基础上使用ajax
index.xhtml

<!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" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich">
<h:head>
        <title>Welcome</title>
</h:head>
<h:body>
    <h:form>
        <h3>please enter your name and password</h3>
        <table>
            <tr>
                <td>Name:</td>
                <td><h:inputText value="#{user.name}" id="name"></h:inputText></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><h:inputSecret value="#{user.password}" id="password"></h:inputSecret></td>
            </tr>
        </table>
        <p>
            <h:commandButton value="Login">
                <f:ajax execute="name password" render="out"></f:ajax>
            </h:commandButton>
        </p>
        <h3><h:outputText id="out" value="#{user.greeting}"></h:outputText></h3>
    </h:form>
</h:body>
</html>

user Bean中增加

public String getGreeting() {
        if(name.length()==0)
            return " ";
        return "welcome to jsf2 + ajax " + name;
    }

当用户单击login时,表单并未提交,一个ajax请求发送到服务器
execute和render指定了组件id的列表。如同表单已经被提交一样处理execute组件。他们的值被发送到服务器,并更新相应的bean属性

ajax请求将输入组件添加到execute列表中,将输出组件添加到render列表中。对execute列表中的组件来说,会执行除“呈现响应”外的所有阶段,在“更新模型值”阶段,会更新模型bean,于此相反,对于render列表中的组件来说,仅执行生命周期的“呈现响应”阶段,结果被发回到ajax请求。 关于 jsf生命周期,参考博客中jsf分类下的“jsf架构”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读