简体   繁体   English

从单个表单插入多个实体

[英]Inserting multiple entities from single form

I want to insert multiple Person entities in a single HTML form. 我想在一个HTML表单中插入多个Person实体。 I want to use Map<Integer, Person> as the property in Action class. 我想使用Map<Integer, Person>作为Action类中的属性。 What should be the form input parameter names for this? 为此,表单输入参数名称应该是什么? The attributes of Person are id, name, age. Person的属性是id,名称,年龄。

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

Here is an example of inserting content into a 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>

Action... Struts2 can make use of Generics for Type conversion 行动... 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;
    }
}

Warning... The following does what you expect that is [1] is treated as a number 警告...以下是您期望的[1]被视为数字

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

['1'] is treated as a character but only if there is a single character ie '22' will be promoted to a string and thus the type conversion would convert '22' to 22 but '1' to 49 which is probably not what you want. ['1']被视为字符,但仅当存在单个字符时,即将'22'提升为字符串,因此类型转换会将'22'转换为22但将'1'转换为49可能不会你想要什么。

["1"] should work but in the struts tag the html which is written becomes : [“ 1”]应该可以工作,但是在struts标记中写入的html变为:

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

Which will not work. 这将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM