• 第五周作业 项目的进展情况


    先说学生端:

      先说框架的搭建,本项目使用的是SSH框架,用到的表六张。学生端的有七个功能。都实现了,后期会加上一点优化的功能包括界面设计,更多的检查。

      下面是一些比较重要的类的实现

      1.增删改查

     1 package cn.hp.base.dao;
     2 
     3 import java.io.Serializable;
     4 import java.lang.reflect.ParameterizedType;
     5 import java.util.List;
     6 
     7 import org.hibernate.Query;
     8 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
     9 
    10 import cn.hp.core.page.PageResult;
    11 import cn.hp.core.util.QueryHelper;
    12 
    13 public abstract class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
    14      Class<T> clazz;
    15        public BaseDaoImpl()
    16        {//BaseDaoImpl<User>获取弗雷德简单类名
    17            ParameterizedType pt=(ParameterizedType) this.getClass().getGenericSuperclass();
    18            clazz=(Class<T>) pt.getActualTypeArguments()[0];//
    19        }
    20     
    21     @Override
    22     public void save(T entity) {
    23         getHibernateTemplate().save(entity);
    24     }
    25 
    26     @Override
    27     public void update(T entity) {
    28         getHibernateTemplate().update(entity);
    29     }
    30 
    31     @Override
    32     public void delete(Serializable id) {
    33         // TODO Auto-generated method stub
    34         
    35     }
    36 
    37     @Override
    38     public T findObjectById(Serializable id) {
    39         return getHibernateTemplate().get(clazz, id);//getHibernateTemplate().get(clazz, id);
    40     }
    41 
    42     @Override
    43     public List<T> findAll() {
    44          Query query = getSession().createQuery("from "+clazz.getSimpleName());
    45          return query.list();
    46     }
    47     
    48     @Override
    49     public PageResult getPageResult(QueryHelper queryHelper, int pageNo,
    50             int pageSize) {
    51         System.out.println(queryHelper.getQueryListHql()+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    52         Query query=getSession().createQuery(queryHelper.getQueryListHql());
    53         List<Object> parameters=queryHelper.getParameters();
    54         if(parameters!=null)
    55         {
    56             for(int i=0;i<parameters.size();i++)
    57             {
    58                 query.setParameter(i, parameters.get(i));
    59             }
    60         }
    61         
    62         if(pageNo<1) pageNo=1;
    63         query.setFirstResult((pageNo-1)*pageSize);//设置数据的起始索引号
    64         query.setMaxResults(pageSize);
    65         List items= query.list();
    66         //获取总记录数
    67         Query queryCount=getSession().createQuery(queryHelper.getQuerycountHql());
    68         if(parameters!=null)
    69         {
    70             for(int i=0;i<parameters.size();i++)
    71             {
    72                 queryCount.setParameter(i, parameters.get(i));
    73             }
    74         }
    75          long tptalCount = (Long) queryCount.uniqueResult();
    76          
    77         return new PageResult(tptalCount, pageNo, pageSize, items);
    78     }
    79     
    80 }
    View Code

      2.查询助手

     1 package cn.hp.core.util;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 public class QueryHelper {
     7     //from子句
     8     private String fromClause="";
     9     //where子句
    10     private String whereClause="";
    11     //order by子句
    12     private String orderByClause="";
    13     
    14     private List<Object> parameters;
    15     //排序顺序
    16     public static String ORDER_BY_DESC="DESC";
    17     public static String ORDER_BY_ASC="ASC";
    18     /*
    19      * 构造from子句
    20      * Class clazz实体类
    21      * String alias实体类对应的别名
    22      */
    23     public QueryHelper(Class clazz,String alias)
    24     {
    25         fromClause="FROM "+clazz.getSimpleName()+" "+alias;
    26     }
    27     /*
    28      * 构造where子句,
    29      * condition查询条件语句,列如i.title like ?
    30      * params查询条件中对应的查询条件值,例如:%标题%
    31      */
    32     public void addCondition(String condition,Object... params)
    33     {//设置查询条件值到查询条件值集合中
    34         if(whereClause.length()>1)
    35         {
    36             whereClause+=" AND "+condition;
    37         }
    38         else
    39         {
    40             whereClause=" WHERE "+condition;
    41         }
    42         if(parameters==null)
    43         {
    44             parameters=new ArrayList<Object>();
    45         }
    46         if(params!=null)
    47         {
    48             for(Object param:params)
    49             {
    50                 parameters.add(param);
    51             }
    52         }
    53     }
    54     public void addOrderByProperty(String property,String order)
    55     {//
    56         if(orderByClause.length()>1)
    57         {
    58             orderByClause+=","+property+" "+order;
    59         }
    60         else
    61         {
    62             orderByClause=" ORDER BY "+property+" "+order;
    63         }
    64     }
    65     
    66     //1.查询HQL语句
    67     public String getQueryListHql()
    68     {
    69         return fromClause+whereClause+orderByClause;
    70     }
    71     
    72     //1.查询HQL语句
    73     public String getQuerycountHql()
    74     {
    75         return "SELECT count(*) "+fromClause+whereClause;
    76     }
    77     
    78     //2.查询HQL语句?对应的条件值集合
    79     public List<Object> getParameters()
    80     {
    81         return parameters;
    82     }
    83 
    84 }
    View Code

      3.分页对象

     1 package cn.hp.core.page;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 public class PageResult {
     7     //总记录数
     8     private long totalCount;
     9     //当前页号
    10     private int pageNo;
    11     //总页数
    12     private int totalPageCount;
    13     
    14     //页大小
    15     private int pageSize;
    16     //列表记录
    17     public List items;
    18     //计算总页数
    19     public PageResult(long totalCount, int pageNo, int pageSize, List items) {
    20         //防止出现空指针异常
    21         this.items = items==null?new ArrayList():items;
    22         this.totalCount = totalCount;
    23         this.pageSize = pageSize;
    24         if(totalCount!=0)//计算总页数
    25         {
    26             int tem=(int)totalCount/pageSize;
    27             this.totalPageCount =totalCount%pageSize==0?tem:tem+1;
    28             this.pageNo=pageNo;
    29         }
    30         else
    31         {
    32             this.pageNo =0;
    33         }
    34     }
    35     
    36     public long getTotalCount() {
    37         return totalCount;
    38     }
    39     public void setTotalCount(long totalCount) {
    40         this.totalCount = totalCount;
    41     }
    42     public int getPageNo() {
    43         return pageNo;
    44     }
    45     public void setPageNo(int pageNo) {
    46         this.pageNo = pageNo;
    47     }
    48     public int getTotalPageCount() {
    49         return totalPageCount;
    50     }
    51     public void setTotalPageCount(int totalPageCount) {
    52         this.totalPageCount = totalPageCount;
    53     }
    54     public int getPageSize() {
    55         if(pageSize<1) pageSize=1;
    56         return pageSize;
    57     }
    58     public void setPageSize(int pageSize) {
    59         this.pageSize = pageSize;
    60     }
    61     public List getItems() {
    62         return items;
    63     }
    64     public void setItems(List items) {
    65         this.items = items;
    66     }
    67  
    68 }
    View Code

    4.BaseAction,接收页面传过来的常用参数,其他的Action都继承于它。

     1 package cn.hp.stu.action;
     2 
     3 import cn.hp.core.page.PageResult;
     4 
     5 import com.opensymphony.xwork2.ActionSupport;
     6 
     7 public class BaseAction extends ActionSupport{
     8     
     9     protected PageResult pageResult;
    10     private int pageNo;
    11     private int pageSize;
    12     protected String[] selectedRow;
    13     
    14     
    15     public String[] getSelectedRow() {
    16         return selectedRow;
    17     }
    18 
    19     public void setSelectedRow(String[] selectedRow) {
    20         this.selectedRow = selectedRow;
    21     }
    22 
    23     private static int DEFAULT_PAGE_SIZE=3;
    24     
    25 
    26     public PageResult getPageResult() {
    27         return pageResult;
    28     }
    29 
    30     public void setPageResult(PageResult pageResult) {
    31         this.pageResult = pageResult;
    32     }
    33 
    34     public int getPageSize() {
    35         if(pageSize<1) pageSize=DEFAULT_PAGE_SIZE;
    36         return pageSize;
    37     }
    38 
    39     public void setPageSize(int pageSize) {
    40         this.pageSize = pageSize;
    41     }
    42 
    43     public int getPageNo() {
    44         return pageNo;
    45     }
    46 
    47     public void setPageNo(int pageNo) {
    48         this.pageNo = pageNo;
    49     }
    50 }
    View Code

    5.接收页面参数的StuAction,主要有上传,下载,跳转等等

      1 package cn.hp.stu.action;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.IOException;
      6 import java.io.InputStream;
      7 import java.util.Date;
      8 import java.util.List;
      9 
     10 import javax.annotation.Resource;
     11 import javax.servlet.ServletOutputStream;
     12 import javax.servlet.http.HttpServletResponse;
     13 
     14 import org.apache.commons.io.FileUtils;
     15 import org.apache.commons.lang.StringUtils;
     16 import org.apache.commons.logging.Log;
     17 import org.apache.commons.logging.LogFactory;
     18 import org.apache.struts2.ServletActionContext;
     19 
     20 import cn.hp.core.entity.Classinfo;
     21 import cn.hp.core.entity.Stu;
     22 import cn.hp.core.entity.StuWork;
     23 import cn.hp.core.entity.TeaWork;
     24 import cn.hp.core.stu.Constant;
     25 import cn.hp.core.util.QueryHelper;
     26 import cn.hp.stu.service.StuService;
     27 import cn.hp.stuwork.service.StuWorkService;
     28 import cn.hp.teawork.service.TeaWorkService;
     29 
     30 /*
     31  * 上传作业需修改
     32  */
     33 public class StuAction extends BaseAction{
     34     private Stu stu;//
     35     
     36     private String stuUserName;//学生的用户名
     37     private Integer stuScore;//学生的分数
     38     private String stuPassword;//密码
     39     private String mail;//邮箱
     40     
     41     private String fileName;//上传作业的文件名
     42      
     43     private String msg;//显示相应的错误消息
     44     
     45     private String username;
     46     private String password;
     47     private String RoleType;
     48     
     49     private File word;
     50     private String wordFileName;
     51     private String wordContentType;
     52     
     53     private Date submitTime;
     54     @Resource
     55     private StuService stuService; 
     56     
     57     private StuWork stuWork;
     58     @Resource
     59     private StuWorkService stuWorkService;
     60     
     61     private Classinfo classinfo;//查找学生的时候顺便把他的班级查找出来。
     62     
     63     @Resource
     64     private TeaWorkService teaWorkService;
     65     //顶部
     66     public String top()
     67     {
     68         
     69         return "top";
     70     }
     71     
     72     //左部的页面
     73     public String left()
     74     {
     75         return "left";
     76     }
     77     
     78     //跳转到学生注册界面
     79     public String registUI()
     80     {
     81         return "registUI";
     82     }
     83     
     84     //跳转到框架的主界面
     85     public String frame()
     86     {
     87         return "frame";
     88     }
     89     
     90     //跳转到教师发布信息的页面
     91     public String teaPub()
     92     {
     93         QueryHelper queryHelper=new QueryHelper(TeaWork.class, "t");
     94         queryHelper.addCondition("t.teaId =? or t.teaId =?", 1,2);
     95         pageResult=teaWorkService.findTeapubByStu(queryHelper,getPageNo(),getPageSize());
     96         return "teapubUI";
     97     }
     98     
     99     //加载教师评论
    100     public String teacomm()
    101     {
    102         Stu stu=(Stu) ServletActionContext.getRequest().getSession().getAttribute(Constant.STU);
    103         pageResult=stuWorkService.findCommentByStu("hepan",getPageNo(),getPageSize());
    104         return "teacomm";
    105     }
    106     
    107     //保存学生的注册信息
    108     public String regist()
    109     {
    110         if(StringUtils.isNotBlank(stu.getStuUserName())&&StringUtils.isNotBlank(stu.getStuPassword()))
    111         {
    112             List<Stu> ls=stuService.fingStuByUsername(stu.getStuUserName());
    113             if(ls.size()>0&&ls!=null)
    114             {
    115                 msg="用户已经存在";
    116                 return "registUI";
    117             }
    118             stuService.save(stu);
    119         }
    120         else
    121         {
    122             msg="用户名或密码为空";
    123             return "registUI";
    124         }
    125         return "home";
    126     }
    127 
    128     
    129     public String loginUI()
    130     {
    131         return "loginUI";
    132     }
    133     
    134     //查询出分数前十的学生,跳转到转到榜样学习的界面
    135     public String learn()
    136     {
    137         System.out.println("chaxun..................");
    138         QueryHelper queryHelper=new QueryHelper(StuWork.class, "w");
    139         queryHelper.addOrderByProperty("w.score ",QueryHelper.ORDER_BY_DESC);
    140         pageResult=stuService.getPageResult(queryHelper,getPageNo(),getPageSize());
    141         return "sup_listUI"; 
    142     }
    143     
    144     //用户点击登陆之后执行的代码
    145     public String login()
    146     {
    147         if(null!=RoleType&&RoleType.equals("rbStudent")&&StringUtils.isNotBlank(username)&&StringUtils.isNotBlank(password))
    148         {
    149             List<Stu> ls=stuService.fingStuByUsernameAndPassword(username,password);
    150             if(ls.size()>0&&ls!=null)
    151             {
    152                 Stu st=ls.get(0);
    153                 ServletActionContext.getRequest().getSession().setAttribute(Constant.STU, st);
    154                 Log log = LogFactory.getLog(getClass());
    155                 return "frame";
    156             }
    157             else
    158             {
    159                 setMsg("用户名或密码错误");
    160             }
    161             return "home";
    162         }
    163         else 
    164         {
    165             setMsg("亲输入用户名密码");
    166         }
    167         return "home";
    168     }
    169     
    170     //跳转到学生列表界面
    171     public String listUI()
    172     {
    173         QueryHelper queryHelper=new QueryHelper(Stu.class, " s ");
    174         //从当前的Session中获取用户的信息,进行查找显示在相应的页面上
    175 //        Stu stu1 = (Stu) ServletActionContext.getRequest().getSession().getAttribute(Constant.STU);
    176 //        queryHelper.addCondition("s.stuUserName=? ", stu1.getStuUserName());
    177         queryHelper.addCondition("s.stuUserName=? ", "hepan");
    178         pageResult=stuService.getPageResult(queryHelper,getPageNo(),getPageSize());
    179         return "listUI";
    180     }
    181     
    182     //依据用户名搜索学生的信息
    183     public String doSearch()
    184     {
    185         if(StringUtils.isNotBlank(stu.getStuUserName()))
    186         {
    187             QueryHelper queryHelper=new QueryHelper(Stu.class, "s");
    188             queryHelper.addCondition(" s.stuUserName like ?", "%"+stu.getStuUserName()+"%");
    189             pageResult=stuService.getPageResult(queryHelper,getPageNo(),getPageSize());
    190         }
    191         return "listUI";
    192     }
    193     
    194     //跳转到编辑学生页面
    195     public String editUI()
    196     {
    197         Stu stu1 = (Stu) ServletActionContext.getRequest().getSession().getAttribute(Constant.STU);
    198         /*if(StringUtils.isNotBlank(stu.getStuUserName()))
    199         {
    200             stu=stuService.findObjectById(stu.getStuUserName());
    201         }*/
    202         stu=stuService.findObjectById("hepan");
    203         return "editUI";
    204     }
    205     
    206     //保存编辑
    207     public String edit()
    208     {
    209         System.out.println(stu.getStuScore());
    210         System.out.println(stu);
    211         if(StringUtils.isNotBlank(stu.getStuUserName()))
    212         {
    213             stuService.update(stu);
    214         }
    215         return listUI();
    216     }
    217     
    218     //处理上传文件的页面
    219     public String upLoad()
    220     {
    221         String realPath = ServletActionContext.getServletContext().getRealPath("upload/stu");
    222         System.out.println(realPath);
    223         System.out.println(wordFileName);
    224         try {
    225             FileUtils.copyFile(word, new File(realPath, wordFileName));
    226         } catch (IOException e) {
    227             // TODO Auto-generated catch block
    228             e.printStackTrace();
    229         }
    230         //把上传时间写入相应的表中
    231         StuWork stuWork=new StuWork();
    232         //设定学生提交作业的时间
    233         Date time= new java.sql.Date(new java.util.Date().getTime());
    234         
    235         stuWork.setSubmitTime(time);
    236         stuWork.setWorkPath(fileName);//保存学生上传的文件名
    237         Stu stu = (Stu) ServletActionContext.getRequest().getSession().getAttribute(Constant.STU);//从Session里面获取学生
    238         stuWork.setStu(stu);
    239         
    240         stuWork.setTeaWork(teaWorkService.findObjectById(1));
    241         
    242         stuWorkService.save(stuWork);
    243         return listUI();
    244     }
    245     
    246     //实现文件的下载
    247     public void downLoad() throws IOException
    248     {
    249         HttpServletResponse response = ServletActionContext.getResponse();
    250         response.setContentType("text/plain;charset=UTF-8");
    251         response.setHeader("Content-Disposition", "attachment;filename="
    252            +new String(this.fileName.getBytes(),"ISO-8859-1"));
    253         String realPath = ServletActionContext.getServletContext().getRealPath("upload/tea/"+fileName);
    254         InputStream in=new FileInputStream(realPath);
    255         ServletOutputStream outputStream = response.getOutputStream();
    256         byte [] buf=new byte[1024];
    257         
    258         while(in.read(buf)!=-1)
    259         {
    260             outputStream.write(buf);
    261         }
    262         if(in!=null)
    263         {
    264             in.close();
    265         }
    266         if(outputStream!=null)
    267         {
    268             outputStream.close();
    269         }
    270     }
    271     
    272     public File getWord() {
    273         return word;
    274     }
    275 
    276     public void setWord(File word) {
    277         this.word = word;
    278     }
    279 
    280     public String getWordFileName() {
    281         return wordFileName;
    282     }
    283 
    284     public void setWordFileName(String wordFileName) {
    285         this.wordFileName = wordFileName;
    286     }
    287 
    288     public String uploadUI()
    289     {
    290         return "uploadUI";
    291     }
    292     
    293     public Stu getStu() {
    294         return stu;
    295     }
    296 
    297     public void setStu(Stu stu) {
    298         this.stu = stu;
    299     }
    300 
    301     public Integer getStuScore() {
    302         return stuScore;
    303     }
    304 
    305     public void setStuScore(Integer stuScore) {
    306         this.stuScore = stuScore;
    307     }
    308 
    309     public String getStuUserName() {
    310         return stuUserName;
    311     }
    312 
    313     public void setStuUserName(String stuUserName) {
    314         this.stuUserName = stuUserName;
    315     }
    316 
    317     public String getStuPassword() {
    318         return stuPassword;
    319     }
    320 
    321     public void setStuPassword(String stuPassword) {
    322         this.stuPassword = stuPassword;
    323     }
    324 
    325     public String getMsg() {
    326         return msg;
    327     }
    328 
    329     public void setMsg(String msg) {
    330         this.msg = msg;
    331     }
    332     
    333     public void addActionError(String anErrorMessage){
    334            String s=anErrorMessage;
    335            System.out.println(s+"TUUUUUUUUUUUUUUUUU");
    336           }
    337     
    338    public void addActionMessage(String aMessage){
    339        String s=aMessage;
    340    }
    341    
    342    public void addFieldError(String fieldName, String errorMessage){
    343        String s=errorMessage;
    344        String f=fieldName;
    345        System.out.println(f);
    346     }
    347 
    348     public String getWordContentType() {
    349         return wordContentType;
    350     }
    351     
    352     public void setWordContentType(String wordContentType) {
    353         this.wordContentType = wordContentType;
    354     }
    355     
    356 
    357     public String getFileName() {
    358         return fileName;
    359     }
    360 
    361     public void setFileName(String fileName) {
    362         System.out.println(fileName);
    363         this.fileName = fileName;
    364     }
    365     
    366 
    367 
    368     public String getMail() {
    369         return mail;
    370     }
    371 
    372     public void setMail(String mail) {
    373         this.mail = mail;
    374     }
    375 
    376     public StuService getStuService() {
    377         return stuService;
    378     }
    379 
    380     public void setStuService(StuService stuService) {
    381         this.stuService = stuService;
    382     }
    383 
    384     public String getUsername() {
    385         return username;
    386     }
    387 
    388     public void setUsername(String username) {
    389         this.username = username;
    390     }
    391 
    392     public String getPassword() {
    393         return password;
    394     }
    395 
    396     public void setPassword(String password) {
    397         this.password = password;
    398     }
    399 
    400     public String getRoleType() {
    401         return RoleType;
    402     }
    403 
    404     public void setRoleType(String roleType) {
    405         RoleType = roleType;
    406     }
    407 
    408     public Date getSubmitTime() {
    409         return submitTime;
    410     }
    411 
    412     public void setSubmitTime(Date submitTime) {
    413         this.submitTime = submitTime;
    414     }
    415 
    416     public StuWork getStuWork() {
    417         return stuWork;
    418     }
    419 
    420     public void setStuWork(StuWork stuWork) {
    421         this.stuWork = stuWork;
    422     }
    423     
    424     
    425     private String workId;//作业id
    426     private TeaWork teaWork;//教师作业
    427     private String workPath;//作业的提交路径
    428     private Integer score;//分数
    429     private String comment;//教师的品论
    430     public String getWorkId() {
    431         return workId;
    432     }
    433 
    434     public void setWorkId(String workId) {
    435         this.workId = workId;
    436     }
    437 
    438     public TeaWork getTeaWork() {
    439         return teaWork;
    440     }
    441 
    442     public void setTeaWork(TeaWork teaWork) {
    443         this.teaWork = teaWork;
    444     }
    445 
    446     public String getWorkPath() {
    447         return workPath;
    448     }
    449 
    450     public void setWorkPath(String workPath) {
    451         this.workPath = workPath;
    452     }
    453 
    454     public Integer getScore() {
    455         return score;
    456     }
    457 
    458     public void setScore(Integer score) {
    459         this.score = score;
    460     }
    461 
    462     public String getComment() {
    463         System.out.println(comment+"1111111111111111111");
    464         return comment;
    465     }
    466 
    467     public void setComment(String comment) {
    468         this.comment = comment;
    469     }
    470 
    471     public Classinfo getClassinfo() {
    472         return classinfo;
    473     }
    474 
    475     public void setClassinfo(Classinfo classinfo) {
    476         this.classinfo = classinfo;
    477     }
    478     
    479 }
    View Code

    界面预览现在还没加上界面的美化,简单地预览一下,功能都实现了。

     界面后期会增强:包括美化,增加js,HTML,JQuery等等

  • 相关阅读:
    蜘蛛修网
    推荐10款用户界面设计
    18个web开发人员必备的Chrome扩展
    分享一个帮助你在线测试响应式设计的web工具 Screenqueri.es
    推荐16款每周设计灵感
    分享一个帮助你快速构建HTML5游戏的javascript类库 CreateJS
    5个jQuery的备选轻量级移动客户端开发(Mobile development)类库
    帮助你高效开发Ajax应用的超酷jQuery插件 AjaxML
    免费资源:350个超棒标志性字符图标
    免费素材下载:超酷的简单按钮UI
  • 原文地址:https://www.cnblogs.com/he-123/p/5347440.html
Copyright © 2020-2023  润新知