当前位置: 首页 > 知识库问答 >
问题:

更新和流程在JSF 2.0 Primefaces 3.4中不能正常工作

冯阳成
2023-03-14

在这个页面中,我将让用户通过点击一个复选框使密码可见。实际上,两个输入(passwordconpassword)应该隐藏,另一个输入(passwordV)应该显示。所有这3个输入具有相同的值,并且当用户在这两种状态之间切换时,需要保持它们的值:(有两个可见的秘密字段或有一个纯文本字段)

JSF页面:

<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./templates/main_template.xhtml"
                xmlns:p="http://primefaces.org/ui"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns="http://www.w3.org/1999/xhtml">

  <ui:define name="subTitle">
    :: #{lbls.newEntry}   </ui:define>

  <ui:define name="content">
    <p:panel rendered="#{current.loggedIn}" header="#{lbls.newEntry}" >
      <h:form id="frmEntry">
        <h:panelGrid columns="3">

          <h:panelGroup>
            <h:outputLabel for="title" value="#{lbls.title}:"/>
            <p:focus for="title"/>
          </h:panelGroup>
          <p:inputText id="title" value="#{entry.passwordEntry.title}" maxlength="100" label="#{lbls.title}" required="true"/>
          <p:message for="title"/>

          <h:outputLabel for="description" value="#{lbls.description}:"/>
          <p:inputTextarea id="description" value="#{entry.passwordEntry.description}" maxlength="500" rows="3" cols="40" label="#{lbls.description}"/>
          <p:message for="description"/>

          <p:spacer height="10"/>
          <p:spacer height="10"/>
          <p:spacer height="10"/>

          <h:outputLabel for="username" value="#{lbls.username}:"/>
          <p:inputText id="username" value="#{entry.passwordEntry.username}" maxlength="100" label="#{lbls.username}"/>
          <p:message for="username"/>          

          <h:outputLabel for="password" id="lblPassword" value="#{lbls.password}:"  styleClass="#{entry.showPasswords ? 'none' : ''}" />  
          <p:password id="password" feedback="true" value="#{entry.passwordEntry.password}" match="conPassword" maxlength="100" 
                      label="#{lbls.password}" promptLabel="#{lbls.strengthPromp}" weakLabel="#{lbls.weakPassword}"  
                      goodLabel="#{lbls.goodPassword}" strongLabel="#{lbls.strongPassword}" styleClass="#{entry.showPasswords ? 'none' : ''}"
                      />
          <p:message id="msgPassword" for="password" class="#{entry.showPasswords ? 'none' : ''}"/>

          <h:outputLabel id="lblConPassword" for="conPassword" value="#{lbls.conPassword}:"
                         styleClass="#{entry.showPasswords ? 'none' : ''}"/>  
          <p:password id="conPassword" value="#{entry.passwordEntry.password}" label="#{lbls.conPassword}" maxlength="100"
                      styleClass="#{entry.showPasswords ? 'none' : ''}"/>
          <p:message id="msgConPassword" for="conPassword" class="display: #{!entry.showPasswords ? 'none' : ''}"/>

          <h:outputLabel id="lblPasswordV" for="passwordV" value="#{lbls.password}:" 
                         styleClass="#{!entry.showPasswords ? 'none' : ''}"/>  
          <p:inputText id="passwordV" value="#{entry.passwordEntry.password}"  maxlength="100" 
                       label="#{lbls.password}" 
                       styleClass="#{!entry.showPasswords ? 'none' : ''}"/>
          <p:message id="msgPasswordV" for="passwordV" 
                     class="#{!entry.showPasswords ? 'none' : ''}"/>

          <h:outputLabel for="showPasswords" value="#{lbls.showPasswords}:"/>
          <p:selectBooleanCheckbox id="showPasswords" label="#{lbls.showPasswords}" value="#{entry.showPasswords}">  
            <p:ajax process="password passwordV conPassword" update="password passwordV conPassword lblPassword lblPasswordV lblConPassword msgPassword msgConPassword msgPasswordV"/>
          </p:selectBooleanCheckbox>
          <h:outputText/>

          <p:spacer height="10"/>
          <p:spacer height="10"/>
          <p:spacer height="10"/>

          <h:outputLabel for="url" value="#{lbls.url}:"/>
          <p:inputText id="url" value="#{entry.passwordEntry.url}" maxlength="255" label="#{lbls.url}"/>
          <p:message for="url"/>

          <h:outputLabel for="ip" value="#{lbls.ip}:"/>
          <p:inputText id="ip" value="#{entry.passwordEntry.ip}" maxlength="255" label="#{lbls.ip}"/>
          <p:message for="ip"/>

          <p:spacer height="10"/>
          <p:spacer height="10"/>
          <p:spacer height="10"/>

          <h:outputLabel for="tags" value="#{lbls.tags}:"/>
          <p:autoComplete id="tags" value="#{entry.selectedTags}" 
                          completeMethod="#{entry.selectTag}" converter="PasswordEntry" multiple="true"
                          var="tag" itemLabel="#{tag.title}" itemValue="#{tag}" />  
          <p:message for="tags"/>

          <p:spacer height="10"/>
          <p:spacer height="10"/>
          <p:spacer height="10"/>

          <h:outputText/>
          <h:panelGroup layout="block" styleClass="right-align">
            <p:commandButton value="#{lbls.save}" actionListener="#{entry.save(event)}" 
                             update=":growl messages"/>
          </h:panelGroup>


          <f:facet name="footer">
            <p:messages id="messages"/>
          </f:facet>
        </h:panelGrid>
      </h:form>
    </p:panel>

    <ui:include src="/templates/not_logged_in.xhtml" rendered="!#{current.loggedIn}"/>   </ui:define>

