Menu

Simple report of detail page

2025-09-22
2025-09-24
  • Frank Messie

    Frank Messie - 2025-09-22

    In Lesson 2 of the Jasperreport lessons we find the following text:
    "There are different ways to obtain the information, such as from the database or directly from the view where the action is called. We will choose the first option".

    Is it possible to provide the code to generate a report that contains some of the fields that are shown on the detail page which shows the action which calls the report.?
    (in stead of querying the fields from the database).
    Thanks in advance.

     
  • Javier Paniza

    Javier Paniza - 2025-09-24

    Hi Frank:

    In the lesson 2 you learn how to use parameters to send data to the report. You can fill the data from any place you want. In the example, it gets the data from the entity, but you can get the data from the view with the same effect.

    That is, in the action code change:

    protected Map getParameters() throws Exception {
        Messages errors = MapFacade.validate("Product", getView().getValues());
        if (errors.contains()) throw new ValidationException(errors);
    
        Map parameters = new HashMap(); 
        parameters.put("id", (getProduct().getNumber()));
        parameters.put("description", getProduct().getDescription());
        parameters.put("author", getProduct().getAuthor().getName());
        parameters.put("isbn", getProduct().getIsbn());
        parameters.put("category", getProduct().getCategory().getDescription());
        parameters.put("price", getProduct().getPrice());
    
        return parameters;
    }
    

    By:

    protected Map getParameters() throws Exception {        
        Map parameters = new HashMap(); 
        parameters.put("id", getView().getValue("number"));
        parameters.put("description", getView().getValue("product.description");
        parameters.put("author", getView().getValue("product.author.name"));
        parameters.put("isbn", getView().getValue("product.isbn"));
        parameters.put("category", getView().getValue(product.category.description"));
        parameters.put("price", getView().getValue("product.price"));
    
        return parameters;
    }
    

    That is, instead of getProduct().getXxx() use getView().getValue("xxx"). The caveat of this approach is that the filed must be displayed in the view. That is if you do:

    getView().getValue("product.isbn"):
    

    But product.isbn is not displayed, the isbn will not be populated, while using :

    getProduct().getIsbn();
    

    It will be populated even if not displayed.


    Help others in this forum as I help you.

     

Log in to post a comment.

MongoDB Logo MongoDB