我们这里注重实现功能,对页面不进行布局。
一、分析增删改查的功能:
- 添加、修改、删除成功后 要重定向到列表功能,这样在刷新页面时才不会出现“又做一次增、删、改”的操作。
- 列表与删除功能都是只有一个请求。
- 添加与修改功能都是有两个请求。
二、代码实现:
RoleAction.java
package cn.xbmu.oa.view.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.xbmu.oa.domain.Role;
import cn.xbmu.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class RoleAction extends ActionSupport {
/**查询*/
public String list() throws Exception {
return "list";
}
/**添加页面*/
public String addUI() throws Exception {
return "addUI";
}
/**添加*/
public String add() throws Exception {
return "toList";
}
/**删除*/
public String delete() throws Exception {
return "toList";
}
/**修改页面*/
public String editUI() throws Exception {
return "editUI";
}
/**修改*/
public String edit() throws Exception {
return "toList";
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置为开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 配置扩展名为action -->
<constant name="struts.action.extension" value="action"/>
<package name="default" namespace="/" extends="struts-default">
<!-- 测试用的action,当与Spring整合后,class属性写的就是bean的名称-->
<action name="test" class="testAction">
<result name="success">/test.jsp</result>
</action>
<!-- 岗位管理 -->
<action name="roleAction_*" 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">roleAction_list</result>
</action>
</package>
</struts>
查询与删除:
RoleAction.java
package cn.xbmu.oa.view.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.xbmu.oa.domain.Role;
import cn.xbmu.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport {
@Resource
private RoleService roleService;
private Long id;
/**查询*/
public String list() throws Exception {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);
return "list";
}
/**添加页面*/
public String addUI() throws Exception {
return "addUI";
}
/**添加*/
public String add() throws Exception {
return "toList";
}
/**删除*/
public String delete() throws Exception {
roleService.delete(id);
return "toList";
}
/**修改页面*/
public String editUI() throws Exception {
return "editUI";
}
/**修改*/
public String edit() throws Exception {
return "toList";
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
RoleService.java
package cn.xbmu.oa.service;
import java.util.List;
import cn.xbmu.oa.domain.Role;
public interface RoleService {
/**
* 查询所有岗位
* @return
*/
List<Role> findAll();
void delete(Long id);
}
RoleServiceImpl.java
package cn.xbmu.oa.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.xbmu.oa.dao.RoleDao;
import cn.xbmu.oa.domain.Role;
import cn.xbmu.oa.service.RoleService;
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
@Resource
private RoleDao roleDao;
public List<Role> findAll() {
return roleDao.findAll();
}
public void delete(Long id) {
roleDao.delete(id);
}
}
RoleDao.java
package cn.xbmu.oa.dao;
import cn.xbmu.oa.base.BaseDao;
import cn.xbmu.oa.domain.Role;
public interface RoleDao extends BaseDao<Role>{
}
RoleDaoImpl.java
package cn.xbmu.oa.dao.impl;
import org.springframework.stereotype.Repository;
import cn.xbmu.oa.base.BaseDaoImpl;
import cn.xbmu.oa.dao.RoleDao;
import cn.xbmu.oa.domain.Role;
@Repository
public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao{
}
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%--
<s:iterator value="#roleList">
<s:property value="id"/>,
<s:property value="name"/>,
<s:property value="description"/>,
<a href="roleAction_delete.action?id=<s:property value='id'/>" οnclick="return confirm('您确定删除吗?')">删除</a><br/>
</s:iterator>
--%>
<s:iterator value="#roleList">
${id},
${name},
${description},
<%--
<a href="${pageContext.request.contextPath}/roleAction_delete.action?id=${id}" οnclick="return confirm('您确定删除吗?')">删除</a>
--%>
<%--
<s:a action="roleAction_delete">
<s:param name="id" value="%{id}"></s:param>
删除
</s:a>
--%>
<s:a action="roleAction_delete?id=%{id}" οnclick="return confirm('您确定删除吗')">删除</s:a>
</s:iterator>
</body>
</html>
运行效果:
修改和添加:
RoleAction.java
package cn.xbmu.oa.view.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.xbmu.oa.domain.Role;
import cn.xbmu.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport {
@Resource
private RoleService roleService;
private Long id;
private String name;
private String description;
/**查询*/
public String list() throws Exception {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);
return "list";
}
/**删除*/
public String delete() throws Exception {
roleService.delete(id);
return "toList";
}
/**添加页面*/
public String addUI() throws Exception {
return "addUI";
}
/**添加*/
public String add() throws Exception {
//得到参数,封装成对象
Role role = new Role();
role.setName(name);
role.setDescription(description);
//保存到数据库中
roleService.save(role);
return "toList";
}
/**修改页面*/
public String editUI() throws Exception {
Role role = roleService.getById(id);
name = role.getName();
description = role.getDescription();
return "editUI";
}
/**修改*/
public String edit() throws Exception {
//从数据库中取出原对象
Role role = roleService.getById(id);
//设置要修改的属性
role.setName(name);
role.setDescription(description);
//更新到数据库中
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;
}
}
RoleService.java
package cn.xbmu.oa.service;
import java.util.List;
import cn.xbmu.oa.domain.Role;
public interface RoleService {
/**
* 查询所有岗位
* @return
*/
List<Role> findAll();
void delete(Long id);
void save(Role role);
Role getById(Long id);
void update(Role role);
}
RoleServiceImpl.java
package cn.xbmu.oa.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.xbmu.oa.dao.RoleDao;
import cn.xbmu.oa.domain.Role;
import cn.xbmu.oa.service.RoleService;
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
@Resource
private RoleDao roleDao;
public List<Role> findAll() {
return roleDao.findAll();
}
public void delete(Long id) {
roleDao.delete(id);
}
public void save(Role role) {
roleDao.save(role);
}
public Role getById(Long id) {
return roleDao.getById(id);
}
public void update(Role role) {
roleDao.update(role);
}
}
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%--
<s:iterator value="#roleList">
<s:property value="id"/>,
<s:property value="name"/>,
<s:property value="description"/>,
<a href="roleAction_delete.action?id=<s:property value='id'/>" οnclick="return confirm('您确定删除吗?')">删除</a><br/>
</s:iterator>
--%>
<s:iterator value="#roleList">
${id},
${name},
${description},
<%--
<a href="${pageContext.request.contextPath}/roleAction_delete.action?id=${id}" οnclick="return confirm('您确定删除吗?')">删除</a>
--%>
<%--
<s:a action="roleAction_delete">
<s:param name="id" value="%{id}"></s:param>
删除
</s:a>
--%>
<s:a action="roleAction_delete?id=%{id}" οnclick="return confirm('您确定删除吗')">删除</s:a>
<s:a action="roleAction_editUI?id=%{id}">编辑</s:a><br/>
</s:iterator><br/>
<s:a action="roleAction_addUI">添加</s:a>
</body>
</html>
addUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:form action="roleAction_add">
<s:textfield label="岗位名称" name="name"/>
<s:textarea label="岗位说明" name="description"/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
editUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:form action="roleAction_edit">
<s:hidden name="id"/>
<s:textfield label="岗位名称" name="name"/>
<s:textarea label="岗位说明" name="description"/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
运行效果:
三、Struts中的模型驱动的使用:
RoleAction.java
package cn.xbmu.oa.view.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.xbmu.oa.domain.Role;
import cn.xbmu.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport implements ModelDriven<Role>{
@Resource
private RoleService roleService;
private Role model = new Role();
public Role getModel() {
return model;
}
/**查询*/
public String list() throws Exception {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);
return "list";
}
/**删除*/
public String delete() throws Exception {
roleService.delete(model.getId());
return "toList";
}
/**添加页面*/
public String addUI() throws Exception {
return "addUI";
}
/**添加*/
public String add() throws Exception {
//保存到数据库中
roleService.save(model);
return "toList";
}
/**修改页面*/
public String editUI() throws Exception {
Role role = roleService.getById(model.getId());
//放到栈顶
ActionContext.getContext().getValueStack().push(role);
return "editUI";
}
/**修改*/
public String edit() throws Exception {
//从数据库中取出原对象
Role role = roleService.getById(model.getId());
//设置要修改的属性
role.setName(model.getName());
role.setDescription(model.getDescription());
//更新到数据库中
roleService.update(role);
return "toList";
}
}