Manipulate a List inside a foreach in a view

1

I have a doubt that it should be simple to solve, but I am not able to develop a logic, or even be doing the wrong thing.

Well, I have a table in which I scroll through a table called "categories" within this table. I have a relationship with another named "targets" as you can see below in the bean:

Destination.java

@Entity
@Table(name="destination")
@Multitenant
@TenantDiscriminatorColumn(name="tenant_id", discriminatorType=DiscriminatorType.INTEGER, contextProperty=PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
@NamedQuery(name="Destination.findAll", query="SELECT d FROM Destination d")
public class Destination implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_destination")
    private Long idDestination;

    @Column(name="tenant_id", insertable=false, updatable=false)
    private Long tenantId;

    @Column(name="appear_website")
    private Boolean dtAppearWebsite;

    @Lob
    @Column(name="description")
    private String dtDescription;

    @Column(name="highlight_website")
    private Boolean dtHighlightWebsite;

    @Column(name="name")
    private String dtName;

    @OneToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="fk_streetview")
    private StreetView streetView;

    @OneToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="fk_video")
    private Video video;

    @ManyToOne(cascade=CascadeType.REFRESH)
    @JoinColumn(name="fk_category")
    private Category categories;


    //Getters and Setters

Category.java

@Entity
@Table(name="category")
@Multitenant
@TenantDiscriminatorColumn(name="tenant_id", discriminatorType=DiscriminatorType.INTEGER, contextProperty=PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
@NamedQuery(name="Category.findAll", query="SELECT c FROM Category c")
public class Category implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_category")
    private Long idCategory;

    @Column(name="ct_active")
    private int ctActive;

    @Column(name="ct_name")
    private String ctName;

    @Column(name="tenant_id", insertable=false, updatable=false)
    private Long tenantId;

    @OneToMany(mappedBy="categories")
    private Set<Destination> destination;

    //Getters and Setters

Right! In my view I have a field in which I displayed the number of destinations linked to each category:

<c:forEach items="${category}" var="c">
    <tr>
        <td><a href="editCategory?id=${c.idCategory}">${c.ctName}</a></td>
        <!-- Campo no qual o numero de destinos é exibido por categoria-->
        <td>${categoryHasDestination}</td>
        <c:choose>
            <c:when test="${c.ctActive=='1'}">
                <td><span class="label label-success">Ativo</span></td>
            </c:when>
            <c:otherwise>
                <td><span class="label label-warning">Desativo</span></td>
            </c:otherwise>
        </c:choose>
    </tr>
</c:forEach>

So I've created a method that returns me the categories and also a list of the number of linked destinations in each category.

@RequestMapping("category")
public String getMenuCategory(Model mv) {
    List<Category> category = dashboardFacade.getCategoryList();
    List<Integer> size = new ArrayList<Integer>();

    for (Category categories : category) {
        Set<Destination> destinationList = categories.getDestination();
        int s = destinationList.size();
        size.add(s);
    }

    /Retorna uma lista com o numero de destinos para cada categoria
    mv.addAttribute("categoryHasDestination", size);

    //Me retorna todas as categorias
    mv.addAttribute("category", category);
    return "category/categoryList";
}

The problem is that in my view I have a foreach, that is, for each record the list is returned and not the number itself. The image below illustrates what I mean:

Thank you all for the help

Hugs!

    
asked by anonymous 13.08.2014 / 21:15

2 answers

0

Create a getDestinationSize method in your Category class that returns the size of the destination attribute.

Then in your JSP, simply access the ${c.destinationSize} method.

    
13.08.2014 / 23:44
0

Using forEach of jstl there is the varStatus attribute that exposes an object of type javax.servlet.jsp.jstl.core.LoopTagStatus in the iteration scope, which provides some data about the object being iterated.

In this case, I am using index to index your list of categoryHasDestination to get the data you need.

<c:forEach items="${category}" var="c" varStatus="status">
    <tr>
        <td><a href="editCategory?id=${c.idCategory}">${c.ctName}</a></td>
        <!-- Campo no qual o numero de destinos é exibido por categoria-->
        <td>${categoryHasDestination[status.index]}</td>
        <c:choose>
            <c:when test="${c.ctActive=='1'}">
                <td><span class="label label-success">Ativo</span></td>
            </c:when>
            <c:otherwise>
                <td><span class="label label-warning">Desativo</span></td>
            </c:otherwise>
        </c:choose>
    </tr>
</c:forEach>

To see the other attributes at a glance in the

14.08.2014 / 01:08