我用webwork2和hibernate3做个小项目,
第一次添加一个部门的时候后台报错java.sql.BatchUpdateException: Duplicate entry '50' for key 1:但页面还显示正常。
如果第二次添加是后台也报错:java.sql.BatchUpdateException: Duplicate entry '60' for key 1,页面也会出现如下错误javax.servlet.ServletException: Could not execute action
com.opensymphony.webwork.dispatcher.DispatcherUtils.serviceAction(DispatcherUtils.java:196)
com.opensymphony.webwork.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:182)我想这是webwork关于重复提交的问题,
请问前辈们这是怎么回事,应该怎么做?
部分代码如下:
department.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="ww" uri="/webwork"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>department.jsp</title>
</head>
<script language="javascript">
function goSubmit(){
if(deptform.update.value=="true"){
deptform.pageAction.value="update";
}else{
deptform.pageAction.value="add";
}
deptform.submit();
}
</script>
<body>
<form name="deptform" action="department.action" method="post">
<ww:token /> 
<input type="hidden" name="pageAction" value="" /> 
<input type="hidden" name="update" value="<ww:property value="update" />" />
    <center>
    <font color="green" size=5>Config a Department</font> 
    <br>
<br>
<table width="40%" align="center">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td align="right"><a href="index.html">index</a></td>
</tr>
</table>
<br>
<table border=1 width="40%">
<tr>
<td>dept ID</td>
<td><input type="text" name="department.id" size=20 value="<ww:property value="department.id" />"></td>
</tr>
<tr>
<td>dept Name</td>
<td><input type="text" name="department.dept_name" size=20 value="<ww:property value="department.dept_name" />"></td>
</tr>
<tr>
<td>dept Head ID</td>
<td><input type="text" name="department.users.id" size=20 value="<ww:property value="department.users.id" />"></td>
</tr>
<tr>
<td align="center" colspan=2><input type="submit" value="submit" onClick="goSubmit()">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="reset" value="reset"></td>
</tr>
</table>
</center>
</form>
</body>
</html>-----------------------------------------------------------------------------------------------------
DepartmentActioin.java
package com.objectiva.xa.action;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import com.objectiva.xa.bean.Department;
import com.objectiva.xa.dao.DepartmentDAO;
import com.opensymphony.xwork.ActionSupport;public class DepartmentAction extends ActionSupport { private static final long serialVersionUID = 9078293677447891869L; private String pageAction; private Department department = new Department(); private DepartmentDAO deptdao = new DepartmentDAO(); private List departmentList; private int deptId; private String update = "false"; public String execute() {
if ("add".equalsIgnoreCase(pageAction)) {
deptdao.beginTransaction();
deptdao.save(department);
deptdao.endTransaction(true);
} else if ("show".equalsIgnoreCase(pageAction)) {
department = deptdao.findById(new Integer(deptId));
update = "true";
return INPUT;
} else if ("update".equalsIgnoreCase(pageAction)) {
deptdao.beginTransaction();
deptdao.merge(department);
deptdao.endTransaction(true);
} else if ("delete".equalsIgnoreCase(pageAction)) {
deptdao.beginTransaction();
deptdao.delete(deptdao.findById(new Integer(deptId)));
deptdao.endTransaction(true);
}
view();
return SUCCESS;
} public void view() {
departmentList = new ArrayList();
Iterator it = deptdao.getAllDepartments();
while (it.hasNext()) {
Department dept = (Department) it.next();
departmentList.add(dept);
}
} public Department getDepartment() {
return department;
} public void setDepartment(Department department) {
this.department = department;
} public void setPageAction(String pageAction) {
this.pageAction = pageAction;
} public List getDepartmentList() {
return departmentList;
} public void setDeptId(int deptId) {
this.deptId = deptId;
} public String getUpdate() {
return update;
}}
-------------------------------------------------------------------------
xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<include file="webwork-default.xml"></include>
<package name="default" extends="webwork-default">
<default-interceptor-ref name="defaultStack" />
<global-results>
<result name="index" type="redirect">/index.html</result>
<result name="error" type="redirect">/error.jsp</result>
</global-results>
<action name="department" class="com.objectiva.xa.action.DepartmentAction">
<result name="success">/departmentList.jsp</result>
<result name="input">/department.jsp</result>
</action>
<action name="user" class="com.objectiva.xa.action.UserAction">
<result name="success">/userList.jsp</result>
<result name="input">/user.jsp</result>
</action>
</package>
</xwork>