📄 f06a2496d59b001b10338a07bc32b2be
字号:
package com.tangjun.web.vo;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import com.tangjun.model.exception.MyException;
import com.tangjun.web.pojo.CD;
import javax.faces.context.FacesContext;
import org.apache.commons.beanutils.BeanUtils;
public class CDBean extends CDBaseBean
{
private DataModel cds = new ListDataModel();
private static List temp;
// private List<Map<String,CD>> items = new ArrayList<Map<String,CD>>();
// private Map<String,CD> itemMap = new TreeMap<String,CD>();
// private CD cd = new CD();
private String sortByProperty = "specialTitle";
public DataModel getCds() throws MyException
{
if(temp==null)
{
temp = this.getCdService().browse();
this.sortTitleAsc();
}
return cds;
}
public void setCds(DataModel cds)
{
this.cds = cds;
}
/**
* 以下代码是CD操作
*/
public String logoutAction()
{
this.setCds(null);
return "Logout";
}
//添加CD
public void add(CD cd)
{
try
{
this.getCdService().add(cd);
}
catch(MyException e)
{
e.printStackTrace();
}
}
public void update(CD cd)
{
try
{
this.getCdService().update(cd);
}
catch(MyException e)
{
e.printStackTrace();
}
}
public void del(Integer id)
{
try
{
this.getCdService().delete(id);
}
catch(MyException e)
{
e.printStackTrace();
}
}
// 升序排序
private transient final Comparator propertyAscEndingComparator = new Comparator()
{
public int compare(
Object object1,
Object object2)
{
try
{
String property1 = BeanUtils
.getProperty(
object1,
CDBean.this.sortByProperty);
String property2 = BeanUtils
.getProperty(
object2,
CDBean.this.sortByProperty);
return property1
.toLowerCase()
.compareTo(
property2
.toLowerCase());
}
catch(IllegalAccessException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
return 0;
}
catch(InvocationTargetException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
return 0;
}
catch(NoSuchMethodException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
return 0;
}
}
};
// 降序排序
private transient final Comparator propertyDesEndingComparator = new Comparator()
{
public int compare(
Object object1,
Object object2)
{
try
{
String property1 = BeanUtils
.getProperty(
object1,
CDBean.this.sortByProperty);
String property2 = BeanUtils
.getProperty(
object2,
CDBean.this.sortByProperty);
return property2
.toLowerCase()
.compareTo(
property1
.toLowerCase());
}
catch(IllegalAccessException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
return 0;
}
catch(InvocationTargetException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
return 0;
}
catch(NoSuchMethodException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
return 0;
}
}
};
/**
* 以下代码为排序代码
* @return String
*/
// 按标题升序排序
public String sortTitleAsc()
{
sortByProperty = "specialTitle";
cds.setWrappedData(sort(propertyAscEndingComparator));
return null;
}
// 按标题降序排序
public String sortTitleDesc()
{
System.out.println(((CD)temp.get(1)).getSpecialTitle());
sortByProperty = "specialTitle";
cds.setWrappedData(sort(propertyDesEndingComparator));
System.out.println(((CD)temp.get(1)).getSpecialTitle());
return null;
}
// 按艺术家升序排序
public String sortArtistAsc()
{
sortByProperty = "artist";
cds.setWrappedData(sort(propertyAscEndingComparator));
return null;
}
// 按艺术家降序排序
public String sortArtistDesc()
{
sortByProperty = "artist";
cds.setWrappedData(sort(propertyDesEndingComparator));
return null;
}
private List sort(Comparator comparator)
{
Collections.sort(temp,comparator);
return temp;
}
/**
* 以下是用户操作
*/
//添加CD到购物车
public String addCDtoCard()
{
return null;
}
/*
* public Collection getItems() { return items; }
*/
/*
* public CD getCd() { return cd; } public void setCd(CD cd) { this.cd = cd; }
*/
/**
* 为客户保存购物车信息
* @return public String clientAdd() { FacesContext context = FacesContext.getCurrentInstance();
* Map map = context.getExternalContext().getRequestParameterMap(); String specialTitle =
* (String)map.get("specialTitle"); String artist = (String)map.get("artist"); float
* price = Float.parseFloat((String)map.get("price")); CD cd = new
* CD(specialTitle,artist,price); itemMap.put(cd.getSpecialTitle(),cd);
* items.add(itemMap); return "add"; } public String remove() { FacesContext context =
* FacesContext.getCurrentInstance(); Map map =
* context.getExternalContext().getRequestParameterMap(); String specialTitle =
* (String)map.get("specialTitle"); CD cd = (CD)itemMap.get(specialTitle);
* itemMap.remove(cd.getSpecialTitle()); items.remove(itemMap); return "remove"; }
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -