实现效果 分类在set集合里不可排序 现要使商家拥有的相同的分来在同一列,需要把set集合里的元素放入list 但是问题是页面是双循环,必须以 . 的形式取集合元素所以需要把list集合声明到实体中,只做临时存储,所以写到dto类,这样就可排序了 我们用一个内部类Collections 的sort ( ....)方法来排序 , 这样内层循环就可以取到拍好序的元素,从而达到目的
jsp页面
<c:forEach items="${businessList}" var="businessDto"><c:forEach items="${businessDto.busniessAssortList}" var="busniessAssort" varStatus="_index"><a style="border:2px solid #DBDBDB;padding: 2px 4px;margin-left:3px;" title="修改" href="${pageContext.request.contextPath}/adminBusiness/toUpdateBusinessAssort.do?id=${businessDto.id}&busniessAssortDto.id=${busniessAssort.id}">${busniessAssort.name} </a><label style="font-size:20px;color:red;cursor: pointer;" οnclick="delBusinessAssortRef(${businessDto.id},${busniessAssort.id});" >⊗</label></c:forEach>
</c:forEach>
实体:
pojo 类
@Entity
@Table(name = "BUSINESS")
@JsonAutoDetect
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "shopkeeper",
"dishAssorts", "busniessAssorts", "empBusRefs", "orders",
"orderDetails", "userAccounts", "activities"})
public class Business implements java.io.Serializable {
//分类set
private Set<BusniessAssort> busniessAssorts = new HashSet<BusniessAssort>(0);
@ManyToMany(mappedBy = "businesss", cascade = { CascadeType.PERSIST })public Set<BusniessAssort> getBusniessAssorts() {return busniessAssorts;}public void setBusniessAssorts(Set<BusniessAssort> busniessAssorts) {this.busniessAssorts = busniessAssorts;}
/*
其他get/set方法
*/
}
dto 类
public class BusinessDto extends Business implements java.io.Serializable,Comparable<BusinessDto> {
//页面取这个集合即可得到处理后的元素顺序
private List<BusniessAssort> busniessAssortList = new ArrayList<BusniessAssort>();//排序
public List<BusniessAssort> getBusniessAssortList() {
return busniessAssortList;
}
public void setBusniessAssortList(List<BusniessAssort> busniessAssortList) {
this.busniessAssortList = busniessAssortList;
}
}
执行的 controllor 层方法
/*** for admin*/@RequestMapping("/toAdminBusniessAssort.do")public String toBusinessInfo(BusinessDto businessDto) {Pager pager = this.calculatePagedInfo();pager.setPageSize(pager.getPageSize()>20 ? pager.getPageSize() : 20); businessDto.setDishType(1L);//第一层排序 List<BusinessDto> businessList = businessService.getBusinessAllForAdmin(pager, businessDto);Collections.sort(businessList,new Comparator<BusinessDto>() {public int compare(BusinessDto o1, BusinessDto o2) { return o2.getId().compareTo(o1.getId());}});
第二层排序
for (BusinessDto businessDto2 : businessList) {//分类排序List<BusniessAssort> busniessAssortList = new ArrayList<BusniessAssort>();busniessAssortList.addAll(businessDto2.getBusniessAssorts());businessDto2.setBusniessAssortList(busniessAssortList);Collections.sort(businessDto2.getBusniessAssortList(),new Comparator<BusniessAssort>() {public int compare(BusniessAssort o1, BusniessAssort o2) { return o2.getId().compareTo(o1.getId());}});}if(businessDto!=null){if(StringUtils.hasText(businessDto.getName())){this.getRequest().setAttribute("name", businessDto.getName()); }}this.getRequest().setAttribute("businessList", businessList);//这里只能把businessList 传到页面 return "admin/business/adminBusAssortRef";}