Eclipse,Tomcat,MySql SSH框架的各种jar包
在做之前先改点东西,看数据表的时候发现命名不太规范,student外键对应的应该是college表的id,所以改以下这几处:1、com.ssh.entities中Student实体类的collegeName改为college;2、Student.hbm.xml中COLLEGE_NAME改为COLLEGE_ID;3、对应数据库的表字段也改为COLLEGE_ID;在SSH框架之浅析探讨(二)中看到StudentAction有两个方法,“validateUserNameAndPassword()”ajax登录验证;“public String list()”主体页面的清单。ajax登录验证已做,那么接着实现“public String list()”方法。依着这个思路“Action需要Service层帮助,Service层需要Dao层帮助”,我们就先去StudentDao中把数据查出来,代码如下:public List
Dao层弄完,接着弄Service层StudentService中主要代码如下: public List
再回到StudentAction中,StudentDao和StudentService返回的结果在请求域中,此时需要StudentAction类实现RequestAware接口并实现相应的方法,用来获取这些值:主要代码如下:private Map
然后在public String list()方法中存值并传到主体页面:public String list(){ request.put('student',studentService.getAll()); return 'list'; }
在主体页面取出值并展示出来:1、打开建立好的WEB-INF下的stu-list.jsp;2、引入struts2的标签;<%@ taglib prefix='s' uri='/struts-tags'%>3、在body中写表格并取值填充值具体代码如下:<%@ page language='java' contentType='text/html; charset=UTF-8' pageEncoding='UTF-8'%><%@ taglib prefix='s' uri='/struts-tags'%>
Student List Page
添加学生信息
没有任何学生信息
ID | 姓名 | 性别 | 年龄 | 出生日期 | 所属学院 | 操作 |
${id } | ${stuName } | ${stuSex } | ${stuAge } | ${college.collegeName } | 删除 编辑 |
同样用ajax删除学生信息1、引入2、写ajax:然后去实现删除功能
在StudentAction代码如下:private Integer id; public void setId(Integer id) { this.id = id; } public String delete() throws Exception { try { studentService.delete(id); inputStream = new ByteArrayInputStream('1'.getBytes('UTF-8')); } catch (Exception e) { e.printStackTrace(); try { inputStream = new ByteArrayInputStream('0'.getBytes('UTF-8')); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } } return 'ajax-success'; }
StudentDao和StudentService代码如下:StudentDao:public void delete(Integer id){ String hql = 'DELETE FROM Student s WHERE s.id = ?'; getSession().createQuery(hql).setInteger(0, id).executeUpdate(); }StudentService:public void delete(Integer id){ studentDao.delete(id); }
最终效果如下:添加与编辑将在SSH框架之浅析探讨(四)添加与删除中详细介绍大家可以看到我是一边写代码一边看效果一边写文档的,一步一步先做什么,再做什么,把他们连贯起来,还是有点累的,而且我现在长期对着电脑眼睛疼,因此原谅以下我分开写,没有一次全部写完,想要一次全写完那就只有把项目全做了,直接复制粘贴所有代码,那样对于初学者会有点懵。
本项目需要网络才能验证功能,想不连网运行需要导入JQuery,不用这个“”