如何在一个页面同时实现增,删,改。

现在遇到一个问题,利用struts framework 开发,,,
对于数据集 列表允许删除和修改,还要在同一一个页面实现添加,如何实现?
因为<html:form action>只能指定一个功能呀?

比较麻烦,关键是解决两个ActionForm在一个request中问题,
或者添加的表单不用Struts的html:form形式,直接使用html的form action

还有一个问题,在action中通过session来保存ArrayList(Element为formbean),可以实现方便的浏览列表,但是更改某条记录的属性值后,提交到服务端,修改的内容是否能自动保存到formbean,也就说我再通过sesseion就能得到最新的ArrayList

使用DispatchAction.具体参考struts文档吧
An abstract Action that dispatches to a public method that is named by the request parameter whose name is specified by the parameter property of the corresponding ActionMapping. This Action is useful for developers who prefer to combine many similar actions into a single Action class, in order to simplify their application design.
To configure the use of this action in your struts-config.xml file, create an entry like this:


<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>

which will use the value of the request parameter named "method" to pick the appropriate "execute" method, which must have the same signature (other than method name) of the standard Action.execute method. For example, you might have the following three methods in the same action:



public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

and call one of the methods with a URL like this: http://localhost:8080/myapp/saveSubscription.do?method=update

谢谢楼上;能不能回答一下我的第2个问题,就是删除,和修改是直接在列表上做的,如何将修改的内容返回到后台!!!!!!!!!

<a href="Edit.do?username=<bean:write name="employee"
property="username" />">Edit</a>
就是这样可以编辑1条数据,如何在列表上提交多条数据,,,各位,帮帮忙拉!!!!!!!!!!!

看不太懂你要做的是个什么东东~~
我以前做投票系统时做过一个东东,不知道是不是你要的,
有一个 GUI 是给管理员添加投票选项用的,主要由投票项的标题,以及投票项的选项两部分构成。

管理员可以在这个界面上动态加载与减少选项的个数,就有点像点击一个按钮,然后增加表格中的一行,或者再点击一个按钮,就删除表格中的一行。

不知道这个是不是你想要的东东

天下为公4s14YofS63.gif

上传图片看不见,上传了一个附件::::

就是在列表上可以修改列内容,点击“修改”按牛提交;
在最末行可以添加新内容,点“添加”按纽提交;
可以复选多行,点“删除”按纽提交;

我不能看到你的n案
不^我猜想你的r

  序   姓名
口 1  [banq]  [板蚶锶]  修改
口 2  [robbin]  [范P]  修改
口 3  [jini]  [99% jakarta]  修改
  [    ]  [      ] 新增

h除


碚f, 你要透^ javascript 砜刂
假O裼 DispatchAction , ⒄丈厦嫫渌怂
假O判ψ atype, 你要嵌入 input hidden name="action"

1,修改...
onclick="javascript:updateData(1)" , 1 是指序, jsp 要A先a生
另外嵌入一 input hidden name="uid"

updateDate(id) {
document.forms[0].uid.value=id;
document.forms[0].atype.value="update";
document.forms[0].submit();
}

2.新增
onclick="javascript:insertData()" ,

insertData() {
document.forms[0].atype.value="insert";
document.forms[0].submit();
}

3.h除
onclick="javacript:deleteData()" ,

deleteData() {
document.forms[0].atype.value="delete";
document.forms[0].submit();
}


h除的程式比^麻, 你自己想一想如何去接 checkbox array .

thanks for 楼上:情况复杂一点的是:修改直接在列表上做,
另外:在不用struts的情况下解决这个问题是没有问题。我想知道就是特别针对于update操作,因为列表是后台用ArrayList封装,前台用<logic:iterate >
显示,就是有没有办法(struts提供的解决方案)能把所有修改的列表都拿到而不用再去拼字符串,而且不是很方便...
<logic:iterate id="employee" name="employees">
<tr align="left">
..........
</tr>
</logic:iterate>

原本我用 netscape o法x你的D
我用 ie 就可以看到了 ^^~

不^你的 update }也是和 delete 一拥
基本上, ActionForm 都是可以接收 Array 的,
你可以在 FormBean 就封b成 List,
Action 就可以直接用了


for example,

JSP

<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<html:form action="/" >
<logic:iterate id="bean" name="exampleListForm" property="beanList" indexId="i"/>
<html:text name="exampleListForm" property="<%=\"employee[\" + i \"].name\"%>">
<html:text name="exampleListForm" property="<%=\"employee[\" + i \"].salary\"%>">
</logic:iterate>
</html:form>
</html:html>

