1. 程式人生 > >ssh關於含有外鍵的傳值中無法識別正確的action的原因和解決辦法

ssh關於含有外鍵的傳值中無法識別正確的action的原因和解決辦法

ces pub ets err ntc 執行函數 success 引入 dst

在含有外鍵的表中,要保存一個值到這個外鍵時:
邏輯思路:
需要先將jsp頁面的值傳到相應的action中,在這個action中需要引入這個外鍵的實體層和DAO層(DAO層只需set方法),在執行函數中對於外鍵的傳值,應該先根據外鍵DAO層的方法來將這個外鍵傳給外鍵的對象,然後將這個對象通過此表中的實體類中的方法保存到該表中。

jsp:

<div class="form-group has-warning">
     <label class="control-label" for="inputWarning">請輸入班級編號</label>
     <input name="studentclass" type="text" class
="form-control" id="inputWarning"> </div>

action:

//變量(與外鍵相關的)
private String studentclass = "";
private Classunit classunit;
private ClassunitDAO classunitDAO;

//函數
public String getStudentclass() {
    return studentclass;
}
public void setStudentclass(String studentclass) {
    this.studentclass = studentclass;
}
public Classunit getClassunit() { return classunit; } public void setClassunit(Classunit classunit) { this.classunit = classunit; } public void setClassunitDAO(ClassunitDAO classunitDAO) { this.classunitDAO = classunitDAO; } public String execute() throws Exception { System.out.println(
"執行add的action"); if(studentService.checkStudentnumber(getStudentnumber())) { return ERROR; } else { System.out.println(studentclass); studentex = new Student(); studentex.setStudentNumber(studentnumber); studentex.setStudentName(studentname); studentex.setStudentGender(studentgender); studentex.setStudentBirth(studentbirth); studentex.setStudentPassword(studentpassword); studentex.setStudentPhone(studentphone); studentex.setStudentAddress(studentaddress); classunit = classunitDAO.findclassunitnumber(studentclass); studentex.setClassunit(classunit); studentService.addStudent(studentex); } return SUCCESS; }

配置:
在spring的事務管理配置中需要將這個表需要註入的DAO和外鍵需要註入的DAO都配置上。

<!-- 新建學生 -->
    <bean id="StudentAddAction" class="com.zdr.action.StudentAddAction">
        <property name="studentService" ref="StudentServiceImpl"/>
        <property name="classunitDAO" ref="ClassunitDAO"/>
    </bean>

否則會因為事務邏輯問題引起的無法識別action的500錯誤。

ssh關於含有外鍵的傳值中無法識別正確的action的原因和解決辦法