How to Use Variables in JSF

0

My question is how to declare a variable to be able to do a total sub, and then perform a counter in JSF

As you investigate, this could be done:

<ui:param name="sub" value="#{item.canProductos * item.ideProducto.preProducto}" /> 

 <h:outputText value="#{sub}"/>

But it does not generate any results, not even an error.

After that I would need to add that variable sub and generate a total  something like that;

<!--TOTAL DE LA COMPRA-->
<ui:param name="total" value="#{ total += sub}" /> 
<h:outputText  value="#{total}"/>

I know that total += sub is not correct but how could I do it?

Thank you very much.

PDT: Also try this:

  

(the namespace first)

xmlns:c="http://java.sun.com/jstl/core"
  

then

<c:set var="varName" value="#{complexModel.currencyAmount}" />
And then the calculated value will be accessible through #{varName}.

But he did not recognize the route in the project.

    
asked by Andres Felipe Diaz 17.04.2017 в 01:56
source

1 answer

0

At the end of the day it is using a Managed Bean to access the variables, i.e. item.canProductos * item.ideProducto.preProducto

Why not let the Managed Bean do the calculation, instead of creating logic in the view?

Something like:

@ManagedBean
@ViewScoped
public class ProductosBacking {

    List<Item> items;
    long totalSub;
    long subShow;

    public ProductosBacking() {
        items = new ArrayList<>();
        items.add(new Item("Item a", 9, new IdeProducto(40)));
        items.add(new Item("Item b", 4, new IdeProducto(2)));
        items.add(new Item("Item c", 105, new IdeProducto(9)));
    }

    public long getSub(long canProductos, long preProducto) {
        long sub = canProductos * preProducto;

        totalSub += sub;
        System.out.printf("Sub %d \nTotalSub: %d", sub, totalSub);
        return sub;
    }

    public List<Item> getItems() {
        return items;
    }

    public long getTotalSub() {
        return totalSub;
    }

    public void updateVal() {}
}

And in the facet:

<h:dataTable value="#{productosBacking.items}" var="item" border="1" cellpadding="20px" cellspacing="10">
    <h:column>
        <f:facet name="header">Item Name</f:facet>
            #{item.nombre}
    </h:column>
    <h:column>
        <f:facet name="header">CanProducto</f:facet>
            #{item.canProductos}
    </h:column>
    <h:column>
        <f:facet name="header">PreProducto</f:facet>
            #{item.ideProducto.preProducto}
    </h:column>
    <h:column>
        <f:facet name="header">Sub</f:facet>
            #{productosBacking.getSub(item.canProductos, item.ideProducto.preProducto)}
    </h:column>
    <f:facet name="footer">
        <h:form>
            Total Sub: <h:outputText value="#{productosBacking.totalSub}" />
            <h:commandButton value="Calcular Sub" action="#{productosBacking.updateVal()}" style="float: right; ">
                <f:ajax render="@form" />
            </h:commandButton>
        </h:form>
    </f:facet>
</h:dataTable>   
    
answered by 22.04.2017 / 17:29
source