</ui:composition>

和Bean:

package package;


@ManagedBean(name = "entry")
@ViewScoped
public class PasswordEntryBean implements Serializable {

  //<editor-fold defaultstate="collapsed" desc="FIELDS">   
  private static final Logger logger = LogUtil.getLogger(PasswordEntryBean.class);
  private PasswordEntry passwordEntry;
  @ManagedProperty(value = "#{current}")
  private CurrentSessionBean current;
  private Database database;
  private List<PasswordTag> selectedTags = new ArrayList<PasswordTag>();
  private Set<PasswordTag> tags;
  private boolean showPasswords;
  //</editor-fold>  

  //<editor-fold defaultstate="collapsed" desc="CONSTRUCTORS">    
  public PasswordEntryBean() {
    passwordEntry = new PasswordEntry();
  }

  @PostConstruct
  public void init() {
  }
  //</editor-fold>

  //<editor-fold defaultstate="collapsed" desc="PROPERTIES">    
  public PasswordEntry getPasswordEntry() {
    return passwordEntry;
  }

  public Database getDatabase() {
    return database;
  }

  public boolean getShowPasswords() {
    return showPasswords;
  }

  public void setShowPasswords(boolean showPasswords) {
    this.showPasswords = showPasswords;
  }

  public void setDatabase(Database database) {
    this.database = database;
  }

  public Set<PasswordTag> getTags() {
    return tags;
  }

  public void setTags(Set<PasswordTag> tags) {
    this.tags = tags;
  }

  public List<PasswordTag> getSelectedTags() {
    return selectedTags;
  }

  public void setSelectedTags(List<PasswordTag> selectedTags) {
    this.selectedTags = selectedTags;
  }

  public void setPasswordEntry(PasswordEntry passwordEntry) {
    this.passwordEntry = passwordEntry;
  }

  public CurrentSessionBean getCurrent() {
    return current;
  }

  public void setCurrent(CurrentSessionBean current) {
    this.current = current;
  }
  //</editor-fold>
}

更新的代码

