0

我正在为我的 webapp 使用 Primefaces 数据表,我想在单击<p:commandButton value="Add" actionListener="#{accountBean.testAdd}" update="accountTable" />添加新数据后重新加载不工作后重新加载数据表,我不知道错误是什么,但这是我的 bean:

@Named
@ViewScoped
public class AccountBean implements Serializable {

    private List<AccountModel> accountModels;
    private AccountModel accountModel = new AccountModel();

    @Inject
    transient private AccountServices accountServices;

    @PostConstruct
    public void init() {
        try {
            this.accountModels = accountServices.getAccountTable();
        } catch (JsonProcessingException | EndpointException ex) {
            Logger.getLogger(AccountBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void testAdd() {
        try {
            System.out.println("SUBMITTED!");
            accountModel.setBalance(BigDecimal.ZERO);
            accountModel.setAccountId("111");
            accountModel.setAllowNegativeBalance(true);
            accountModel.setName("Reload");
            accountModel.setBirthDate(LocalDate.now());
            accountModel.setAddress("Tasik Reload");
            accountServices.registerAccount(accountModel);
            System.out.println("END HERE");
        } catch (JsonProcessingException | EndpointException ex) {
            Logger.getLogger(AccountBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Setter Getter 
}

这是我的 index.xhtml

<h:form id="dataTableAccount">
    <div class="card">
        <p:commandButton value="Add" actionListener="#{accountBean.testAdd}" update="accountTable" />
        <p:dataTable id="accountTable" lazy="true" var="accountModel" value="#{lazyAccountView.lazyModel}" paginator="true" rows="10"
                     paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                     rowsPerPageTemplate="5,10,15">
            <p:column headerText="Account ID" sortBy="#{accountModel.accountId}" filterBy="#{accountModel.accountId}">
                <h:outputText value="#{accountModel.accountId}" />
            </p:column>

            <p:column headerText="Name" sortBy="#{accountModel.name}" filterBy="#{accountModel.name}">
                <h:outputText value="#{accountModel.name}"  />
            </p:column>

            <p:column headerText="Balance">
                <h:outputText value="#{accountModel.balance}" />
            </p:column>

            <p:column headerText="Allow Negative Balance" sortBy="#{accountModel.allowNegativeBalance}" filterBy="#{accountModel.allowNegativeBalance}">
                <h:outputText value="#{accountModel.allowNegativeBalance}" />
            </p:column>
        </p:dataTable>
    </div>
</h:form>

这是我的POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.probation1</groupId>
    <artifactId>Mockva-UI-JSF</artifactId>
    <version>1</version>
    <packaging>war</packaging>
    <name>Mockva-UI-JSF</name>
    
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <jakartaee>8.0</jakartaee>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>10.0.0-RC2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.2</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.12.2</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-client</artifactId>
            <version>9.4.7.v20170914</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${jakartaee}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
     <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-api</artifactId>
                                    <version>${jakartaee}</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我的帐户模型:

public class AccountModel implements Serializable{
    
    public String accountId;
    public String name;
    public String address;
    @JsonFormat(pattern = Json.JSON_FORMAT_DATE)
    @Temporal(TemporalType.DATE)
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    public LocalDate birthDate;
    public boolean allowNegativeBalance;
    public BigDecimal balance;
    
    // Setter Getter

}

还有我的 AccountServices

@Named
@ApplicationScoped
public class AccountServices {

    @Inject
    private RestMockvaEndpoint restMockvaEndpoint;

    public AccountModel registerAccount(AccountModel accountModel) throws JsonProcessingException, EndpointException {
        AccountModel account = new JsonResponseReader(restMockvaEndpoint.send(new EndpointRequestBuilder()
                .method("POST")
                .content(Json.getWriter().writeValueAsBytes(accountModel), MediaType.APPLICATION_JSON)
                .resource("/account")
                .build())).getContentObject(AccountModel.class);
        return account;
    }
}

任何想法?

已编辑:当我单击按钮时,顺便说一句,我的日志上没有任何错误,但数据表仍未重新加载。

4

1 回答 1

0

通过调用this.accountModels = accountServices.getAccountTable();解决testAdd()。谢谢大家!我很感激你。

于 2021-04-19T01:46:45.053 回答