为什么要迭代 ArrayList<> 中的最后一个数据?

Why iterate last data from ArrayList<>?

我完美地从数据库中获取数据并传递给 Thymeleaf(模板),但问题出在控制器中的 mdl.addAttribute("number" ,request.getNumber()) 附近,以检测 foreach 循环迭代中的最后一个值并通过模型发送

下面是我的代码:

Dto

public interface ProfileDto {
    public Integer getU_id();
    public Integer getP_id();
    public String getEmail();
    public String getUsername();
    public String getPassword();
    public String getContact();
    public String getDate();
    public String getProfile();
    public String getWebsite();
    public String getBio();
    public String getGender();
    public String getPost();
}

实体

@Entity
@Table(name = "request_master")
public class Request {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int req_id;
    private int sender_id;
    private int receiver_id;
    private String status;
    private String date;
    @Transient
    private int number;
    // getter setter

}

存储库

public interface profileRepo extends JpaRepository<Request, Integer> {
    @Query(nativeQuery = true, value = "SELECT * FROM registration_master rm INNER JOIN profile_master pm ON rm.u_id = pm.user_id WHERE rm.u_id != ?")
    List<ProfileDto> findByIdUser(Integer Id);

public interface requestRepo extends JpaRepository<Request, Integer> {
    @Query(nativeQuery = true,  value="SELECT * FROM request_master WHERE sender_id = ? and receiver_id = ?")
    List<Request> getSuggetionButton(Integer Sender_id, Integer Receiver_id);
}

服务

@Service
public class ServiceImpl implements Service {
    @Autowired
    private profileRepo profileRepo;

    @Autowired
    private requestRepo requestRepo;

    @Override
    public List<ProfileDto> getSuggestedList(Integer Id) {
        return this.profileRepo.findByIdUser(Id);
    }

    @Override
    public List<Request> getSuggestionButton(Integer Sender_id, Integer Receiver_id) {
        return this.requestRepo.getSuggetionButton(Sender_id, Receiver_id);
    }

}

控制器

@Controller
public class Controller {
    @Autowired
    private Service service;
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Model mdl, Request request) {
        int SessionId = Integer.parseInt(session.getAttribute("uid").toString());
        List<ProfileDto> Suggestion_list = service.getSuggestedList(SessionId);
        for(ProfileDto Suggestion_id : Suggestion_list)
        {
            List<Request> Friend_request = this.service.getSuggestionButton(SessionId, Suggestion_id.getU_id());
            if(Friend_request.size() > 0)
            {
                request.setNumber(Friend_request.size());
            }
            else
            {
                request.setNumber(0);
            }
        }
        mdl.addAttribute("number" ,request.getNumber());
        mdl.addAttribute("suggestionList", Suggestion_list);
        return "post";
        
    }
}

百里香叶

<div class="follow-user-list" th:each="suggetionFriend : ${suggestionList}">
    <div class="follow-user clearfix" th:id="'follow-user'+${suggetionFriend.u_id}">
        <img th:src="${suggetionFriend.profile}" alt="" class="profile-photo-sm pull-left" />
        <div class="name clearfix">
            <h5>
                <a href="#" class="follow-name" th:text="${suggetionFriend.username}"></a>
            </h5>
            <div class='follow-unfollow-btn' th:id="'follow-unfollow-button'+${suggetionFriend.u_id}">
                <div th:text="${number}">
                                
                </div>
            </div>
        </div>
    </div>
</div>

in below image 1 is for condition matched and find data and 0 is for condition not matched and not find data

In My output i can get only last iterate data in both user

输出:

预期输出:

我认为问题在于将数据从控制器传递到 thymeleaf

如果您有将价值从 Controller 转移到 Template 的好主意,请告诉我

你应该为每个 profile/user 维护 request 而不是单个 request,我的意思是你应该为每个 [=] 维护 request 15=],你可以维护 profileId/userId 的地图和 profile/user 的 request 的数量,并在你的 template 中使用该地图,尝试修改你的代码如下

控制器

@Controller
public class Controller {
    @Autowired
    private Service service;
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Model mdl, Request request) {
        Map<Integer, Integer> profileToNoOfRequestMap = new HashMap<>();
        int SessionId = Integer.parseInt(session.getAttribute("uid").toString());
        List<ProfileDto> Suggestion_list = service.getSuggestedList(SessionId);
        for(ProfileDto Suggestion_id : Suggestion_list)
        {
            List<Request> Friend_request = this.service.getSuggestionButton(SessionId, Suggestion_id.getU_id());

            profileToNoOfRequestMap.put(Suggestion_id.getU_id(), Friend_request.size());
        }
        mdl.addAttribute("profileToNoOfRequestMap", profileToNoOfRequestMap);
        mdl.addAttribute("suggestionList", Suggestion_list);
        return "post";
        
    }
}

百里香叶

<div class="follow-user-list" th:each="suggetionFriend : ${suggestionList}">
    <div class="follow-user clearfix" th:id="'follow-user'+${suggetionFriend.u_id}">
        <img th:src="${suggetionFriend.profile}" alt="" class="profile-photo-sm pull-left" />
        <div class="name clearfix">
            <h5>
                <a href="#" class="follow-name" th:text="${suggetionFriend.username}"></a>
            </h5>
            <div class='follow-unfollow-btn' th:id="'follow-unfollow-button'+${suggetionFriend.u_id}">
                <div th:text="${profileToNoOfRequestMap.get(suggetionFriend.u_id)}">
                                
                </div>
            </div>
        </div>
    </div>
</div>