⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 60837f1377a8001b122086c96388e37a

📁 用jsf实现cd商店的程序
💻
字号:
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 com.tangjun.web.pojo.CDType;

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 CD						newCd			= new CD();

	private String					sortByProperty	= "specialTitle";

	private String					itemValue;

	private DataModel 
	
	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 String add()
	{
		try
		{

			CDType cdType = new CDType();
			cdType.setId(Integer.valueOf(this.itemValue));
			newCd.setCdtype(cdType);
			this.getCdService().add(newCd);
			// 重新加载数据
			temp = null;
			return "add:ok";
		}

		catch(Exception e)
		{
			System.out.println(e.getMessage());
			return "add:err";
		}

	}

	/*
	 * 拓展功能 public String updateAction() { try { this.getCdService().update(newCd); return
	 * "update:ok"; } catch(MyException e) { e.printStackTrace(); return "update:err"; } } public
	 * String delAction() { try { this.getCdService().delete(newCd.getId()); return "delete:ok"; }
	 * catch(MyException e) { e.printStackTrace(); return "delete:err"; } }
	 */
	// 升序排序
	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()
	{
		sortByProperty = "specialTitle";
		cds.setWrappedData(sort(propertyDesEndingComparator));
		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;
	}

	/**
	 * 以下是用户操作
	 */

	public Collection getItems()
	{
		return items;
	}

	public CD getNewCd()
	{
		return newCd;
	}

	public void setNewCd(CD cd)
	{
		this.newCd = cd;
	}

	public String getItemValue()
	{
		return itemValue;
	}

	public void setItemValue(String itemValue)
	{
		this.itemValue = itemValue;
	}

	/**
	 * 为客户保存购物车信息
	 */
	public String clientAdd()
	{
		//FacesContext context = FacesContext.getCurrentInstance();
		//Map map = context.getExternalContext().getRequestParameterMap();
		String specialTitle = (String)map.get("specialTitle");
		String artist = (String)map.get("artist");
		CD cd = (CD)cds.getRowData();
		
		System.out.println("specialTitle:"+cd.getSpecialTitle());
		System.out.println("artist:"+cd.getArtist());
		//(String)map.get("price")
		float price = Float.parseFloat("15.1");
		//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 + -