<?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:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
  <h:head>
    <title>Facelet Title</title>
    <style type="text/css">
      .none {
        display: none;
      }
    </style>
  </h:head>
  <h:body>
    <h1>Register</h1>
    <h:form id="frmRegistration">
      <h:panelGrid columns="3">
        <h:outputLabel value="Username:" for="username"/>
        <p:inputText label="username" id="username" value="#{testBean.username}" required="true"/>
        <p:message for="username"/>

        <h:outputLabel value="Password:" id="lblPassword" for="password" styleClass="#{!testBean.visiblePassword ? '' : 'none'}"/>
        <p:password label="password" id="password" value="#{testBean.password}" 
                        styleClass="#{!testBean.visiblePassword ? '' : 'none'}"/>
        <p:message for="password" id="msgPassword" class="#{!testBean.visiblePassword ? '' : 'none'}"/>

        <h:outputLabel value="Confirm Password:" id="lblCpassword" for="cpassword" styleClass="#{!testBean.visiblePassword ? '' : 'none'}"/>
        <p:password label="confirm password" id="cpassword" value="#{testBean.password}" 
                        styleClass="#{!testBean.visiblePassword ? '' : 'none'}"/>
        <p:message for="cpassword" id="msgCpassword" class="#{!testBean.visiblePassword ? '' : 'none'}"/>

        <h:outputLabel value="Password:" id="lblVpassword" for="vpassword" styleClass="#{testBean.visiblePassword ? '' : 'none'}"/>
        <p:inputText label="password" id="vpassword" value="#{testBean.password}" 
                        styleClass="#{testBean.visiblePassword ? '' : 'none'}"/>
        <p:message for="vpassword" id="msgVpassword" class="#{testBean.visiblePassword ? '' : 'none'}"/>

        <h:outputLabel value="Show password"/>
        <p:selectBooleanButton value="#{testBean.visiblePassword}" 
                               onLabel="Yes" offLabel="No">
          <p:ajax update="messages password cpassword vpassword lblPassword lblCpassword lblVpassword msgPassword msgCpassword msgVpassword"
                   process="messages password cpassword vpassword" listener="#{testBean.addMessage}" />
        </p:selectBooleanButton>


        <f:facet name="footer">
          <p:commandButton actionListener="#{testBean.save(event)}" value="Save" update="messages"/>
          <p:messages id="messages"/>
        </f:facet>
      </h:panelGrid>
    </h:form>
  </h:body>
</html>
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

@ManagedBean
@ViewScoped
public class TestBean {

  private String username;
  private String password;
  private boolean visiblePassword;

  public void addMessage() {
    String summary = visiblePassword ? "Checked" : "Unchecked";

    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(summary));
  }

  public TestBean() {
  }

  public void save(ActionEvent event) {
  }

  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 isVisiblePassword() {
    return visiblePassword;
  }

  public void setVisiblePassword(boolean visiblePassword) {
    this.visiblePassword = visiblePassword;
  }
}

第二次更新

我使用了redisplay,空值的问题得到了解决,但是输入仍然不会隐藏/显示,除非我将updateprocess设置为@form,这对我的情况不利。

