| | 2 | AdSumQueryTag.java是后台查询推广来源统计结果的业务逻辑标签处理程序。[[BR]] |
| | 3 | {{{ #!java |
| | 4 | /** |
| | 5 | * |
| | 6 | * @author ChenYang |
| | 7 | * Date: 2012-2-1 |
| | 8 | */ |
| | 9 | public class AdSumQueryTag extends BaseTagSupport<AdService> { |
| | 10 | |
| | 11 | private String items; |
| | 12 | |
| | 13 | public void setItems(String items) { |
| | 14 | this.items = items; |
| | 15 | } |
| | 16 | |
| | 17 | @Override |
| | 18 | public int doStartTag() throws JspException { |
| | 19 | |
| | 20 | HttpServletRequest request = this.getHttpServletRequest(); |
| | 21 | |
| | 22 | if(StringUtils.isBlank(request.getParameter("gid"))){ |
| | 23 | throw new RuntimeException("没有选择游戏"); |
| | 24 | } |
| | 25 | |
| | 26 | if(StringUtils.isBlank(request.getParameter("startdate"))){ |
| | 27 | throw new RuntimeException("没有选择开始时间"); |
| | 28 | } |
| | 29 | |
| | 30 | if(StringUtils.isBlank(request.getParameter("enddate"))){ |
| | 31 | throw new RuntimeException("没有选择结束时间"); |
| | 32 | } |
| | 33 | |
| | 34 | long gameid = Long.parseLong(request.getParameter("gid")); |
| | 35 | |
| | 36 | Date start = null; |
| | 37 | Date end = null; |
| | 38 | try { |
| | 39 | start = DateUtils.parseDate(request.getParameter("startdate"), new String[]{"yyyy-MM-dd"}); |
| | 40 | String enddate = request.getParameter("enddate"); |
| | 41 | enddate = enddate + " 23:59:59 999"; |
| | 42 | |
| | 43 | end = DateUtils.parseDate(enddate, new String[]{"yyyy-MM-dd HH:mm:ss SSS"}); |
| | 44 | } catch (ParseException e) { |
| | 45 | e.printStackTrace(); |
| | 46 | } |
| | 47 | AdService adService = this.getBean("adService"); |
| | 48 | List<AdSum> list = adService.findByCondi(gameid, start, end); |
| | 49 | |
| | 50 | this.pageContext.setAttribute(items, list); |
| | 51 | |
| | 52 | return Tag.SKIP_BODY; |
| | 53 | } |
| | 54 | |
| | 55 | } |
| | 56 | }}} |
| | 57 | 该标签依赖的ApplicationContext对象,可以通过继承AbstractTransactionalJUnit4SpringContextTests获得,HttpServletRequest对象和pageContext对象可以通过Spring测试框架提供的模拟对象来模拟。 |
| | 58 | 测试代码如下: |
| | 59 | {{{ #!java |
| | 60 | @RunWith(SpringJUnit4ClassRunner.class) |
| | 61 | @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) |
| | 62 | public class AdSumQueryTagTest extends AbstractTransactionalJUnit4SpringContextTests{ |
| | 63 | |
| | 64 | @Test |
| | 65 | public void doStartTag() throws Exception { |
| | 66 | |
| | 67 | AdSumQueryTag adSumQueryTag = new AdSumQueryTag(); |
| | 68 | adSumQueryTag.setApplicationContext(applicationContext); |
| | 69 | adSumQueryTag.setItems("items"); |
| | 70 | |
| | 71 | MockHttpServletRequest request = new MockHttpServletRequest(); |
| | 72 | |
| | 73 | //模拟查询条件 |
| | 74 | request.addParameter("gid", "1"); |
| | 75 | request.addParameter("startdate", "2012-02-08"); |
| | 76 | request.addParameter("enddate", "2012-02-08"); |
| | 77 | |
| | 78 | MockPageContext pageContext = new MockPageContext(); |
| | 79 | |
| | 80 | adSumQueryTag.setHttpServletRequest(request); |
| | 81 | adSumQueryTag.setPageContext(pageContext); |
| | 82 | |
| | 83 | adSumQueryTag.doStartTag(); |
| | 84 | |
| | 85 | } |
| | 86 | } |
| | 87 | |
| | 88 | }}} |