1

<h:outputText>我正在尝试在<h:inputText>单击按钮或链接后做出改变。与此类似的情况:如何在 JSF 中构建“编辑”按钮并在 h:outputText 和 h:inputText 之间切换,但我想使用 PrimeFaces。

我认为这会很有用:http: //www.primefaces.org/showcase/ui/inplace.jsf 但我想让编辑模式在单击编辑按钮或超链接后触发,而不是在单击我想要的文本后改变。

当然,在单击编辑按钮后,我希望将更改为“接受”按钮,这将允许我保存更改并将 inputText 更改为 outputText

4

1 回答 1

2

看看这个SSCCE,它做你想做的事。

TestBean.java

package com.mycompany;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@ViewScoped
public class TestBean {

    /**
     * Controls if the input field is available or not
     */
    private boolean editable = false;

    /**
     * The String value you want to edit
     */
    private String value = "Default value";

    /**
     * Changes between the inputText and the outputText
     */
    public void changeEditable() {
        editable = !editable;
    }

    public String getValue() {
        return value;
    }

    public boolean isEditable() {
        return editable;
    }

    /**
     * Definitely saves the value
     */
    public void saveValue() {
        FacesMessage message = new FacesMessage("Value " + value + " saved!");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

索引.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:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>JSF Demo</title>
</h:head>
<h:body>
    <p:messages />
    <h:form>
        <h:panelGroup rendered="#{!testBean.editable}">
            <h:outputText value="#{testBean.value}" />
        </h:panelGroup>
        <h:panelGroup rendered="#{testBean.editable}">
            <p:inputText value="#{testBean.value}" />
        </h:panelGroup>

        <p:commandButton
            value="#{testBean.editable ? 'Confirm value' : 'Change value'}"
            update="@form" actionListener="#{testBean.changeEditable}" />

        <p:commandButton value="Save value" ajax="false"
            action="#{testBean.saveValue}" />
    </h:form>

</h:body>
</html>
于 2013-08-28T07:31:26.353 回答