系统管理模块_岗位管理_改进_使用ModelDroven方案_套用美工写好的页面效果_添加功能与修改功能使用同一个页面

改进_使用ModelDroven方案

@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport implements ModelDriven<Role>{//把ModelDriven放到栈顶,帮我们封装参数 //在Action里面要用到Service,用注解@Resource,另外在RoleServiceImpl类上要添加注解@Service
@Resource
private RoleService roleService;
private Role model = new Role(); public Role getModel() {
return model;
} /*private Long id; //实现了ModelDriven,这些属性及setget方法就不用写了
private String name; //栈顶的Role中有了这些属性
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;
}*/ /**
* 列表
*/
public String list() {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);//用ognl里的#号来获取map的东西
return "list";
} /**
* 删除
*/
public String delete() {
roleService.delete(model.getId()); //取参数可以从model里获取了
return "toList";
}
/**
* 添加页面
*/
public String addUI() {
return "addUI";
}
/**
* 添加
*/
public String add() {
/*//封装到对象中
Role role = new Role();
role.setName(model.getName());//名称和说明怎么得到,跟id一样声明字段,setget方法
role.setDescription(model.getDescription()); //保存到数据库中
roleService.save(role);*/ //添加的方法可以简化成一行代码
roleService.save(model); return "toList";
}
/**
* 修改页面
*/
public String editUI() { //准备回显的数据
Role role =roleService.getById(model.getId());
ActionContext.getContext().getValueStack().push(role);//放到栈顶,从栈顶找对象 return "editUI";
}
/**
* 修改
*/
public String edit() {
//1.从数据库中获取原对象
Role role = roleService.getById(model.getId());//role是根据id来的 //2.设置要修改的属性
role.setName(model.getName());
role.setDescription(model.getDescription());
//3.更新到数据库
roleService.update(role); return "toList";
}
}

套用美工写好的页面效果

图片样式拷过来并指定正确的路径

从静态页面中拷贝这两个文件夹

系统管理模块_岗位管理_改进_使用ModelDroven方案_套用美工写好的页面效果_添加功能与修改功能使用同一个页面

分别在list.jsp、addUI.jsp、editUI.jsp中替换美工做好的页面代码

系统管理模块_岗位管理_改进_使用ModelDroven方案_套用美工写好的页面效果_添加功能与修改功能使用同一个页面

系统管理模块_岗位管理_改进_使用ModelDroven方案_套用美工写好的页面效果_添加功能与修改功能使用同一个页面

修改路径

系统管理模块_岗位管理_改进_使用ModelDroven方案_套用美工写好的页面效果_添加功能与修改功能使用同一个页面

添加循环<s:iterator>及修改里面的name,description和删除修改添加的超链接

修改完的list.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>岗位列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
<script type="text/javascript">
</script>
</head>
<body> <div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 岗位管理
</div>
<div id="Title_End"></div>
</div>
</div> <div id="MainArea">
<table cellspacing="0" cellpadding="0" class="TableStyle"> <!-- 表头-->
<thead>
<tr align="CENTER" valign="MIDDLE" id="TableTitle">
<td width="200px">岗位名称</td>
<td width="300px">岗位说明</td>
<td>相关操作</td>
</tr>
</thead> <!--显示数据列表-->
<tbody id="TableData" class="dataContainer" datakey="roleList">
<s:iterator>
<tr class="TableDetail1 template">
<td>${name}&nbsp;</td>
<td>${description}&nbsp;</td>
<td>
<s:a action="role_delete?id=%{id}" onclick="return confirm('确定要删除吗')">删除</s:a>,
<s:a action="role_editUI?id=%{id}">修改</s:a>
<a href="setPrivilegeUI.html">设置权限</a>
</td>
</tr>
</s:iterator>
</tbody>
</table> <!-- 其他功能超链接 -->
<div id="TableTail">
<div id="TableTail_inside">
<s:a action="role_addUI"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></s:a>
</div>
</div>
</div>
</body>
</html>

同样addUI.jsp也要按照这样更改

修改好的addUI.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>岗位设置</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
<script type="text/javascript">
</script>
</head>
<body> <!-- 标题显示 -->
<div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 岗位设置
</div>
<div id="Title_End"></div>
</div>
</div> <!--显示表单内容-->
<div id="MainArea">
<s:form action="role_add">
<div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1">
<IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 岗位信息 </DIV> -->
</div> <!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="100">岗位名称</td>
<td><s:textfield name="name" cssClass="InputStyle" /> *</td>
</tr>
<tr>
<td>岗位说明</td>
<td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td>
</tr>
</table>
</div>
</div> <!-- 表单操作 -->
<div id="InputDetailBar">
<input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/>
<a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a>
</div>
</s:form>
</div>
</body>
</html>

修改好的editUI.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>岗位设置</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
<script type="text/javascript">
</script>
</head>
<body> <!-- 标题显示 -->
<div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 岗位设置
</div>
<div id="Title_End"></div>
</div>
</div> <!--显示表单内容-->
<div id="MainArea">
<s:form action="role_edit">
<s:hidden name="id"></s:hidden> <div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1">
<IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 岗位信息 </DIV> -->
</div> <!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="100">岗位名称</td>
<td><s:textfield name="name" cssClass="InputStyle" /> *</td>
</tr>
<tr>
<td>岗位说明</td>
<td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td>
</tr>
</table>
</div>
</div> <!-- 表单操作 -->
<div id="InputDetailBar">
<input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/>
<a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a>
</div>
</s:form>
</div>
</body>
</html>

添加功能与修改功能使用同一个页面

添加的时候不用隐藏id,提交的地址不一样,其他差不多一样

addUI.jsp和editUI.jsp保留一个并改名为savaUI.jsp

同时在struts.xml文件中要修改

<result name="saveUI">/WEB-INF/jsp/roleAction/saveUI.jsp</result>

下一步要修改RoleAction.java中的return

return "saveUI";

saveUI.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>岗位设置</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
<script type="text/javascript">
</script>
</head>
<body> <!-- 标题显示 -->
<div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 岗位设置
</div>
<div id="Title_End"></div>
</div>
</div> <!--显示表单内容-->
<div id="MainArea"> <s:form action="role_%{ id == null ? 'add' : 'edit' }">
<s:hidden name="id"></s:hidden> <div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1">
<IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 岗位信息 </DIV> -->
</div> <!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="100">岗位名称</td>
<td><s:textfield name="name" cssClass="InputStyle" /> *</td>
</tr>
<tr>
<td>岗位说明</td>
<td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td>
</tr>
</table>
</div>
</div> <!-- 表单操作 -->
<div id="InputDetailBar">
<input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/>
<a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a>
</div>
</s:form>
</div>
</body>
</html>

================================= CRUD的Action方法的模板===================================

/** 列表 */

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";

}

==========================================

<!-- users属性,本类与User的一对多 -->

格式:?属性,本类与?的?

?1属性名

?2关联对类型

?3关系

多对一:

  <many-to-one name="" class="" column=""></many-to-one>

一对多(Set):

  <set name="">

    <key column=""></key>

    <one-to-many class=""/>

  </set>

多对多(Set):

  <set name="" table="">

    <key column=""></key>

    <many-to-many class="" column=""></many-to-many>

  </set>

上一篇:JS调用本地应用程序


下一篇:AVL树(Java实现)