|
在夜色狼族(wolfuni)老师ssh2项目的基础上, 我作了修改添加和删出 student, 添加和删出 都工作, 唯有修改有问题。
我用了两种方法,
一种用的ActionForm, ok
另一种用的DynaActionForm, 可以修改student, 但是在同时会删出course_student_table的数据
这是hibernate sql 语句
Hibernate: update student set stu_name=?, stu_pwd=? where stu_id=?
Hibernate: delete from course_student_table where stu_id=?
1. struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="com.affecta.db.Student" />
<form-bean name="courseIDForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="coursesID" type="java.lang.String[]" />
</form-bean>
<form-bean name="studentProfileForm" type="com.affecta.db.Student" />
<form-bean name="stuProDynaForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="stu" type="com.affecta.db.Student" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards >
<forward
name="home"
path="/home.do"
redirect="true" />
</global-forwards>
<action-mappings >
<action
attribute="loginForm"
input="/form/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward
name="relogin"
path="/form/login.jsp"
redirect="true" />
<forward
name="showMyCourse"
path="/showMyCourse.do"
redirect="true" />
<forward
name="showAllCourses"
path="/showAllCourses.do"
redirect="true" />
<forward
name="home"
path="/home.jsp"
redirect="true" />
</action>
<action path="/showAllCourses" type="org.springframework.web.struts.DelegatingActionProxy">
<forward
name="showall"
path="/form/showCourses.jsp"
redirect="true" />
</action>
<action
attribute="courseIDForm"
input="/form/showCourses.jsp"
name="courseIDForm"
path="/selectCourse"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward
name="showMyCourse"
path="/showMyCourse.do"
redirect="true" />
</action>
<action path="/showMyCourse" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="showMyCouList" path="/form/showMyCourse.jsp" />
</action>
<action
attribute="studentProfileForm"
input="/form/studentProfile.jsp"
name="studentProfileForm"
parameter="do"
path="/studentProfile"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward
name="editStudentProfile"
path="/form/editStudentProfile.jsp"
redirect="true" />
<forward
name="login"
path="/form/login.jsp"
redirect="true" />
<forward
name="viewStudentProfile"
path="/viewStudentProfile.jsp"
redirect="true" />
</action>
<action forward="/home.jsp" path="/home" />
<action
attribute="stuProDynaForm"
input="/form/stuForm.jsp"
name="stuProDynaForm"
parameter="do"
path="/stuProDyna"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="viewStuPro" path="/viewStuPro.jsp" />
<forward name="editStuPro" path="/form/stuForm.jsp" redirect="false"/>
<forward name="login" path="/form/login.jsp" redirect="true" />
</action>
</action-mappings>
<message-resources parameter="com.affecta.ui.ApplicationResources" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
***********************************************************************
2. action 文件
public ActionForward saveStu(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm stuForm = (DynaActionForm) form;// TODO Auto-generated method stub
String stuID= request.getParameter("stu.stuId");
String doString = request.getParameter("do");
System.out.println((Student)stuForm.get("stu"));
if(stuID!=null){
studao.update((Student)stuForm.get("stu"));
return viewStu(mapping,form,request,response);
}else{
studao.save((Student)stuForm.get("stu"));
System.out.println("login");
return mapping.findForward("login");
}
//return null;
}
请大家看一看, 到底是什么问题, 如何需要代码的, 请告诉我,我找空间上传。
多谢!
如果我在
public ActionForward saveStu(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm stuForm = (DynaActionForm) form;// TODO Auto-generated method stub
String stuID= request.getParameter("stu.stuId");
String doString = request.getParameter("do");
if(stuID!=null){
Student student = studao.studao.findById(stuID);
student.setStuName(((Student)stuForm.get("stu")).getStuName());
student.setStuPwd(((Student)stuForm.get("stu")).getStuPwd());
studao.update(student);
return viewStu(mapping,form,request,response);
}else{
studao.save((Student)stuForm.get("stu"));
System.out.println("login");
return mapping.findForward("login");
}
//return null;
}
改了以后, 就没有
Hibernate: delete from course_student_table where stu_id=?
执行。但是这样太麻烦了, 如果student 有好多属性, 要增加好多代码, 不知道大家有什么好方法没有?
[ 本帖最后由 owenhuan 于 2007-1-31 11:42 编辑 ] |
|