Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jsf 2 - How to prevent multiple composite components reset themselves on a JSF page?

I put this problem in a simple example, a composite component that calculates the sum of 2 inputs and prints the result in an outputText

Main JSF page:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ez="http://java.sun.com/jsf/composite/ezcomp/">
    <h:head></h:head>
    <h:body>
      <ez:Calculator />
      <br/>
      <br/>
      <ez:Calculator />
      <br/>
      <br/>
      <ez:Calculator />
    </h:body>
</html>

Composite component 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:composite="http://java.sun.com/jsf/composite"
  xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>This content will not be displayed</title>
    </h:head>
    <h:body>
    <composite:interface componentType="calculator">
    </composite:interface>

    <composite:implementation>
      <h:form>
      <h:inputText id="first" value="#{cc.firstNumber}" /> 
      <h:commandButton value="+" action="#{cc.sum}"/>
      <h:inputText id="second" value="#{cc.secondNumber}" /> 
      </h:form>
      <h:outputText id="result" value="#{cc.result}" />
  </composite:implementation>

</h:body>
</html>

Composite component backing bean:

package ez;


import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;

@FacesComponent("calculator")
public class Calculator extends UINamingContainer  {

    private Long firstNumber;
    private Long secondNumber;
    private Long result;

    public Calculator() {
    }

    @Override
    public String getFamily() { 
      return "javax.faces.NamingContainer"; 
    }

    public void setFirstNumber(String firstNumber) {
      this.firstNumber = Long.parseLong(firstNumber);
    }

    public String getFirstNumber() {
      if(firstNumber == null) {
        return null;
      }
      return firstNumber.toString();
    }

    public void setSecondNumber(String secondNumber) {
      this.secondNumber = Long.parseLong(secondNumber);
    }

    public String getSecondNumber() {
      if(secondNumber == null) {
        return null;
      }
      return secondNumber.toString();
    }

    public String getResult() {
      if(result == null) {
        return null;
      }
      return result.toString();
    }

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

    public void sum() {
      this.result = this.firstNumber + this.secondNumber;
    }
}

So, I have 3 Composite Components that all should do the same thing, but when I press a SUM button, after the server processes the request, the result is printed out on the page, but the other 2 components are cleared of their values.

How can I prevent this? How can I force it to retain those values?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UIComponent instances are recreated on every request, hereby losing all instance variables everytime. They basically act like request scoped managed beans, while you intend to have them in the view scope. You need to take view state saving into account on a per-attribute basis. This is normally by default already done for all attributes of #{cc.attrs}. So, if you can, just make use of it:

<cc:interface componentType="calculator">
    <cc:attribute name="firstNumber" type="java.lang.Long" />
    <cc:attribute name="secondNumber" type="java.lang.Long" />
</cc:interface>
<cc:implementation>
    <h:form>
        <h:inputText id="first" value="#{cc.attrs.firstNumber}" /> 
        <h:commandButton value="+" action="#{cc.sum}"/>
        <h:inputText id="second" value="#{cc.attrs.secondNumber}" /> 
    </h:form>
    <h:outputText id="result" value="#{cc.attrs.result}" />
</cc:implementation>

with just this (nullchecks omitted; I recommend to make use of required="true" on the inputs)

@FacesComponent("calculator")
public class Calculator extends UINamingContainer {

    public void sum() {
        Long firstNumber = (Long) getAttributes().get("firstNumber");
        Long secondNumber = (Long) getAttributes().get("secondNumber");
        getAttributes().put("result", firstNumber + secondNumber);
    }

}

Otherwise, you'd have to take state saving into account yourself by delegating all attribute getters/setters to UIComponent#getStateHelper(). Based on the very same Facelets code as you have, the entire backing component would look like this:

@FacesComponent("calculator")
public class Calculator extends UINamingContainer {

    public void sum() {
        setResult(getFirstNumber() + getSecondNumber());
    }

    public void setFirstNumber(Long firstNumber) {
        getStateHelper().put("firstNumber", firstNumber);
    }

    public Long getFirstNumber() {
        return (Long) getStateHelper().eval("firstNumber");
    }

    public void setSecondNumber(Long secondNumber) {
        getStateHelper().put("secondNumber", secondNumber);
    }

    public Long getSecondNumber() {
        return (Long) getStateHelper().eval("secondNumber");
    }

    public void setResult(Long result) {
        getStateHelper().put("result", result);
    }

    public Long getResult() {
        return (Long) getStateHelper().eval("result");
    }

}

See, no local variables anymore. Note that I also removed the need for those ugly manual String-Long conversions by just declaring the right getter return type and setter argument type. JSF/EL will do the conversion automagically based on default converters or custom Converters. As there's already a default one for Long, you don't need to provide a custom Converter.


Unrelated to the concrete problem, you can safely remove the getFamily() method. The UINamingContainer already provides exactly this. If you were implementing NamingContainer interface instead, then you'd indeed need to provide it yourself, but this is thus not the case here. The above backing component examples have it already removed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...