xtend delet 实现 result action == b- actions asto
设计实体 --> JavaBean --> hbm.xml --> 建表
设计Role实体
- public class Role {
- private Long id;
- private String name;
- private String description;
- 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;
- }
- }
映射文件
- <hibernate-mapping package="cn.itcast.oa.domain">
- <class name="Role" table="itcast_role">
- <id name="id">
- <generator class="native" />
- </id>
- <property name="name"></property>
- <property name="description"></property>
- </class>
- </hibernate-mapping>
加到hibernate.cfg.xml配置中,让它知道有这个映射文件才能建表
<mapping resource="cn/itcast/oa/domain/Role.hbm.xml" />
运行测试类,创建SessionFactory时就会创建itcast_role表
- //测试SessionFactory
- @Test
- public void testSessionFactory() throws Exception {
- SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
- System.out.println(sessionFactory);
- }
2,分析有几个功能,对应几个请求。
添加、修改、删除成功后 要重定向到列表功能,这样在刷新页面时才不会出现“又做一次增、删、改”的操作。
列表与删除功能都是只有一个请求
添加与修改功能都是有两个请求
增删改查共4个功能,6个请求,需要在Action中有6个对应的处理方法。
作用 |
方法名 |
返回值 |
对应的JSP页面 |
列表 |
list() |
list |
list.jsp |
删除 |
delete() |
toList |
|
添加页面 |
addUI() |
addUI |
addUI.jsp |
添加 |
add() |
toList |
|
修改页面 |
editUI() |
editUI |
editUI.jsp |
修改 |
edit() |
toList |
toList的配置为:type="redirectAction" actionName=“xxAction_list”
<result name="toList" type="redirectAction">role_list</result>
===================================================================
请求数量地址栏
转发1 不变在一个功能之间的来回跳转
重定向 2 变化在多个功能之间的跳转
role_* ---> {1}代表第一个方法
*号代表
role_list list
role_addUI addUI
role_deletedelete
3,实现功能:
1,写Action类,写Action中的方法,确定Service中的方法。
先完成列表和删除功能
- @Controller@Scope("prototype") public class RoleAction extends ActionSupport {
- //在Action里面要用到Service,用注解@Resource,另外在RoleServiceImpl类上要添加注解@Service
- @Resource private RoleService roleService;
- private Long id;
- /**
- * 列表
- */
- public String list() {
- List < Role > roleList = roleService.findAll();
- ActionContext.getContext().put("roleList", roleList); //用ognl里的#号来获取map的东西
- return "list";
- }
- /**
- * 删除
- */
- public String delete() {
- roleService.delete(id);
- return "toList";
- }
- /**
- * 添加页面
- */
- public String addUI() {
- return "addUI";
- }
- /**
- * 添加
- */
- public String add() {
- return "toList";
- }
- /**
- * 修改页面
- */
- public String editUI() {
- return "editUI";
- }
- /**
- * 修改
- */
- public String edit() {
- return "toList";
- }
- //--------------
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- }
在struts.xml文件中配置
- <!-- 岗位管理 -->
- <action name="role_*" class="roleAction" method="{1}">
- <result name="list">/web-INF/jsp/roleAction/list.jsp</result>
- <result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result>
- <result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result>
- <result name="toList" type="redirectAction">role_list</result>
- </action>
2,写Service方法,确定Dao中的方法。
先完成列表和删除功能
RoleService.java
- //接口中只有方法的声明,没有方法的实现
- public interface RoleService {
- //查询所有
- List < Role > findAll();
- //删除
- void delete(Long id);
- }
RoleServiceImpl.java
- //在Action中要调用Service,要写下面两个注解
- @Service
- @Transactional //业务层要管理实务,控制开关事务
- public class RoleServiceImpl implements RoleService{
- //Service里要调用Dao,得到它通过注入
- @Resource
- private RoleDao roleDao;
- public List<Role> findAll() {
- return roleDao.findAll();
- }
- public void delete(Long id) {
- roleDao.delete(id);
- }
- }
3,写Dao方法。
RoleDao.java
- public interface RoleDao extends BaseDao<Role>{
- }
列表与删除等公共方法都在继承的BaseDao里有
RoleDaoImpl.java
- //放到容器里面,以供Service使用Dao的接口与实现类
- @Repository
- public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao{
- }
4,写JSP
list.jsp
- <%@ taglib prefix="s" uri="/struts-tags" %><!-- 引入struts标签 -->
- <body>
- <s:iterator value="#roleList"><!-- 得到里面的集合 -->
- <s:property value="id"/>,
- <s:property value="name"/>,
- <s:property value="description"/>,
- <s:a action="role_delete?id=%{id}">删除</s:a>
- </s:iterator>
- </body>
访问:http://localhost:8080/ItcastOA/role_list.action即可看到列表
实现添加和修改功能
1,写Action类,写Action中的方法,确定Service中的方法。
RoleAction.java
- @Controller@Scope("prototype") public class RoleAction extends ActionSupport {
- //在Action里面要用到Service,用注解@Resource,另外在RoleServiceImpl类上要添加注解@Service
- @Resource private RoleService roleService;
- private Long id;
- private String name;
- private String description;
- /**
- * 列表
- */
- public String list() {
- List < Role > roleList = roleService.findAll();
- ActionContext.getContext().put("roleList", roleList); //用ognl里的#号来获取map的东西
- return "list";
- }
- /**
- * 删除
- */
- public String delete() {
- roleService.delete(id);
- return "toList";
- }
- /**
- * 添加页面
- */
- public String addUI() {
- return "addUI";
- }
- /**
- * 添加
- */
- public String add() {
- //封装到对象中
- Role role = new Role();
- role.setName(name); //名称和说明怎么得到,跟id一样声明字段,setget方法
- role.setDescription(description);
- //保存到数据库中
- roleService.save(role);
- return "toList";
- }
- /**
- * 修改页面
- */
- public String editUI() {
- //准备回显的数据
- Role role = roleService.getById(id);
- //ActionContext.getContext().getValueStack().push(role);//放到栈顶
- this.name = role.getName();
- this.description = role.getDescription();
- return "editUI";
- }
- /**
- * 修改
- */
- public String edit() {
- //1.从数据库中获取原对象
- Role role = roleService.getById(id); //role是根据id来的
- //2.设置要修改的属性
- role.setName(name);
- role.setDescription(description);
- //3.更新到数据库
- roleService.update(role);
- return "toList";
- }
- //--------------
- 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;
- }
- }
2,写Service方法,确定Dao中的方法。
RoleService.java
- //接口中只有方法的声明,没有方法的实现
- public interface RoleService {
- //查询所有
- List < Role > findAll();
- //删除
- void delete(Long id);
- //保存
- void save(Role role);
- Role getById(Long id);
- //更新
- void update(Role role);
- }
3,写Dao方法。
RoleDao.java
- public interface RoleDao extends BaseDao<Role>{
- }
4,写JSP
addUI.jsp
- <body>
- <s:form action="role_add"><!-- 提交的地址 -->
- <s:textfield name="name"></s:textfield>
- <s:textarea name="description"></s:textarea>
- <s:submit value="提交"></s:submit>
- </s:form>
- </body>
editUI.jsp
- <s:form action="role_edit"><!-- 提交的地址 -->
- <s:hidden name="id"></s:hidden><!-- 修改要给出隐藏的id -->
- <s:textfield name="name"></s:textfield>
- <s:textarea name="description"></s:textarea>
- <s:submit value="提交"></s:submit>
- </s:form>
访问:http://localhost:8080/ItcastOA/role_list.action验证即可
系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体
来源: http://www.bubuko.com/infodetail-2390656.html