0

I have small question: I create commandButton linked to my onload function in SalesBean.java and I want to fill datatable with properties belong to logged user after click commandButton, but this approach not works for me, after click nothing happens (in debug mode breakpoints in SalesBean.onload are not reached). Do You know what's wrong here? Why SalesBean.onload() are not executed?

If i uncomment line in @PostConstruct - properties for specified in SalesBean user are displayed correctly, but I want to show properties for logged user, how to achieve that? Or maybe - how to execute SalesBean.onload() on page load? I read about Primeface's remoteCommand, this line is also in my sales.xhtml but table is still empty

This is my 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);
    }
}

and 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>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
mtmx
  • 827
  • 1
  • 10
  • 31
  • Have you tried wrapping your commands in a form? Moving the init logic on @PostConstruct method? Calling your remoteCommand onload of your h:body? There could be others ways ^^ – WoAiNii May 27 '20 at 17:35
  • Remove manual including jquery, remove the services, use a static arraylist for the data, remove all js, all css, effectivly create a [mcve]. And read https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value – Kukeltje May 27 '20 at 18:24
  • And in the postconstruct you can access the loggedIn user – Kukeltje May 27 '20 at 18:26
  • 2
    RequestScope can also be a problem, data will be lost after every request. Better use ViewScope. Instead of the RemoteCommand use @PostConstruct or a ViewAction. And check your browsers DeveloperTools for errors. – TomStroemer May 27 '20 at 18:43
  • with @ViewScope everything works properly, thanks – mtmx May 28 '20 at 12:45

0 Answers0