Noli
Noli

Reputation: 163

Pass changed value from inputText to bean

I'm trying to pass the changed value of the variable to a bean method:

<h:panelGroup rendered="#{! empty gradesBean.getAllGrades()}">
                <h:dataTable value="#{gradesBean.getAllGrades()}" var="g">
                    <h:column>
                        <f:facet name="header">#{msg['outputSubject']}</f:facet>
                        <h:inputText value="#{g.subject}" onchange="#{g.subject}" />
                    </h:column>
                    <h:column>
                        <f:facet name="header">#{msg['outputGrade']}</f:facet> 
                        <h:inputText value="#{g.mark}" onchange="#{g.mark}"/>
                    </h:column>
                    <h:column>
                        <h:form>
                            <h:commandButton value="#{msg['actionSave']}" action="#{gradesBean.edit(g)}" />
                        </h:form>
                    </h:column>
                </h:dataTable>
(.........)
</h:panelGroup>

I want the changes of the user he does in the inputText on g.subject and g.mark to be passed to gradesBean.edit(g). When I try to do that somehow the same values of the original values of the both variables are passed. There are getter and setter methods in the respective object

@Named
@ViewScoped
public class GradesBean extends AbstractBean implements Serializable {

    private static final long serialVersionUID = 320401008216711886L;

    private static final String NO_GRADES_PRESENT = "keine Noten     eingetragen";

    private static final Logger loggerLogger.getLogger(GradesBean.class);

    @Inject
    private transient GradeDAO gradeDAO;

    @Inject
    private UserDAO userDAO;

    private Grade grade;

    private List<Grade> allGrades;

    @PostConstruct
    public void init() {
        if (!isLoggedIn()) {
            return;
        }
        grade = new Grade();
        allGrades = getSession().getUser().getGrades();
    }

    public Grade getGrade() {
        return grade;
    }

    public List<Grade> getAllGrades() {
        return allGrades;
    }

    public String getGradeAverage() {
        final List<BigDecimal> theDecimals = new ArrayList<>(allGrades.size());
        for (final Grade g : allGrades) {
            theDecimals.add(g.getMark());
        }
        final Configuration config = Configuration.getDefault();
        final int scale = config.getScale();
        final RoundingMode roundingMode = config.getRoundingMode();
        try {
            final BigDecimal average = de.unibremen.st.gradelog.businesslogic.Math
                .average(theDecimals, scale, roundingMode);
        return average.stripTrailingZeros().toPlainString();
        } catch (final ArithmeticException e) {
            logger.debug(
                "Calculation of grade average has been called without any grades.", e);
        return NO_GRADES_PRESENT;
        }
    }

    public String getGradeMedian() {
        final List<BigDecimal> theDecimals = new ArrayList<>(allGrades.size());
        for (final Grade g : allGrades) {
            theDecimals.add(g.getMark());
        }
        try {
            final BigDecimal median = de.unibremen.st.gradelog.businesslogic.Math
                .median(theDecimals);
        return median.stripTrailingZeros().toPlainString();
        } catch (final ArithmeticException e) {
        logger.debug(
                "Calculation of grades median has been called without any grades.", e);
            return NO_GRADES_PRESENT;
        }
    }

    public String save() {
        if (!isLoggedIn()) {
            return null;
        }
        final User user = getSession().getUser();
        grade.setUser(user);
        user.addGrade(grade);
        gradeDAO.save(grade);
        try {
            userDAO.update(user);
        } catch (final DuplicateUniqueFieldException e) {
            throw new UnexpectedUniqueViolationException(e);
        }
        init();
        return null;
    }

    public String edit() {
        if (!isLoggedIn()) {
            return null;
        }
        assertNotNull(grade);
        final User user = getSession().getUser();
        gradeDAO.update(grade);
        try {
            userDAO.update(user);
        } catch (final DuplicateUniqueFieldException e) {
            throw new UnexpectedUniqueViolationException(e);
        }
        init();
        return null;
    }

    public String remove(final Grade theGrade) {
        if (!isLoggedIn()) {
            return null;
        }
        assertNotNull(theGrade);
        final User user = getSession().getUser();
        user.removeGrade(theGrade);
        gradeDAO.remove(theGrade);
        try {
            userDAO.update(user);
        } catch (final DuplicateUniqueFieldException e) {
            throw new UnexpectedUniqueViolationException(e);
        }
        init();
        return null;
    }

}`

Upvotes: 1

Views: 78

Answers (1)

dsp_user
dsp_user

Reputation: 2121

g.mark and g.subject will already call the corresponding setters on your backing bean class (I assume that public setters getMark() and getSubject() exist).

Since action="#{gradesBean.edit(g)}" is resolved at the server (when the page is first displayed), it will have the original values , not the changed values. Anyway , to see the changed values you can use ajax (f:ajax tag and the corresponding listener attribute should be set). But you don't have to do this. Simply change your handler to action="#{gradesBean.edit()}" //no argument and get the latest values from your bean class instance.

I could be more specific if you want.

Upvotes: 1

Related Questions