简体   繁体   English

JSF Setter方法无法在inputText形式下使用

[英]JSF Setter method not working in inputText form

I have the code below. 我有下面的代码。 When the user changes the data in the text fields (the correct information shows up, assumably using the getName, getAddress methods etc...) and then hits the save button, the user is taken back to mainPage. 当用户更改文本字段中的数据(显示正确的信息,大概使用getName,getAddress方法等),然后单击“保存”按钮时,用户将被带回到mainPage。 If I click on "Edit/View Personal..." and it reloads the editpersonal.xhtml page again, the original values are there, not the changed values. 如果单击“编辑/查看个人...”,然后它再次重新加载editpersonal.xhtml页面,则原始值在那里,而不是更改后的值。 I've checked the headers being sent in chrome and they are being sent so for whatever reason, they are not being set. 我已经检查了以chrome发送的标头,并且无论出于何种原因都未发送标头。 There exists a get and set method for each of the fields. 每个字段都有一个get and set方法。

I have almost identical code in other pages where I use inputText fields to SET information and it works without issue. 我在其他页面中使用inputText字段设置信息的代码几乎相同,并且可以正常工作。 I have no idea why this isnt working. 我不知道为什么这不起作用。

mainPage.xhtml (every function and command on this webpage works) mainPage.xhtml(此网页上的所有功能和命令均有效)

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
    <head>
        <title>TODO supply a title</title>
    </head>
    <body>
        <c:set var="userid" value="${CustomBuild.userid}"/>
        #{CustomBuild.getCustomerDetails(userid)}

        <c:set var="configs" value="#{CustomBuild.configs}"/>

        Welcome <h:outputText escape="false" value="#{CustomBuild.name}"/>
        <br></br>
        Please choose from one of the following options below.
        <br></br><br></br>

        <h:form>
                <h:commandButton id="editp" value="View/Edit Personal Information" action="editpersonal" />
                <br></br><br></br>
                <c:choose>
                    <c:when test="#{configs==0}">
                        <h:commandButton id="viewo" value="View Orders" action="vieworders" disabled="true"/>
                    </c:when>
                    <c:when test="#{configs!=0}">
                        <h:commandButton id="viewo" value="View Orders" action="vieworders" disabled="false"/>
                    </c:when>
                </c:choose>

                <br></br><br></br>
                <h:commandButton id="createo" value="Create New Order" action="createorder" />

        </h:form>
    </body>
</html>

editpersonal.xhtml <-- This page doesnt appear to be "saving" the data. editpersonal.xhtml <-此页面似乎没有“保存”数据。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <head>
        <title>TODO supply a title</title>
    </head>
    <body>
        <h:form>
            <h:panelGrid>
            <h:outputLabel value="Name: " style="font-weight:bold" />
            <h:inputText value="#{CustomBuild.name}" />
            <br></br>
            <h:outputLabel value="Address " style="font-weight:bold" />
            <h:inputText value="#{CustomBuild.address}" />
            <br></br>
            <h:outputLabel value="Phone Number: " style="font-weight:bold" />
            <h:inputText value="#{CustomBuild.phone}" />
            <br></br>
            <h:outputLabel value="Email Address: " style="font-weight:bold" />
            <h:inputText value="#{CustomBuild.email}" />
            <br></br>
            </h:panelGrid>
            <h:commandButton id="Save" value="save" action="mainPage" />
        </h:form>
        <h:form>
            <h:commandButton id="Cancel" value="cancel" action="mainPage" />
        </h:form>

    </body>
</html>

So I decided to try something. 所以我决定尝试一下。 My plan was to run a save to database function when hitting the save button. 我的计划是在单击“保存”按钮时运行“保存到数据库”功能。

Makes sense right? 有道理吧?

The function is below. 该功能如下。 Basically what is does is take the new data (which I know is being saved because I checked through the debugger) and save it back to the database. 基本上,要做的是获取新数据(由于通过调试器检查,因此我知道该数据已保存),然后将其保存回数据库。 My plan was then to grab fresh data from the database when that mainPage and edit page were loaded. 然后我的计划是在加载该mainPage和edit页面时从数据库中获取新数据。

This should definitely work. 这绝对应该工作。

However what I discovered was immediately after writing and testing the saveDetails function, the data in the inputFields is already being updated and shown. 但是,我发现是在编写和测试saveDetails函数之后立即发现的,inputFields中的数据已经被更新和显示。 So this is the solution I guess but I have no idea WHY it suddenly works and didnt before since nothing in this function should affect the data saved in the bean. 所以我猜这是解决方案,但是我不知道为什么它突然起作用并且以前没有起作用,因为此函数中的任何内容都不会影响保存在bean中的数据。

Basically the next step would have been to write another function that grabs fresh data from the database and fill the bean data with it so that it would always show the most current data, but before I could even do that, the input fields started updating properly. 基本上,下一步将是编写另一个函数,该函数从数据库中获取新数据并用它填充Bean数据,以便它始终显示最新数据,但在我什至不能这样做之前,输入字段就开始正确更新。

public String saveDetails() {
    try {
        int tempUserId = getUserid();
        String stringUserId = Integer.toString(tempUserId);
        String tempName = getName();
        String tempAddress = getAddress();
        String tempPhone = getPhone();
        String tempEmail = getEmail();

        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/TMA3C", "owner", "jsfdb");

        Statement stmt = con.createStatement();
        String query = "UPDATE APP.USERS SET FULLNAME = '" + tempName + "', ADDRESS = '" + tempAddress + "', PHONENUMBER = '" + tempPhone + "', "
                + "EMAIL = '" + tempEmail + "' WHERE USERID = " + stringUserId;
        System.out.println(query);
        stmt.executeUpdate(query);


    } catch (ClassNotFoundException ex) {
        Logger.getLogger(CustomBuild.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(CustomBuild.class.getName()).log(Level.SEVERE, null, ex);
    }

    return "mainPage";
}

Does anyone know WHY this suddenly solved this problem? 有谁知道为什么这突然解决了这个问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM