将更改后的值从inputText传递给bean

时间:2016-08-24 09:43:09

标签: jsf xhtml

我正在尝试将变量的变量值传递给bean方法:

<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>

我希望他在g.subjectg.mark的inputText中所做的更改会传递给gradesBean.edit(g)。当我尝试以某种方式这样做时,两个变量的原始值的相同值被传递。各个对象中有getter和setter方法

@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;
    }

}`

1 个答案:

答案 0 :(得分:1)

g.mark和g.subject已经调用了你的支持bean类中的相应setter(我假设存在公共setter getMark()和getSubject())。

由于 action =&#34;#{gradesBean.edit(g)}&#34; 在服务器上解析(首次显示页面时) ,它将具有原始值,而不是更改的值。无论如何,要查看更改的值,您可以使用ajax(f:ajax标记和相应的侦听器属性应该设置)。但你不必这样做。只需将处理程序更改为  行动=&#34;#{gradesBean.edit()}&#34; //没有争论 并从bean类实例中获取最新值。

如果你愿意,我可以更具体。