gpt4 book ai didi

java - Struts 1 ActionForm - 从纯 HTML 中检索集合

转载 作者:行者123 更新时间:2023-12-02 08:32:12 24 4
gpt4 key购买 nike

大家好,我(就像其他人一样)继承了一些 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 类(来 self 从网上学到的)是:

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 操作中会出现空的 SelectedRows?

感谢所有有耐心阅读这么长的问题的人...

并且(希望)感谢所有潜在的救世主:)

编辑1:

由于 @ahiru 的请求,我发布了我的 Action 类 - 目前只是一个精简版。几乎什么都不做,类:

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;
}
}

最佳答案

我不是 struts 大师,但 struts 可以处理从输入中构建列表吗?使用字符串数组会更好吗?您还尝试过在表单中​​添加方法吗?

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[]">

我也很好奇 spring 和 struts 是否能很好地协同工作(我对此没有做过任何研究)

关于java - Struts 1 ActionForm - 从纯 HTML 中检索集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3053216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com