实战3--板块管理的步骤
步骤:
一. 写实体类, 配置文件 Forum.hbm.xml, 在hibernate.cfg.xml里添加实体, 运行sessionFactory的test方法, 得到表格
二. 写action, 添加基本方法, 写jsp页面,配置struts.xml
三. 创建service接口和实现类,BaseAction里声明service实例
四. 填空实现action功能
一. 写实体类, 配置文件 Forum.hbm.xml, 在hibernate.cfg.xml里添加实体, 运行sessionFactory的test方法, 得到表格
1. 写 实体类 Forum.java:
package cn.itcast.oa.domain; public class Forum { private Long id; private String name; private String description; private int position; //代表用于指定顺序 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } }
2. 写配置文件 Forum.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.oa.domain"> <class name="Forum" table="itcast_forum"> <id name="id"> <generator class="native"></generator> </id> <property name="name" /> <property name="description" /> <property name="position" /> </class> </hibernate-mapping>
3. 在hibernate.cfg.xml里添加此实体
<!--3. mapping --> <mapping resource="cn/itcast/oa/domain/User.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Role.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Department.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Privilege.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Forum.hbm.xml" />
4. 通过sessionFactory生成表
package cn.itcast.oa.test; import org.hibernate.SessionFactory; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testSessionFactory() throws Exception{ SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory"); System.out.println(sessionFactory); } }
5. 确认生成表OK
二. 写action, 添加基本方法, 写jsp页面,配置struts.xml
1. 创建action, 注意继承 extends BaseAction<Forum>, 添加基本方法, 注意添加
@Controller @Scope("prototype")
package cn.itcast.oa.view.action; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import cn.itcast.oa.base.BaseAction; import cn.itcast.oa.domain.Forum;
@Controller @Scope("prototype") public class ForumManageAction extends BaseAction<Forum>{ /** 列表 */ public String list() throws Exception { return "list"; } /** 删除 */ public String delete() throws Exception { return "toList"; } /** 添加页面 */ public String addUI() throws Exception { return "saveUI"; } /** 添加 */ public String add() throws Exception { return "toList"; } /** 修改页面 */ public String editUI() throws Exception { return "saveUI"; } /** 修改 */ public String edit() throws Exception { return "toList"; } /** 上移 */ public String moveUp() throws Exception { return "toList"; } /** 下移 */ public String moveDown() throws Exception { return "toList"; } }
2. 创建jsp页面, 注意要新建目录 forumManageAction,
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ include file="/WEB-INF/jsp/public/commons.jspf" %> list
saveUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ include file="/WEB-INF/jsp/public/commons.jspf" %> saveUI
3. struts.xml添加板块管理模块
<!-- 板块管理 --> <action name="forumManage_*" class="forumManageAction" method="{1}"> <result name="list">/WEB-INF/jsp/forumManageAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/forumManageAction/saveUI.jsp</result> <result name="toList" type="redirectAction">forumManage_list</result> </action>
三. 创建service接口和实现类,配置实现类, BaseAction里声明service实例
1. 创建service的接口
ForumService.java接口, 记住要 extends DaoSupport<Forum>
package cn.itcast.oa.service; import cn.itcast.oa.base.DaoSupport; import cn.itcast.oa.domain.Forum; public interface ForumService extends DaoSupport<Forum>{ }
2. 写实现类 ForumServiceImpl.java, 记住要 extends DaoSupportImpl<Forum>implements ForumService
package cn.itcast.oa.service.impl; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.itcast.oa.base.DaoSupportImpl; import cn.itcast.oa.domain.Forum; import cn.itcast.oa.service.ForumService; @Service @Transactional @SuppressWarnings("unchecked") public class ForumServiceImpl extends DaoSupportImpl<Forum>implements ForumService { }
3. 给实现类配置
@Service @Transactional @SuppressWarnings("unchecked")
4. 在BaseAction里声明service实例
//******************service实例的声明****************** @Resource protected RoleService roleService; @Resource protected DepartmentService departmentService; @Resource protected UserService userService; @Resource protected PrivilegeService privilegeService; @Resource protected ForumService forumService;
四. 填空实现action功能
1. 首先list功能
/** 列表 */ public String list() throws Exception { List<Forum> forumlist = forumService.findAll(); ActionContext.getContext().put("forumlist", forumlist); return "list"; }
2. delete功能
/** 删除 */ public String delete() throws Exception { forumService.delete(model.getId()); return "toList"; }
3. addUI功能不需要添加什么
4. add功能
/** 添加 */ public String add() throws Exception { forumService.save(model); return "toList"; }
5. editUI功能
/** 修改页面 */ public String editUI() throws Exception { //准备回显的数据 Forum forum = forumService.getById(model.getId()); ActionContext.getContext().getValueStack().push(forum); return "saveUI"; }
6. edit功能:
1. 从数据库中取出原对象
2. 设置要修改的属性
3. 更新到数据库
/** 修改 */ public String edit() throws Exception { //1. 从数据库中取出原对象 Forum forum = forumService.getById(model.getId()); //2. 设置要修改的属性 forum.setName(model.getName()); forum.setDescription(model.getDescription()); //3. 更新到数据库 forumService.update(model); return "toList"; }
7. 上移下移功能
/** 上移 */ public String moveUp() throws Exception { forumService.moveUp(model.getId()); return "toList"; } /** 下移 */ public String moveDown() throws Exception { forumService.moveDown(model.getId()); return "toList"; }