FormBean:

import java.util.ArrayList;

import java.util.List;

import org.apache.struts.action.ActionForm;

public class ExampleListForm extends ActionForm {

private List beanList = new ArrayList();

public List getBeanList(){

return beanList;

}

public void setBeanList(List list){

beanList = list;

}

public Employee getEmployee(int index){

while(index >= beanList.size()){

beanList.add(new Employee());

}

return (Employee)beanList.get(index);

}

public void setEmployee(int index,Employee emp){

beanList.set(index,emp);

}

}

BeanClass

public class Employee {

private String name;

private String salary;


public String getName() {

return name;

}

public String getSalary() {

return salary;

}


public void setName(String name) {

this.name = name;

}

public void setSalary(String salary) {

this.salary = salary;

}

}


只要你能用 jsp 出淼,
一定可以改成 struts 架 ^^~

如何在struts里写脚本>>>,如果写<html:form >标签,脚本错误;如果用html的<form>标签,又提交不到后台>>>
to 大家,help?????????

<script language="Javascript">
function addtype()
{
document.forms[0].method="post";
document.forms[0].itype.value="add";
document.forms[0].submit;
}
</script>

<html:form action="/DepartmentAction.do">
<html:errors />
<input type="hidden" name="action" value="DepartmentAction">
<input type="hidden" name="itype">
<table width="650"
border="0" cellspacing="0" cellpadding="0">
<tr align="left">
<th><bean:message key="app.depid" /></th>
<th><bean:message key="app.depname" /></th>
<th>选择</th>
</tr>
<!-- iterate over the results of the query -->
<logic:iterate id="department" name="departments">
<tr align="left">
<td>
<bean:write name="department" property="depid" />
</td>
<td>
<input type="text" size="15" value="<bean:write name='department' property='depname'/>">
</td>
<td width=10%>
<input type="checkbox" size="8" value="<bean:write name='department' property='depid'/>" value1="<bean:write name='department' property='depname'/>">
</td>
</tr>
</logic:iterate>
<tr align="left">
<td>
<input type="text" name="adddepid"/>
</td>
<td>
<input type="text" size="15" name="adddepname">
</td>
<td width=10%>   
</td>
</tr>
<tr>
<td colspan="3">
<hr>
</td>
</tr>
</table>
<br>
<input type="button" value="添加" onclick="addtype()"/>  
<input type="button" value="修改"/>  
<input type="button" value="删除"/>  
<html:form>

如何在struts里写脚本>>>,如果写<html:form >标签,脚本错误;如果用html的<form>标签,又提交不到后台>>>
to 大家,help?????????



// 在此输入java代码
<script language=
"Javascript">
function addtype()
{
document.forms[0].method=
"post";
document.forms[0].itype.value=
"add";
document.forms[0].submit;
}
</script>

<html:form action=
"/DepartmentAction.do">
<html:errors />
<input type=
"hidden" name="action" value="DepartmentAction">
<input type=
"hidden" name="itype">
<table width=
"650"
border=
"0" cellspacing="0" cellpadding="0">
<tr align=
"left">
<th><bean:message key=
"app.depid" /></th>
<th><bean:message key=
"app.depname" /></th>
<th>选择</th>
</tr>
<!-- iterate over the results of the query -->
<logic:iterate id=
"department" name="departments">
<tr align=
"left">
<td>
<bean:write name=
"department" property="depid" />
</td>
<td>
<input type=
"text" size="15" value="<bean:write name='department' property='depname'/>">
</td>
<td width=10%>
<input type=
"checkbox" size="8" value="<bean:write name='department' property='depid'/>" value1="<bean:write name='department' property='depname'/>">
</td>
</tr>
</logic:iterate>
<tr align=
"left">
<td>
<input type=
"text" name="adddepid"/>
</td>
<td>
<input type=
"text" size="15" name="adddepname">
</td>
<td width=10%>&nbsp;&nbsp;&nbsp;
</td>
</tr>
<tr>
<td colspan=
"3">
<hr>
</td>
</tr>
</table>
<br>
<input type=
"button" value="添加" onclick="addtype()"/>&nbsp;&nbsp;
<input type=
"button" value="修改"/>&nbsp;&nbsp;
<input type=
"button" value="删除"/>&nbsp;&nbsp;
<html:form>