Java 从单个表单插入多个实体
我想在一个HTML表单中插入多个人实体。我想使用Java 从单个表单插入多个实体,java,struts2,Java,Struts2,我想在一个HTML表单中插入多个人实体。我想使用Map作为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?}' />
Map
作为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>
//人1
//人2
以下是将内容插入地图的示例
<%@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;
}
}
包com.quaternion.struts2basic.action.test;
导入com.opensymphony.xwork2.ActionSupport;
导入java.util.HashMap;
导入java.util.Map;
公共类测试扩展了ActionSupport{
//公众希望缩短例子
publicmap myMap=newhashmap();
公共字符串exectute(){
回归成功;
}
}
警告。。。以下是您所期望的,即[1]被视为一个数字
<s:textfield size="40" name="myMap[1]"></s:textfield><br/>
['1']被视为一个字符,但仅当存在单个字符时,ie'22'才会提升为字符串,因此类型转换会将'22'转换为22,而将'1'转换为49,这可能不是您想要的
[“1”]应该可以工作,但在struts标记中,编写的html变成:
<input type="text" name="myMap["1"]" size="40" value="" id="test_myMap_"444"_"/><br/>
这是行不通的