gpt4 book ai didi

java - 从单个表单插入多个实体

转载 作者:行者123 更新时间:2023-12-01 15:32:25 25 4
gpt4 key购买 nike

我想在单个 HTML 表单中插入多个 Person 实体。我想使用Map<Integer, Person>作为 Action 类中的属性。表单输入参数名称应该是什么? Person的属性有id、name、age。

<form action="createPeople">
//person1
<input type='text' name='{What is the name here?}' />
<input type='text' name='{What is the name here?}' />
<input type='text' name='{What is the name here?}' />

//person2
<input type='text' name='{What is the name here?}' />
<input type='text' name='{What is the name here?}' />
<input type='text' name='{What is the name here?}' />
</form>

最佳答案

以下是将内容插入 map 的示例。

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<h1>Input Test</h1>
<s:form action="test">
<s:textfield size="40" name="myMap[1]"></s:textfield><br/>
<s:textfield size="40" name="myMap[2]"></s:textfield><br/>
<s:textfield size="40" name="myMap[33]"></s:textfield><br/>
<s:textfield size="40" name="myMap[444]"></s:textfield><br/>
<s:textfield size="40" name="myMap[999]"></s:textfield><br/>
<s:submit/>
</s:form>
</body>
</html>

操作...Struts2 可以利用泛型进行类型转换

package com.quaternion.struts2basic.action.test;

import com.opensymphony.xwork2.ActionSupport;
import java.util.HashMap;
import java.util.Map;

public class Test extends ActionSupport{
//public to make example shorter
public Map<Integer, String> myMap = new HashMap<Integer, String>();

public String exectute(){
return SUCCESS;
}
}

警告...以下内容符合您的预期,即 [1] 被视为数字

<s:textfield size="40" name="myMap[1]"></s:textfield><br/>

['1'] 被视为一个字符,但仅当存在单个字符时,即 '22' 将被提升为字符串,因此类型转换会将 '22' 转换为 22,但将 '1' 转换为 49,其中可能不是您想要的。

["1"] 应该可以工作,但是在 struts 标签中,编写的 html 变成:

<input type="text" name="myMap[&quot;1&quot;]" size="40" value="" id="test_myMap_&quot;444&quot;_"/><br/>

这是行不通的。

关于java - 从单个表单插入多个实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9418371/

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