控制层代码如下:
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class WebFrontController {
// 首页
private static String getIndexpage = getViewPath("/web/front/index");
@Autowired
private CourseService courseService;
/**
* 获取网站首页数据
*
* @param request
* @param response
* @param model
* @return
*/
@RequestMapping("/index")
public String getIndexpage(HttpServletRequest request,
HttpServletResponse response, Model model) {
try {
JSONObject object = new JSONObject();
// 所有产品分类课程
List<Product> productsList = (List<Product>) CacheUtil
.get(CacheConstans.INDEX_PRODUCT_CLASSIFY);
if (productsList == null || productsList.size() == 0) {
QueryProduct queryProduct = new QueryProduct();
queryProduct.setParentId(0);
productsList = productService.getAllProductList(queryProduct);
CacheUtil.set(CacheConstans.INDEX_PRODUCT_CLASSIFY,
productsList, CacheConstans.RECOMMEND_COURSE_TIME);// 缓存一小时
}
Iterator<Product> it = productsList.iterator();
while (it.hasNext()) {
Product product = it.next();
int pId = product.getId();
try {
QueryCourse queryCourses = new QueryCourse();
queryCourses.setCount(4);// 只显示4条数据
queryCourses.setIsavaliable(1);// 只查询上架的
queryCourses.setSellType("COURSE");// 类型课程
queryCourses.setIsOverdue("true");// 查询未过期的
queryCourses.setOrder("PRODUCTSORT");
queryCourses.setProductId(pId);
List<CourseDto> courseProductClassifyList = courseService
.queryCourse(queryCourses);
product.setCourseProductClassifyList(courseProductClassifyList);
} catch (Exception e) {
logger.error(
"WebFrontController.courseProductClassifyList()--error",
e);
}
}
// request.setAttribute("productsList", productsList);
object.put("productsList", productsList);
model.addAttribute(object);
}catch (Exception e) {
logger.error("WebFrontController.getIndexpage()--error", e);
return setExceptionRequest(request, e);
}
return getIndexpage;
}
}
注意:我们上面引用的是com.alibaba.fastjson.JSONObject
那么在jsp页面如何正确的获取到JSONObject里面的productsList呢?请看如下代码?
<!-- 按产品分类显示开始 -->
<c:forEach var="products" items="${JSONObject.productsList}" varStatus="productsStat">
<div class="bg-fa -of">
<section class="container">
<header class="comm-title clearfix ">
<div class="fr mt5 mr5" style="margin-top: 9px;letter-spacing: 1px;">
<a
href="/front/showcoulist?queryCourse.productId=${products.id}"
title="查看全部课程>" class="c-more"><span style="font-size:15px;color:#009944;">查看全部课程></span></a>
</div>
<h2 class="fl tac popover-main">
<span class="c-333">${products.productName}</span>
</h2>
<c:if test="${products.productName == '校园EMBA'}">
<div class="popover dn">
EMBA是国际商业领域成熟、成功的教育模式,是专门培养企业高级管理人员的教育项目。其特点在于知识的复合性、实操性、浓缩性。本培训项目借鉴该模式,将其引入校园,专门为非工商管理类大学生、研究生及职业人士定制化培养,让您手捧“专业文凭+就业证书”,顺利地走进您理想中的就业单位。
</div>
</c:if>
<c:if test="${products.productName == '企业课程'}">
<div class="popover dn">
为帮助在职人员利用碎片化时间,学习系统化知识,丰富各类管理人员、技术人员的知识结构,我们打造了管理者职业能力提升系列线上课程。
</div>
</c:if>
<div class="clear"></div>
</header>
<article class="comm-course-list <c:if test="${products.productName == '专家讲座'}">comm-course-list-b</c:if>">
<ul class="of clearfix">
<c:if test="${products.productName != '专家讲座'}">
<li class="index-video-li">
<div class="cc-l-wrap cc-v-wrap">
<div class="cc-backgroud"
<c:if test="${JSONObject.trailerList.size()>=productsStat.index }"> style="background-color:${ JSONObject.trailerList[productsStat.index].backcolor};padding-bottom:1px ;" </c:if>>
<h3 class="index-h3-title">—${JSONObject. trailerList[productsStat.index].title}—</h3>
<h3 class="index-h3-desc">观看视频简介▼</h3>
<section class="course-img index-video-img">
<img
src="${ctx}/images/upload/trailer/${JSONObject. trailerList[productsStat.index].image}"
class="img-responsive"
style="border-radius: 9px;" />
<div class="cc-video-mask">
<a href="javascript:void(0)"
style="margin:-19px 0 0 -19px;"
onclick="ShowDiv('MyDiv','fade','${JSONObject. trailerList[productsStat.index].videourl}')"
title="点击播放" class="v-play-btn"><em
style="background:url(${ctx}/static/mooc_web/img/play.png) no-repeat;height: 38px;width: 38px;display: inline-block;"></em></a>
</div>
</section>
</div>
</div>
</li>
</c:if>
<c:forEach var="courseNew" varStatus="stat" items="${products.courseProductClassifyList}">
<li
<c:if test="${stat.last&&products.productName != '专家讲座'}"> class="m-img-sd" </c:if>>
<div class="cc-l-wrap">
<section class="course-img">
<c:if test="${null!= courseNew.logo }">
<img xsrc="${ctx}${courseNew.logo }"
src="${ctx}/static/mooc_web/img/default-img.gif"
class="img-responsive" alt="${courseNew.courseName }">
</c:if>
<c:if test="${null== courseNew.logo }">
<img xsrc="static/mooc_web/img/default-img.gif"
src="${ctx}/static/mooc_web/img/default-img.gif"
class="img-responsive" alt="${courseNew.courseName }">
</c:if>
<div class="cc-mask">
<a href="${ctx}/front/couinfo/${courseNew.courseId}"
title="开始学习" class="comm-btn c-btn-1">开始学习</a>
</div>
</section>
<div class="cou-info-box">
<h3 class="hLh30 txtOf mt10 ">
<a href="${ctx}/front/couinfo/${courseNew.courseId}"
title="${courseNew.courseName }"
class="course-title fsize15 c-333">${courseNew.courseName }</a>
</h3>
<section class="clearfix of" style="margin: 5px 0px 11px 0px">
<span class="jgAttr" >
<tt class="c-777777 f-fM mr5 fsize12 txtOf"" >${courseNew.title }</tt>
</span>
</section>
<section class="clearfix of">
<span class="fl jgAttr txtOf">
<tt class="c-777777 f-fM mr5 fsize12">${courseNew.pageViewcount }人浏览 |
<c:if test="${courseNew.currentPrice!='0.00' }">
${courseNew.pageBuycount }人购买 |
</c:if> 共${courseNew.lessionNum }节课</tt>
</span>
</section>
<div>
<c:if test="${courseNew.currentPrice=='0.00' }">
<span class="jgTag"><tt class="c-green fsize14 f-fM">免费</tt></span>
</c:if>
<c:if test="${courseNew.currentPrice!='0.00' }">
<span class="jgTag"><tt class="c-yellow fsize14 f-fM">¥${courseNew.currentPrice }</tt></span>
</c:if>
</div>
</div>
</div>
</li>
</c:forEach>
</ul>
<div class="clear"></div>
</article>
</section>
</div>
</c:forEach>
<!-- 按产品分类显示结束 -->
视图页面显示如下:
ok,大功告成!