求助各位大侠
本人用Struts1 写了一个学生信息的增删改查功能,各页面直接的相互转跳(根据action)已没有问题,新增一个学生的功能也没有问题(可以在页面输入信息,然后点“按钮“可以将数据记录保存到数据库中),但是修改和删除的功能就不能实现了,原因就是从页面中不能获取到 id ,因为修改和删除都是根据id的,
主要就是让各位看一下 业务逻辑的方法有没有问题,还有就是id是如何传给业务逻辑的
现将需要各位指导的部分代码粘出来,希望大家给予指导
数据库字段:id,name,sex,birth,score 数据库用的MYSQL5.01、studentDao.java
只是粘贴了修改和删除的业务逻辑代码
public class StudentDao {
//3、修改(更新)学生信息
public boolean updateStudent(StudentForm student){
boolean b = false;
PreparedStatement pstm = null;
try {
String sql = "update student set name=?,sex=?,birth=?,score=? where id=?";
pstm = (PreparedStatement)new DBAccess().getConnection().prepareStatement(sql);
pstm.setString(1, student.getName());
pstm.setString(2, student.getSex());
pstm.setDate(3, student.getBirth());
pstm.setFloat(4, student.getScore());
pstm.setInt(5, student.getId());
int rowNum = pstm.executeUpdate();
if(rowNum > 0){
b = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
//4、删除学生信息
public boolean deleteStudent(StudentForm student){
boolean b = false;
PreparedStatement pstm = null;
try {
String sql = "delete from student where id=?";
pstm = (PreparedStatement)new DBAccess().getConnection().prepareStatement(sql);
pstm.setInt(1, student.getId());
System.out.println(student.getId());
int rowNum = pstm.executeUpdate();
if(rowNum > 0){
b = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}
2、deleteStudentAction.java
public class DeleteStudentAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
StudentForm student = (StudentForm)form;
StudentDao studentDao = new StudentDao();
String returnURLKeyWord = "deleteFailure";
boolean successful = false;
successful = studentDao.deleteStudent(student);
if(successful == true){
returnURLKeyWord = "deleteSuccessful";
}
return mapping.findForward(returnURLKeyWord);
}}
3、updateStudentAction.java
public class UpdateStudentAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
StudentForm student = (StudentForm)form;
StudentDao studentDao = new StudentDao();
boolean updateSuccess = false;
updateSuccess = studentDao.updateStudent(student);
String returnURLKeyWord = "updateStudentFialure";
if(updateSuccess == true){
returnURLKeyWord = "updateStudentSuccessful";
}
return mapping.findForward(returnURLKeyWord);
}}
4、struts-config.xml的部分代码
<action-mappings>
<action path="/UpdateStudentAction"
type="cn.giant.tbl.student.action.UpdateStudentAction"
name="studentForm">
<forward name="updateStudentFialure" path="/update.jsp"></forward>
<forward name="updateStudentSuccessful" path="/show.jsp"></forward>
</action>
<action path="/UpdateForwardAction"
type="cn.giant.tbl.student.action.UpdateForwardAction"
name="studentForm">
<forward name="updateStudent" path="/update.jsp"></forward>
</action>
<action path="/DeleteStudentAction"
type="cn.giant.tbl.student.action.DeleteStudentAction"
name="studentForm">
<forward name="deleteSuccessful" path="/show.jsp"></forward>
<forward name="deleteFailure" path="/deletefail.jsp"></forward>
</action>
</action-mappings>
5、jsp页面部分代码 show.jsp
我的实现原理:show.jsp是一个展示数据记录的页面,在每一条数据记录后面都有两个按钮,一个是修改,一个是删除。
点击修改按钮时,页面转跳到update.jsp(该功能已经实现),然后在update.jsp页面中的tesx框中输入要修改的信息(没有id这个框,id是不能修改的,修改记录也是根据id的),然后点击“提交修改”保存。但是问题就在这里了,id获取不到。
点击删除按钮时,直接删除该条记录,删除成功就重新转跳到show.jsp(相当于刷新吧),删除失败就转跳到deletefail.jsp。现在也是和修改一样的问题,id获取不到,所以每次我点击删除就直接跳到deletefail.jsp了。
<body>
<%
String sql = "select * from student";
List students = new StudentDao().findBySql(sql);
%>
<center>
<table border="3">
<tr>
<th colspan="8">
学生信息表
</th>
</tr>
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
性别
</td>
<td>
生日
</td>
<td>
分数
</td>
<td colspan="2" align="center">
操作
</td>
</tr>
<%
for (int i = 0; i < students.size(); i++) {
StudentForm student = new StudentForm();
student = (StudentForm) students.get(i);
%>
<tr>
<td>
<%=student.getId()%>
</td>
<td>
<%=student.getName()%>
</td>
<td>
<%=student.getSex()%>
</td>
<td>
<%=student.getBirth()%>
</td>
<td>
<%=student.getScore()%>
</td>
<td>
<form action="<%=basePath%>/UpdateForwardAction.do" method="post">
<input type="submit" value="修改">
</form>
</td>
<td>
<form action="<%=basePath%>/DeleteStudentAction.do" method="post">
<input type="submit" value="删除">
</form>
</td>
</tr>
<%
}
%>
</table>
<form action="<%=basePath%>/AddForwardAction.do" method="post">
<input type="submit" value="添加学生">
</form> </center> </body>
6、update.jsp部分代码
注意这个里面的id是隐藏的,是不允许修改的,本来是希望这个id是从show.jsp点击“修改”按钮时传过来的,但是没有成功,所以希望大家看看怎么办
<body>
<form action="<%=basePath%>/UpdateStudentAction" method="post">
<table border="3" align="center">
<tr>
<th colspan="2">
修改信息
</th>
</tr>
<tr>
<td>
<input type="hidden" name="id">
</td>
</tr>
<tr>
<td>
姓名:
</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>
性别:
</td>
<td>
<input type="text" name="sex">
</td>
</tr>
<tr>
<td>
生日:
</td>
<td>
<input type="text" name="birth">
</td>
</tr>
<tr>
<td>
分数:
</td>
<td>
<input type="text" name="score">
</td>
</tr>
</table>
<table align="center">
<tr>
<td>
<input type="submit" value="提交修改">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
本人用Struts1 写了一个学生信息的增删改查功能,各页面直接的相互转跳(根据action)已没有问题,新增一个学生的功能也没有问题(可以在页面输入信息,然后点“按钮“可以将数据记录保存到数据库中),但是修改和删除的功能就不能实现了,原因就是从页面中不能获取到 id ,因为修改和删除都是根据id的,
主要就是让各位看一下 业务逻辑的方法有没有问题,还有就是id是如何传给业务逻辑的
现将需要各位指导的部分代码粘出来,希望大家给予指导
数据库字段:id,name,sex,birth,score 数据库用的MYSQL5.01、studentDao.java
只是粘贴了修改和删除的业务逻辑代码
public class StudentDao {
//3、修改(更新)学生信息
public boolean updateStudent(StudentForm student){
boolean b = false;
PreparedStatement pstm = null;
try {
String sql = "update student set name=?,sex=?,birth=?,score=? where id=?";
pstm = (PreparedStatement)new DBAccess().getConnection().prepareStatement(sql);
pstm.setString(1, student.getName());
pstm.setString(2, student.getSex());
pstm.setDate(3, student.getBirth());
pstm.setFloat(4, student.getScore());
pstm.setInt(5, student.getId());
int rowNum = pstm.executeUpdate();
if(rowNum > 0){
b = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
//4、删除学生信息
public boolean deleteStudent(StudentForm student){
boolean b = false;
PreparedStatement pstm = null;
try {
String sql = "delete from student where id=?";
pstm = (PreparedStatement)new DBAccess().getConnection().prepareStatement(sql);
pstm.setInt(1, student.getId());
System.out.println(student.getId());
int rowNum = pstm.executeUpdate();
if(rowNum > 0){
b = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}
2、deleteStudentAction.java
public class DeleteStudentAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
StudentForm student = (StudentForm)form;
StudentDao studentDao = new StudentDao();
String returnURLKeyWord = "deleteFailure";
boolean successful = false;
successful = studentDao.deleteStudent(student);
if(successful == true){
returnURLKeyWord = "deleteSuccessful";
}
return mapping.findForward(returnURLKeyWord);
}}
3、updateStudentAction.java
public class UpdateStudentAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
StudentForm student = (StudentForm)form;
StudentDao studentDao = new StudentDao();
boolean updateSuccess = false;
updateSuccess = studentDao.updateStudent(student);
String returnURLKeyWord = "updateStudentFialure";
if(updateSuccess == true){
returnURLKeyWord = "updateStudentSuccessful";
}
return mapping.findForward(returnURLKeyWord);
}}
4、struts-config.xml的部分代码
<action-mappings>
<action path="/UpdateStudentAction"
type="cn.giant.tbl.student.action.UpdateStudentAction"
name="studentForm">
<forward name="updateStudentFialure" path="/update.jsp"></forward>
<forward name="updateStudentSuccessful" path="/show.jsp"></forward>
</action>
<action path="/UpdateForwardAction"
type="cn.giant.tbl.student.action.UpdateForwardAction"
name="studentForm">
<forward name="updateStudent" path="/update.jsp"></forward>
</action>
<action path="/DeleteStudentAction"
type="cn.giant.tbl.student.action.DeleteStudentAction"
name="studentForm">
<forward name="deleteSuccessful" path="/show.jsp"></forward>
<forward name="deleteFailure" path="/deletefail.jsp"></forward>
</action>
</action-mappings>
5、jsp页面部分代码 show.jsp
我的实现原理:show.jsp是一个展示数据记录的页面,在每一条数据记录后面都有两个按钮,一个是修改,一个是删除。
点击修改按钮时,页面转跳到update.jsp(该功能已经实现),然后在update.jsp页面中的tesx框中输入要修改的信息(没有id这个框,id是不能修改的,修改记录也是根据id的),然后点击“提交修改”保存。但是问题就在这里了,id获取不到。
点击删除按钮时,直接删除该条记录,删除成功就重新转跳到show.jsp(相当于刷新吧),删除失败就转跳到deletefail.jsp。现在也是和修改一样的问题,id获取不到,所以每次我点击删除就直接跳到deletefail.jsp了。
<body>
<%
String sql = "select * from student";
List students = new StudentDao().findBySql(sql);
%>
<center>
<table border="3">
<tr>
<th colspan="8">
学生信息表
</th>
</tr>
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
性别
</td>
<td>
生日
</td>
<td>
分数
</td>
<td colspan="2" align="center">
操作
</td>
</tr>
<%
for (int i = 0; i < students.size(); i++) {
StudentForm student = new StudentForm();
student = (StudentForm) students.get(i);
%>
<tr>
<td>
<%=student.getId()%>
</td>
<td>
<%=student.getName()%>
</td>
<td>
<%=student.getSex()%>
</td>
<td>
<%=student.getBirth()%>
</td>
<td>
<%=student.getScore()%>
</td>
<td>
<form action="<%=basePath%>/UpdateForwardAction.do" method="post">
<input type="submit" value="修改">
</form>
</td>
<td>
<form action="<%=basePath%>/DeleteStudentAction.do" method="post">
<input type="submit" value="删除">
</form>
</td>
</tr>
<%
}
%>
</table>
<form action="<%=basePath%>/AddForwardAction.do" method="post">
<input type="submit" value="添加学生">
</form> </center> </body>
6、update.jsp部分代码
注意这个里面的id是隐藏的,是不允许修改的,本来是希望这个id是从show.jsp点击“修改”按钮时传过来的,但是没有成功,所以希望大家看看怎么办
<body>
<form action="<%=basePath%>/UpdateStudentAction" method="post">
<table border="3" align="center">
<tr>
<th colspan="2">
修改信息
</th>
</tr>
<tr>
<td>
<input type="hidden" name="id">
</td>
</tr>
<tr>
<td>
姓名:
</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>
性别:
</td>
<td>
<input type="text" name="sex">
</td>
</tr>
<tr>
<td>
生日:
</td>
<td>
<input type="text" name="birth">
</td>
</tr>
<tr>
<td>
分数:
</td>
<td>
<input type="text" name="score">
</td>
</tr>
</table>
<table align="center">
<tr>
<td>
<input type="submit" value="提交修改">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
<input type="submit" value="删除">
</form>
<form action="<%=basePath%>/UpdateForwardAction.do?id=<%=student.Id()%>" method="post">
<input type="submit" value="修改">
</form>
</td>
<td>
<form action="<%=basePath%>/DeleteStudentAction.do?id=<%=student.Id()%>" method="post">
<input type="submit" value="删除">
</form>
</td>
<form action="<%=basePath%>/DeleteStudentAction.do" method="post">
<input type="submit" value="删除">
</form>这form里面什么都没有 提交通过struts的actionform怎么就能取到值
老老实实url后面把ID带进去
<a href="<%=basePath%>/DeleteStudentAction.do?id=<%=student.getId()%>
">删除</a>
id隐藏在update.jsp里面
<input type="hidden" name="id" value="">
这样写不行的 action里面的<%= %>里面的内容不会解析的
url还会吧id=<%=student.Id()%>带上
直接A标签吧
各位大侠都指导的很对 小弟在此谢过了
结贴 散分