从单个表单插入多个实体

时间:2012-02-23 17:49:06

标签: java struts2

我想在一个HTML表单中插入多个Person实体。我想在Action类中使用Map<Integer, Person>作为属性。这个表单的输入参数名称应该是什么? 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>

1 个答案:

答案 0 :(得分:1)

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

<%@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/>

哪个不起作用。

相关问题