【问题标题】:How to make jsf change what is rendering after button press using f:ajax?如何使用 f:ajax 使 jsf 更改按下按钮后呈现的内容?
【发布时间】:2014-06-04 12:15:24
【问题描述】:

我对渲染而不是渲染感到困惑。

首先,我需要输入登录密码和登录按钮。 然后,如果登录密码签出,我需要隐藏所有输入字段和“Enter”按钮,并显示另一个按钮以导航到另一个页面。

最好的方法是什么?

这是我目前所拥有的。

<h:form>
  <table>
    <h:panelGroup>
      <tr><td>Username : </td><td><h:inputText value="#{loginBean.username}"/></td></tr>
      <tr><td>Password : </td><td><h:inputSecret value="#{loginBean.password}"/></td></tr>
      <tr><td colspan="2">
        <h:commandButton value="Enter" action="#{loginBean.login}">
          <f:ajax event="change"  render="loggedin" listener="#{loginBean.handleEvent}" />
        </h:commandButton>
      </td></tr>
    </h:panelGroup>
    <tr><td colspan="2">
      <h:commandButton rendered="loginBean.login = false" id="loggedin" value="Go to the special page" action="logingo.xhtml"/>
    </td></tr>
  </table>
</h:form>

loginBean 的测试非常简单:

@Named(value = "loginBean")
@ManagedBean
@ViewScoped

public class LoginBean {

    public boolean result;
    private String username = "";
    private String password = "";
    public boolean output;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Boolean getOutput() {
        return output;
    }

    public void setoutput(String password) {
        this.output = output;
    }

    public void handleEvent(AjaxBehaviorEvent event) {
       if (result == true){ 
          output = true;
       }
       else output = false;
    }

    public boolean getResult() {
        return result;
    }

    public void setResult(String username) {
        this.result = result;
    }

    public void login() {
        if (this.username.isEmpty() == false && this.username.equalsIgnoreCase("11111")
                && this.password.isEmpty() == false && this.password.equals("11111")) {
            result = true;
        }
        result = false;
    }

【问题讨论】:

    标签: java ajax jsf rendering


    【解决方案1】:

    您的rendered 属性是正确的。但是你的语法有点不对。

    应该是这样的:

    rendered="#{loginBean.login}"
    

    当使用这个时,如果登录返回true 并且如果你希望条件检查为假,那么就会呈现,试试这个:

    rendered="#{!loginBean.login}"
    

    希望对您有所帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      相关资源
      最近更新 更多