评论

收藏

[jQuery] 物流项目宣传活动任务前台分页展示

开发技术 开发技术 发布于:2021-07-02 09:51 | 阅读数:559 | 评论:0

  PromotionAction代码:
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion>{
private File titleImgFile;//接收上传的图片
private String titleImgFileName;//接收上传图片的名称
public void setTitleImgFileFileName(String titleImgFileFileName) {
this.titleImgFileFileName = titleImgFileFileName;
}
public void setTitleImgFile(File titleImgFile) {
this.titleImgFile = titleImgFile;
}
@Autowired
private PromotionService promotionService;
@Action(value="promotion_save",results={@Result(name="success",type="redirect",location="./pages/take_delivery/promotion.html")})
public String save() throws IOException{
//宣传图上传、在数据表保存宣传图的路径
String savePath = ServletActionContext.getServletContext().getRealPath("/upload/");
String saveUrl = ServletActionContext.getServletContext().getContextPath("/upload/");
//生成随机图片名
UUID uuid = UUID.randomUUID();
String ext = titleImgFileFileName.substring(titleImgFileName.lastIndexOf("."));//扩展名
String randomFileName = uuid +ext;//图片名=随机唯一串+扩展名
//保存图片(绝对路径保存)
File destFile = new File(savePath + "/" + randomFileName);
System.out.println(destFile.getAbsolutePath());
FileUtils.copyFile(titleImgFile,destFile);
//将相对工程web的访问路径,保存到model中
model.setTitleImg(ServletActionContext.getRequest().getContextPath()+"/upload/"+randomFileName);
//调用业务层,完成活动任务数据保存
promotionService.save(model);
return SUCCESS;
}
//促销活动列表分页查询
@Action(value="promotion_pageQuery",results = {@Result(name="success",type="json")})
public String pageQuery(){
//构造分页查询参数
Pageable pageable = new PageRequest(page-1,rows);
//调用业务层完成查询
Page<Promotion> pageData = promotionService.findPageData(pageable);
//压入值栈
pushPageDataToValueStack(pageData);
return SUCCESS;
}
}
  apache.common.io包下有一个FileUtils类,其中有一个copyFile的方法,可以直接将上传的图片保存到指定路径。
public static void copyFile(File srcFile, File destFile) throws IOException {
copyFile(srcFile, destFile, true);
}
  PromotionService接口代码:
public interface PromotionService {
//保存宣传任务
void save(Promotion promotion);
//分页查询
Page<Promotion> findPageData(Pageable pageable);
//根据page和rows返回分页数据
@Path("/pageQuery")
@GET
@Produces({"application/xml","application/json"})
PageBean<Promotion> findPageData(@QueryParam("page") int page,@QueryParam("rows") int rows);
//根据id查询
@Path("/promotion/{id}")
@GET
@Produces({"application/xml","application/json"})
Promotion findById(@PathParam("id") Integer id);
//设置活动过期
void updateStatus(Date date);
}
  PromotionServiceImpl实现类代码:
@Service
@Transactional
public class PromotionServiceImpl implements PromotionService {
@Autowired
private PromotionRepository promotionRepository;
@Override
public void save(Promotion promotion){
promotionRepository.save(promotion);
}
@Override
public Page<Promotion> findPageData(Pageable pageable){
return promotionRepository.findAll(pageable);
}
@Override
public PageBean<Promotion> findPageData(int page,int rows){
Pageable pageable = new PageRequest(page-1,rows);
Pahe<Promotion> pageData = promotionRepository.findAll(pageable);
//封装到Page对象
PageBean<Promotion> pageBean = new PageBean<Promotion>();
pageBean.setTotalCount(pageData.getTotalElements());
pageBean.setPageData(pageData.getContent());
return pageBean;
}
@Override
public Promotion findById(Integer id){
return promotionRepository.findOne(id);
}
@Override
public void updateStatus(Date date){
promotionRepository.updateStatus(date);
}
}
  dao持久层PromotionRepository代码:
public interface PromotionRepository extends JpaRepository<Promotion,Integer>{
@Query("update Promotion set sratus='2' where endDate<? and status='1'>")
@Modifying
void updateStatus(Date now);
}
  

  
关注下面的标签,发现更多相似文章