目录
2. web层,在DiaryTypeServlet里面,调用前面的方法;
1. dao层,写添加与修改的方法;
这里面我们在DiaryTypeDao主要写添加日志类别与修改日志类别的方法;
//日志类别添加
public int diaryTypeAdd(Connection con,DiaryType diaryType) throws Exception{
String sql="insert into t_diarytype values(null,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,diaryType.getTypeName());
return pstmt.executeUpdate();
}
//日志类别修改
public int diaryTypeUpdate(Connection con,DiaryType diaryType) throws Exception{
String sql="update t_diarytype set typeName=? where diaryTypeId=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,diaryType.getTypeName());
pstmt.setInt(2,diaryType.getDiaryTypeId());
return pstmt.executeUpdate();
}
2. web层,在DiaryTypeServlet里面,调用前面的方法;
这里我们在DiaryTypeServlet里面调用前面的添加与修改方法;
注意:这里不管是添加日志类别还是修改日志,我们最后都会显示出日志类别信息的页面,也即diaryTypeSave.jsp页面;
diaryTypeSave.jsp页面:
我们先新建一个diaryTypeSave.jsp页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 使用JSTL标签需要引入的头文件 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!-- 使用fmt 对时间进行格式化的时候需要引入的头文件 -->
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head>
<title>Title</title>
<script type="text/javascript">
function checkForm(){
var typeName=document.getElementById("typeName").value;
if(typeName==null||typeName==""){
document.getElementById("error").innerHTML="类别名称不能为空!";
return false;
}
return true;
}
</script>
</head>
<body>
<div class="data_list">
<div class="data_list_title">
<c:choose>
<c:when test="${diaryType.diaryTypeId!=null }">
<img src="${pageContext.request.contextPath}/images/diary_type_edit_icon.png"/>
修改日记类别
</c:when>
<c:otherwise>
<img src="${pageContext.request.contextPath}/images/diaryType_add_icon.png"/>
添加日记类别
</c:otherwise>
</c:choose>
</div>
<form action="diaryType?action=save" method="post" onsubmit="return checkForm()">
<div class="diaryType_form" >
<table align="center">
<tr>
<td>类别名称:</td>
<td><input type="text" id="typeName" name="typeName" value="${diaryType.typeName }" style="margin-top:5px;height:30px;" /></td>
</tr>
<tr>
<%--日志类别主键,设置为隐藏域--%>
<input type="hidden" id="diaryTypeId" name="diaryTypeId" value="${diaryType.diaryTypeId }"/>
<td><input type="submit" class="btn btn-primary" value="保存"/></td>
<td><button class="btn btn-primary" type="button" onclick="javascript:history.back()">返回</button> <font id="error" color="red">${error }</font> </td>
</tr>
</table>
</div>
</form>
</div>
</body>
</html>
而判断是添加日志类别还是修改日志类别的方法是,我们判断diaryTypeId是否为空;
在DiaryTypeServlet里面:
//预处理日志类别的方法
private void diaryTypePreSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String diaryTypeId=request.getParameter("diaryTypeId");
Connection con=null;
try {
if(StringUtil.isNotEmpty(diaryTypeId)){
con=dbUtil.getCon();
DiaryType diaryType=diaryTypeDao.diaryTypeShow(con,diaryTypeId);
request.setAttribute("diaryType",diaryType);
}
//将得到的mainPage和diaryTypeSave连接起来
request.setAttribute("mainPage", "diaryType/diaryTypeSvae.jsp");
//内部转发
request.getRequestDispatcher("mainTemp.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//添加日志类别的方法
private void diaryTypeSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String diaryTypeId=request.getParameter("diaryTypeId");
String typeName=request.getParameter("typeName");
DiaryType diaryType=new DiaryType(typeName);
if(StringUtil.isNotEmpty(diaryTypeId)){
diaryType.setDiaryTypeId(Integer.parseInt(diaryTypeId));
}
Connection con=null;
try {
con=dbUtil.getCon();
int saveNum;
if(StringUtil.isNotEmpty(diaryTypeId)){
saveNum=diaryTypeDao.diaryTypeUpdate(con, diaryType);
} else{
saveNum=diaryTypeDao.diaryTypeAdd(con, diaryType);
}
if(saveNum>0){
//添加成功,跳转到日志列表页面
request.getRequestDispatcher("diaryType?action=list").forward(request, response);
}else{
request.setAttribute("diaryType", diaryType);
request.setAttribute("error", "保存失败!");
//将得到的mainPage和diarySave连接起来
request.setAttribute("mainPage", "diaryType/diaryTypeSvae.jsp");
//内部转发
request.getRequestDispatcher("mainTemp.jsp").forward(request, response);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
当然,这里我们在前面的dao层里面还要添加一个显示日志类别信息的方法diaryTypeShow;
//根据日志Id显示日志类别
public DiaryType diaryTypeShow(Connection con,String diaryTypeId) throws Exception{
DiaryType diaryType=new DiaryType();
String sql="select * from t_diaryType where diaryTypeId=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,diaryTypeId);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
diaryType.setDiaryTypeId(rs.getInt("diaryTypeId"));
diaryType.setTypeName(rs.getString("typeName"));
}
return diaryType;
}
3.我们在前台页面添加一下链接;
在diaryTypeList.jsp页面上:
添加日志类别链接:
<button class="btn btn-primary btn-success" type="button" onclick="javascript:window.location='diaryType?action=preSave'">添加日志类别</button>
修改日志类别链接:
<button class="btn btn-primary btn-info" type="button" onclick="javascript:window.location='diaryType?action=preSave&diaryTypeId=${diaryType.diaryTypeId }'">修改</button>
这两个链接传递给后台的都是action=preSave这个请求;
而修改日志类别的链接会多传递一个diaryTypeId,给后台进行判断看看是修改还是添加;
最后我们在diaryTypeServlet里面的添加日志的方法里面对diaryTypeId进行判断,
若为空则是添加操作,不为空则是修改操作;
4. 测试;