<?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:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
  <h:head>
    <title>Facelet Title</title>
    <style type="text/css">
      .none {
        display: none;
      }
    </style>
  </h:head>
  <h:body>
    <h1>Register</h1>
    <h:form id="frmRegistration">
      <h:panelGrid columns="3">
        <h:outputLabel value="Username:" for="username"/>
        <p:inputText label="username" id="username" value="#{testBean.username}" required="true"/>
        <p:message for="username"/>

        <h:outputLabel value="Password:" id="lblPassword" for="password" rendered="#{!testBean.visiblePassword}"/>
        <p:password redisplay="true" label="password" id="password" value="#{testBean.password}" 
                        rendered="#{!testBean.visiblePassword}"/>
        <p:message for="password" id="msgPassword" rendered="#{!testBean.visiblePassword}"/>

        <h:outputLabel value="Confirm Password:" id="lblCpassword" for="cpassword" rendered="#{!testBean.visiblePassword}"/>
        <p:password redisplay="true" label="confirm password" id="cpassword" value="#{testBean.password}" 
                        rendered="#{!testBean.visiblePassword}"/>
        <p:message for="cpassword" id="msgCpassword" rendered="#{!testBean.visiblePassword}"/>

        <h:outputLabel value="Password:" id="lblVpassword" for="vpassword" rendered="#{testBean.visiblePassword}"/>
        <p:inputText label="password" id="vpassword" value="#{testBean.password}" 
                        rendered="#{testBean.visiblePassword}"/>
        <p:message for="vpassword" id="msgVpassword" rendered="#{testBean.visiblePassword}"/>

        <h:outputLabel value="Show password"/>
        <p:selectBooleanButton value="#{testBean.visiblePassword}" 
                               onLabel="Yes" offLabel="No">
          <p:ajax update="messages password cpassword vpassword lblPassword lblCpassword lblVpassword msgPassword msgCpassword msgVpassword"
                   process="password cpassword vpassword" listener="#{testBean.addMessage}" />
        </p:selectBooleanButton>


        <f:facet name="footer">
          <p:commandButton actionListener="#{testBean.save(event)}" value="Save" update="messages"/>
          <p:messages id="messages"/>
        </f:facet>
      </h:panelGrid>
    </h:form>
  </h:body>
</html>

共有1个答案

狄元魁
2023-03-14

您的主要错误是在客户端使用CSS切换输入字段的可见性,而不是在服务器端使用JSF。所以JSF基本上从不知道显示/隐藏哪一个。它只知道两个字段都显示了。因此它将处理这两个字段。当您将两个字段的值绑定到同一属性时,它最终总是获得最后一个处理字段的值。

您需要在服务器端使用JSF显示/隐藏输入字段。您可以使用thereforProvided的rendered属性。

rendered="#{testBean.visiblePassword}"
 类似资料:
  • 这是我的DUsers类: 正如您所看到的,我定义了几个,除了最后一个需要我的数据库之外,它们都正常工作。为了运行这个查询,我定义了两个函数: 在此之后,我定义了一个路径,后面跟着一个POST请求,该请求应该更新我的数据库,但遗憾的是,它没有更新我的数据库。 有人知道我哪里错了吗?

  • 修改文件,将它们更新的内容添加到索引中. $ git add file1 file2 file3 你现在为commit做好了准备,你可以使用 git diff 命令再加上 --cached 参数 ,看看哪些文件将被提交(commit)。 $ git diff --cached (如果没有--cached参数,git diff 会显示当前你所有已做的但没有加入到索引里的修改.) 你也可以用git

  • 我正在处理碎片,所以在我的视图寻呼机中有三个碎片 因此,我在第一个片段中使用了方法,所以当我从一个片段切换到另一个片段并返回到第一个片段时,我的方法不起作用 所以有人给出解决方案吗

  • 我正试图在按下某个按钮时弹出一个警报对话框。我首先使用了Android Developer的示例代码而不是'这不起作用,所以我根据在这个站点上发现的情况进行了更改,但是现在我的程序在按下按钮后被迫停止。 就你的知识而言,这是在第二个不同于主要的活动中完成的。不确定这是否重要.... ‘ 碰撞日志:“03-25 19:34:24.373:E/AndroidRuntime(18828):致命异常:ma

  • 2,错误{org.apache.directory.server.LDAP.ldapserver}-ERR_171无法将LDAP服务(10,389)绑定到服务注册表。java.net.BindException:已在使用的地址 请帮忙谢谢 --------提示------------------- JAVA_HOME环境变量设置为/opt/java CARBON_HOME环境变量设置为/mnt/1

  • 当我试图从我的web服务器获取SOAP响应时,我的小部件更新服务抛出了一个:“android.os.NetworkOnMainThreadException”。 我确信SOAP代码没有错误,因为我只通过IntentService而不是服务在应用程序中使用相同的代码。 我根据最后一个示例设计了我的小部件:http://www.vogella.com/tutorials/AndroidWidgets/