单击按钮/页面加载后填充p:datatable

时间:2020-05-27 15:48:44

标签: spring jsf primefaces

我有一个小问题: 我在SalesBean.java中创建了链接到我的onload函数的commandButton,并且我想在单击commandButton之后用属于登录用户的属性填充数据表,但是这种方法对我不起作用,单击之后什么都没有发生(在调试模式下,SalesBean.onload中的断点是还没到)。 你知道这是怎么回事吗?为什么不执行SalesBean.onload()?

如果我在@PostConstruct中取消注释行-在SalesBean用户中指定的属性正确显示,但是我想显示已登录用户的属性,如何实现?或者也许-如何在页面加载时执行SalesBean.onload()?我读到有关Primeface的remoteCommand的信息,该行也在我的sales.xhtml中,但表仍然为空

这是我的SalesBean.java

package application.beans;

import application.model.views.AuctionView;
import application.service.AuctionViewService;
import application.service.UserService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.el.MethodExpression;
import javax.faces.application.FacesMessage;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
@RequestScoped
@Component
public class SalesBean implements Serializable {

    @Autowired
    private  AuctionViewService auctionViewService;

    @Autowired
    private UserService userService;

    private String firstName = "first";
    private String lastName = "last";
    private List<AuctionView> userProperty = new ArrayList<>();

    @PostConstruct
    public void init() {
        //userProperty = auctionViewService.findByEmail("seller@seller.com");
    }

    public void onload() {
        String userName = userService.getLoggedUser();
        userProperty = auctionViewService.findByEmail(userName);
    }
}

和sales.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
<h:head>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css"></link>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/main.css"></link>
    <script src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js" type=""></script>
    <script src="${pageContext.request.contextPath}/js/bootstrap.min.js" type=""></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/common/main.js"></script>
    <title>Sales</title>
</h:head>
<h:body>
<div id="wrapper">

    <div id="navigationMenuPlaceholder"></div>

    <!-- Page Content -->
    <div id="page-content-wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-12">
                    <p:panelGrid columns="2">
                        <h:outputText value="#{salesBean.firstName}"/>
                        <h:outputText value="#{salesBean.lastName}" />
                    </p:panelGrid>

                    <p:commandButton value="Get properties p" action="#{salesBean.onload}" style="margin-right:20px;" styleClass="ui-priority-primary" />
                    <p:remoteCommand name="onload" action="#{salesBean.onload}" autoRun="true" />

                    <p:dataTable var="property" value="#{salesBean.userProperty}">
                        <p:column headerText="PropertyId">
                            <h:outputText value="#{property.propertyId}" />
                        </p:column>

                        <p:column headerText="UserId">
                            <h:outputText value="#{property.userId}" />
                        </p:column>

                        <p:column headerText="Street">
                            <h:outputText value="#{property.street}" />
                        </p:column>

                        <p:column headerText="House no">
                            <h:outputText value="#{property.homeNumber}" />
                        </p:column>

                        <p:column headerText="Local no">
                            <h:outputText value="#{property.localNumber}" />
                        </p:column>

                        <p:column headerText="Post code">
                            <h:outputText value="#{property.postCode}" />
                        </p:column>

                        <p:column headerText="City">
                            <h:outputText value="#{property.city}" />
                        </p:column>

                        <p:column headerText="Price">
                            <h:outputText value="#{property.price}" />
                        </p:column>

                        <p:column headerText="Size">
                            <h:outputText value="#{property.size}" />
                        </p:column>

                    </p:dataTable>
                </div>
            </div>
        </div>
    </div>
    <div id="footerPlaceholder">
    </div>
    <!-- /#page-content-wrapper -->
</div>
</h:body>
</html>

0 个答案:

没有答案