写了很多的Aciton和Jsp,慢慢的总结出了一定的经验。即将Action中的数据封装到List 或Map中展示到JSP中。这样处理非常方便。
废话少说,线上一段代码
Action代码:
/**
*
*
* 项目名称:***
* 类名称:CouponListAction
* 类描述: 首页优惠券的获取根据下载量来排名
* 创建人:***
* 创建时间:2011-8-20 上午11:47:11
* 修改人:***
* 修改时间:2011-8-20 上午11:47:11
* 修改备注:
* @version
*
*/
public class CouponListAction extends CustomizeActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
Logger Log = Logger.getLogger(CouponListAction.class);
private String city;
private int pageSize = 4;
private ICouponService couponService;
private IStoreService storeService;
private List<Map<String, String>> couponList;
private int couponCount;
public String execute()
{
try{
if(StringUtil.isEmpty(city))
{
Log.error("取不到城市");
return "error";
}
BaseQuery searchVO = buildSearchVo();
couponList = this.buidCouponList(searchVO);
this.getCouponSum(searchVO);
couponList = this.getList(couponList);
}
catch (Exception e) {
LOG.debug("获取优惠券异常:",e);
e.printStackTrace();
}
return super.SUCCESS;
}
/**
* 封装查询条件
* @return
*/
private BaseQuery buildSearchVo() {
BaseQuery searchVO = new BaseQuery();
searchVO.setCity(Integer.parseInt(city));
searchVO.setSearchPageNum(1);
searchVO.setStatus(Parameter.Coupon.STATUS_CHECK,Parameter.Coupon.STATUS_UNCHECK);
searchVO.setSortKoubeiPriority();
searchVO.setSortBySales(SearchSortType.desc);
searchVO.setOnline();
return searchVO;
}
/**
* 获取优惠券总数
* @param searchVO
*/
private void getCouponSum(BaseQuery searchVO)
{
couponCount = couponService.count(searchVO);
}
/**
* 封装展示优惠券的信息
* @param searchVO
*/
private List<Map<String, String>> buidCouponList(BaseQuery searchVO)
{
List<Coupon> list = couponService.list(searchVO);
if(list==null)
{
Log.info("优惠券的数目为零");
return null;
}
couponList = new ArrayList<Map<String,String>>();
Iterator<Coupon> it = list.iterator();
while(it.hasNext())
{
Coupon coupon = it.next();
Map<String, String> map = new HashMap<String, String>();
String tempPicture = this.getImg(100, 100, coupon);
String title = coupon.getCouponDetail().getTitle();
if(StringUtil.isEmpty(tempPicture)||StringUtil.isEmpty(title)){
continue;
}
map.put("categoryName",this.getCategoryName(coupon.getCouponAddress().getCategoryId()));
map.put("categoryUrl",this.getCategoryUrl()+coupon.getCouponAddress().getCategoryId()+"&city="+city );
map.put("title",title);
map.put("endDate",this.formateTime(coupon.getEndTime()));
map.put("downlond", String.valueOf(coupon.getCouponDetail().getSale()));
map.put("detailUrl",this.getUrl()+coupon.getId());
map.put("picture", tempPicture);
map.put("pictureUrl",this.getPictureUrl()+city);
couponList.add(map);
}
return couponList;
}
/**
* 取出优惠券的条数,如果小于四条则全部显示,如果大于四条则截取四条
* @param list
* @return
*/
private List<Map<String, String>> getList(List<Map<String,String>> list){
List<Map<String, String>> myList = new ArrayList<Map<String,String>>();
if(list.size()<4){
return list;
}
else{
for(int i=0;i<4;i++){
myList.add(list.get(i));
}
}
return myList;
}
/**
* 封装链接条件优惠券detail页
* @return
*/
private String getUrl(){
String gbName = com.koubei.common.util.Area.getCityGbName(Integer.parseInt(this.getCity()));
String url = "http://"+gbName+".koubei.com/youhui/showcoupondetail.html?couponId=";
return url;
}
/**
* 封装分类链接条件
* @return
*/
private String getCategoryUrl(){
String gbName = com.koubei.common.util.Area.getCityGbName(Integer.parseInt(this.getCity()));
String url = "http://"+gbName+".koubei.com/youhui/list.html?vo.ioCategoryId=";
return url;
}
/**
* 封装图片链接地址优惠券list页
* @return
*/
private String getPictureUrl(){
String gbName = com.koubei.common.util.Area.getCityGbName(Integer.parseInt(this.getCity()));
String url = "http://"+gbName+".koubei.com/youhui/list.html?vo.ioOrder=2&city=";
return url;
}
/**
* 获取店铺地址
* @param storeId
* @return
*/
private String getStoreAddress(String storeId)
{
if(StringUtil.isEmpty(storeId))
{
Log.info("不存在该店铺");
return "";
}
Store store = storeService.getStoreOnlyById(storeId);
if(store==null)
{
Log.info("存在该店铺但该店铺没有命名");
return "";
}
return store.getAddress();
}
/**
* 通过key值取得分类信息
* @param key
* @return
*/
private String getCategoryName(int key)
{
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(Parameter.CategoryParameter.XIUXIANYULE, "休闲娱乐");
map.put(Parameter.CategoryParameter.LVYOUJIUDIAN, "旅游酒店");
map.put(Parameter.CategoryParameter.BIANMINFUWU, "便民服务");
map.put(Parameter.CategoryParameter.GOUWU, "购物");
map.put(Parameter.CategoryParameter.QITA, "其它");
map.put(Parameter.CategoryParameter.CANYINMEISHI, "餐饮美食");
return map.get(key);
}
private String formateTime(Date date)
{
if(date==null)
{
return "";
}
return DateUtil.dateFormat(date,"yyyy-MM-dd");
}
/**
* 获得优惠券的马良图片
* @param width
* @param height
* @return
*/
public String getImg(int width,int height,Coupon coupon) {
//默认图片
// String imgpath="http://k.kbcdn.com/product/huangye/cate_list/img/d.png";
String imgpath = "";
if(null == coupon.getCouponDetail()){
return imgpath;
}
try{
if((null!=coupon.getCouponDetail().getPictures()) && coupon.getCouponDetail().getPictures().size() !=0 ){
String pic = coupon.getCouponDetail().getPictures().get(0);
//马良图片
if(pic.indexOf("image/jpeg") != -1 || pic.indexOf("image/png") != -1 || pic.indexOf("image/gif")!= -1){
String marLinePic = Marline.get(pic, width, height);
if(StringUtil.isNotEmpty(marLinePic)){
return marLinePic;
}
return imgpath;
}
//直接
if(pic.indexOf("http://")!=-1){
return pic;
}
//etao
String etaoPic = HttpUrlConnectionUtil.getPictureFromEtao(pic, width+"", height+"");
if(StringUtils.isNotBlank(etaoPic)){
return etaoPic;
}
return imgpath;
}
if(null==coupon){//下面的判断的coupon不能为空
return imgpath;
}
if(storeService == null){
throw new IllegalStateException("please set storeService instance");
}
if(StringUtils.isNotEmpty(coupon.getStoreId())){
Store store=storeService.getStoreWithPicById(coupon.getStoreId());
if(null != store && store.getShowPicList() != null && store.getShowPicList().size() != 0){
Picture thepic = store.getShowPicList().get(0);
String pic = thepic.getFilePath(width+"x"+height);
if(StringUtils.isNotBlank(pic)){
return pic;
}
}
}
}catch(Exception ex){
Log.error(ex);
}
return imgpath;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public ICouponService getCouponService() {
return couponService;
}
public void setCouponService(ICouponService couponService) {
this.couponService = couponService;
}
public int getCouponCount() {
return couponCount;
}
public void setCouponCount(int couponCount) {
this.couponCount = couponCount;
}
public IStoreService getStoreService() {
return storeService;
}
public void setStoreService(IStoreService storeService) {
this.storeService = storeService;
}
public List<Map<String, String>> getCouponList() {
return couponList;
}
public void setCouponList(List<Map<String, String>> couponList) {
this.couponList = couponList;
}
}
JSP代码的展示:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:if test="couponList != null && couponList.size() > 0">
<s:set name="cityName" value='@com.koubei.common.util.Area@getCityPinyinName(city)'/>
<div class="yk-mod yk-mod-rounded-corner k2-column youhuiquan">
<span class="yk-x1"><span class="yk-x1a"></span></span>
<div class="yk-mod-content">
<div class="hd k2-fix-float">
<a href="http://<s:property value='#cityName' />.koubei.com/youhui/list.html?city=<s:property value='city' />">优惠信息<span class="yuihou-count">(<s:property value="couponCount"/>张)</span></a>
<a class="more" href="http://<s:property value='#cityName' />.koubei.com/youhui/list.html?city=<s:property value='city' />">更多</a>
</div>
<style>
.coupon-list{}
.coupon-list .item{height:72px;position:relative;width;padding: 6px 10px 6px 110px;border-bottom:1px dotted #ddd;vertical-align:middle;}
.coupon-list .item:hover{background:#FFF7E9;}
.coupon-list span.img{position:absolute;top:6px;left:10px;border:1px solid #eee;background:#eee;display:block;height:70px;overflow:hidden;line-height:72px;width:90px}
.coupon-list .title{display:block;height:18px;overflow:hidden;}
.coupon-list .prefix{display:block;height:18px;overflow:hidden;}
.coupon-list .download{display:block;height:18px;overflow:hidden;background-position:-113px -409px;padding-left:10px;}
.coupon-list .expires{display:block;height:18px;overflow:hidden;}
.coupon-list .item span em{color:#666;}
</style>
<div class="bd coupon-list k2-fix-float">
<s:iterator value="couponList" status="u">
<div class="item k2-fix-float">
<span class="img">
<a href="<s:property value='pictureUrl'/>"><img src="<s:property value='picture'/>" width='100'></a>
</span>
<span class="title">
<a href="<s:property value='pictureUrl'/>" title="<s:property value="title"/>"><s:property value="title"/></a>
</span>
<span class="prefix"><em>类别:</em><a href="<s:property value="categoryUrl"/>"><s:property value="categoryName"/></a></span>
<span class="download"><em>已下载:</em><s:property value="downlond"/>次</span>
<span class="expires"><em>截止日期:</em><s:property value="endDate"/></span>
</div>
</s:iterator>
</div>
</div>
<span class="yk-x2"><span class="yk-x2a"></span></span>
</div>
</s:if>
这样就完成了Action数据在JSP中的展示