大家好我(就像其他人一样)继承了一些struts 1代码。我需要在这个项目中添加更多页面。
我无法弄清楚如何将几个不同但性质相似的输入元素映射到我的ActionForm。
让我详细说明一下。我在用户输入越来越多的项目时动态创建一个新的<Input>
元素(我使用YUI自动完成表单元素,对于每个输入的输入,我将其作为输入元素添加到我的表单并绘制一个新的YUI自动完成 - 复杂听起来,我知道)
所以...我的表格看起来有点像(...经过一些美化和一些这样的......):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>My Cool App - Test Case Builder</title>
<link rel="stylesheet" type="text/css" href="../script/yui/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="../skins/myCoolApp/button/button.css" />
<link rel="stylesheet" type="text/css" href="../script/yui/autocomplete/assets/skins/sam/autocomplete.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../skins/myCoolApp/testcase.css" />
<!-- YUI JAVA SCRIPTS -->
<script type="text/javascript" src="../script/yui/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="../script/yui/element/element-min.js"></script>
<script type="text/javascript" src="../script/yui/button/button-min.js"></script>
<script type="text/javascript" src="../script/yui/datasource/datasource-min.js"></script>
<script type="text/javascript" src="../script/yui/autocomplete/autocomplete-min.js"></script>
<!-- APP JAVA SCRIPTS -->
<script type="text/javascript" src="../script/myCoolApp/myCoolApp.js" ></script>
<script type="text/javascript" src="../script/myCoolApp/stack.js" ></script>
<script type="text/javascript" src="../script/myCoolApp/testcase/testcase.js"></script>
<script type="text/javascript" src="../script/myCoolApp/testcase/default-data.js" ></script>
<script type="text/javascript" src="../script/myCoolApp/testcase/data-structs.js" ></script>
<script type="text/javascript" src="../script/myCoolApp/testcase/ui-elements.js" ></script>
</head>
<body class="cf010">
<div id="wrap">
<div id="header">
<div id="main-header">
COOL APP
</div>
</div>
<div id="main-body">
<div id="content">
<div class="col main">
<div id="main">
<form method="post" id="testcaseForm" class="typea" action="">
<fieldset>
<legend>Test Case Builder</legend>
<div id="tk1" class="tabcontrol">
<ul class="tabs">
<li class="first active">
<a href="#">
<span>General</span>
</a>
</li>
<li class="last">
<a href="#">
<span>Parameters</span>
</a>
</li>
</ul>
<div id="tab0" class="tc-panel">
<dl class="cls9">
<dt>
<label for="scenario">Choose Scenario:</label>
</dt>
<dd>
<input type="text" id="scenario" name="scenario" class="text" />
<span id="scenarioToggle"></span>
<div class="auto-complete" id="scenarioContainer"></div>
</dd>
<dt>
<label for="ruleID">Choose Rule ID:</label>
</dt>
<dd>
<input type="text" id="ruleID" name="ruleID" class="text" />
<span id="ruleIDToggle"></span>
<div class="auto-complete" id="ruleIDContainer"></div>
</dd>
<dt>
<label for="Test Case Name" accesskey="t"><span class="accesskey">T</span>est Case Name:</label>
</dt>
<dd>
<input type="text" id="testCaseName" name="testCaseName" class="text" />
</dd>
</dl>
</div>
<div id="tab1" class="tc-panel hidden">
<div class="toolbar" id="action-bar">
<ul>
<li class="first">
<a title="select all" href="#" id="btmSelectAll" class="button">
<span>select all</span>
</a>
</li>
<li>
<a title="remove row" href="#" id="btmRemove" class="button">
<span>remove row</span>
</a>
</li>
<li>
<a title="undo last" href="#" id="btmRollBack" class="button disabled">
<span>undo last</span>
</a>
</li>
<li class="last">
<a title="accept row" href="#" id="btmAccept" class="button disabled">
<span>accept row</span>
</a>
</li>
</ul>
</div>
<div id="param.list" class="gridclip">
<table id='param.list.tbl' class='grid modela' >
<caption>Test Case Summary</caption>
<col/><col/><col/>
<thead>
<tr>
<th class='hl center first'>
<input class='grid-select-all' type='checkbox' />
<th>
<th scope='col'>Row</th>
<th scope='col'>Parameter</th>
<th scope='col' class='last'>Value</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope='row'>Total</th>
<td colspan='3'>2 parameters as Test Case input</td>
</tr>
</tfoot>
<tbody id='param.list.tbl.body'>
<tr class='odd'>
<td class='rowcheck center first'>
<input value='param1###value1' id='cb1' name='SelectedRows' class='grid-select-row' type='checkbox'/>
</td>
<td class='id'>1</td>
<td>param1</td>
<td class='last'>value1</td>
</tr>
<tr class='even'>
<td class='rowcheck center first'>
<input value='param2###value2' id='cb2' name='SelectedRows' class='grid-select-row' type='checkbox'/>
</td>
<td class='id'>2</td>
<td>param2</td>
<td class='last'>value2</td>
</tr>
<tr class='odd'>
<td class='rowcheck center first' />
<td class='id'><em>new</em></td>
<td>
<dl class='clsTable'>
<dt>
<input type='text' id='param' name='param' class='text paramInput' />
</dt>
<dd>
<span id='paramToggle' />
</dd>
<div class='auto-complete' id='paramContainer' />
</dl>
</td>
<td class='last'>
<dl class='clsTable'>
<dt>
<input type='text' id='value' name='value' class='text valueInput' />
</dt>
</dl>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div> <!-- tabcontrol -->
</fieldset>
<div class="submit-box">
<input type="submit" name="formRun" id="formRun" class="form-save"
value="Execute" accesskey="x" title="Run: Press Alt + [Shift] + x" />
<input type="submit" name="formSave" id="formSave"
value="Save" accesskey="s" title="Save: Press Alt + [Shift] + s" />
<input type="submit" name="formLoad" id="formLoad"
value="Load" accesskey="l" title="Load: Press Alt + [Shift] + l" />
<input type="submit" name="formCancel" id="formCancel" class="form-cancel"
value="Cancel" accesskey="c" title="Cancel: Press Alt + [Shift] + c" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
正如您所看到的,以下内容几乎是重复的:
<tr class='odd'>
<td class='rowcheck center first'>
<input value='param1###value1' id='cb1' name='SelectedRows' class='grid-select-row' type='checkbox'/>
</td>
<td class='id'>1</td>
<td>param1</td>
<td class='last'>value1</td>
</tr>
<tr class='even'>
<td class='rowcheck center first'>
<input value='param2###value2' id='cb2' name='SelectedRows' class='grid-select-row' type='checkbox'/>
</td>
<td class='id'>2</td>
<td>param2</td>
<td class='last'>value2</td>
</tr>
我的stuts-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="TestCaseForm" type="com.blahblah.mycoolapp.forms.TestCaseForm" />
</form-beans>
<action-mappings>
<action path="/pages/SaveTestCase" name="TestCaseForm"
type="org.springframework.web.struts.DelegatingActionProxy" scope="request">
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>
我也使用spring 2.56(相关部分):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/pages/SaveTestCase" class="com.blahblah.mycoolapp.actions.TestCaseBuilderSaveAction" />
</beans>
我的Java ActionForm类(从我从网上学到的)是:
package com.blahblah.mycoolapp.forms;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.action.ActionForm;
public class TestCaseForm extends ActionForm {
private static final long serialVersionUID = 2352146257739099766L;
private String scenario;
private String ruleID;
private String testCaseName;
private List<String> SelectedRows = new ArrayList<String>() ;
public String getScenario() {
return scenario;
}
public void setScenario(String scenario) {
this.scenario = scenario;
}
public String getRuleID() {
return ruleID;
}
public void setRuleID(String ruleID) {
this.ruleID = ruleID;
}
public String getTestCaseName() {
return testCaseName;
}
public void setTestCaseName(String testCaseName) {
this.testCaseName = testCaseName;
}
public List<String> getSelectedRows() {
return SelectedRows;
}
public void setSelectedRows(int index, String value) {
this.SelectedRows.add(value);
}
}
问题是为什么我在TestCaseBuilderSave Action中获得一个空的SelectedRows?
感谢所有有耐心的人阅读这么长的问题......
和(希望)感谢所有潜在的救星:)
编辑1:
由于@ ahiru的请求,我发布了我的动作类 - 暂时只是被剥离了。几乎什么都不做,上课:
public class TestCaseBuilderSaveAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=superfish.xml");
try
{
ServletOutputStream out = response.getOutputStream();
String contentStr = "<Yaneeve>Has been here...</Yaneeve>";
byte[] bytes = contentStr.getBytes();
out.write(bytes, 0, bytes.length);
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:1)
我不是struts master或者其他任何东西,但struts可以处理从输入中构建List吗?使用字符串数组会更好吗?您是否尝试在表单中添加方法?
public void setSelectedRows(List<String> list) { SelectedRows = list;}
我认为有一种方法可以在struts配置中定义索引输入。
- 更新
您是否可以在尝试获取这些值的位置发布您的操作类?
我知道你说过上面的方法不起作用,但是在类似的情况下这对我有用(我想)。对不起,我找不到确切的代码,但这是我记得的:
-ActionForm
private String[] strings;
public void setStrings(String[] strings) { this.strings = strings;}
public String[] getStrings() { return strings; }
JSP / HTML /不管
<input type="text" value="x" name="strings[]">
<input type="text" value="y" name="strings[]">
<input type="text" value="z" name="strings[]">
我很好奇弹簧和支柱是否很好地合作(我没有对此进行过研究)
答案 1 :(得分:1)
似乎问题是复选框没有被“检查”。
只有'已选中'复选框才能将其值传输到服务器...
我想强调的一点是,我将代码转换为@ahiru建议的代码:
private String[] strings;
public void setStrings(String[] strings) { this.strings = strings;}
public String[] getStrings() { return strings; }