使用DTO绑定多个动态创建的字段的相同属性名称 [英] Bind same attribute names of multiple dynamically created fields using DTO
问题描述
我有2个下拉菜单 A
和 B
。
-
A
是正常下拉列表 -
B
是多选下拉列表
A
is a normal dropdownB
is a multiselect dropdown
A
包含学校中的课程列表并且 B
包含学校中的部门
A
contains list of classes in a school and B
contains divisions in a school
还有一个用于动态创建这两个下拉菜单的按钮(可能有 n 下拉菜单)
There is also a button for dynamic creation of these two dropdowns(There may be n dropdowns)
我想同时将 n 下拉列表值保存到我的数据库中,但问题是我的全部问题动态创建的字段属性名称与原始的两个下拉列表相同,这样我就可以获得输入到后端的最后一个下拉列表的值。我可以使用js动态更改属性名称,但我无法在dto中动态创建变量。
I want to save n dropdowns values simultaneously to my database but the problem is all my dynamically created field attribute names are same as the original two dropdowns, this way I am getting the value of last dropdown entered to my backend. I can dynamically change the attribute names using js, but I can't dynamically create variables in my dto.
我希望我的n个下拉值绑定到相同的DTO变量中列表或类似的东西,有没有其他方法来实现这一目标?
i研究并上了 apache commons collections4
和 autopopulatingList
但是我没有找到任何正确的例子
I want my n dropdown values to bind into same DTO variables as a list or something like that, Is there any other methods to achieve this?
i researched and went up with apache commons collections4
and autopopulatingList
but i don't find any proper examples
我的DTO课程
public class TestDto {
private Long fkcl;
private String[] fkdiv;
public Long getFkcl() {
return fkcl;
}
public void setFkcl(Long fkcl) {
this.fkcl = fkcl;
}
public String[] getFkdiv() {
return fkdiv;
}
public void setFkdiv(String[] fkdiv) {
this.fkdiv = fkdiv;
}
ClassDes
public class ClassDes {
public List<TestDto> list = new ArrayList<TestDto>();
public List<TestDto> getList() {
return list;
}
public void setList(List<TestDto> list) {
this.list = list;
}
}
控制器
@RequestMapping(value = "/testing")
public ModelAndView ff(Model model) {
ClassDes testprof = new ClassDes();
List<ClassMaster> bslist = serv.findAllclass();
model.addAttribute("blah", bslist);
List<StudentMaster> std = stdServ.findAll();
model.addAttribute("std", std);
return new ModelAndView("test" , "testprof", testprof);
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(@ModelAttribute ClassDes testprof) {
System.out.println(BasicGson.toGson(testprof));
return new ModelAndView("redirect:/testing", "testprof", testprof);
}
但我的清单返回空 {list: []}
但在我的ajax方法中,
显示
but my list returns empty {"list":[]}
but in my ajax method,
it shows
{"listed[].fkcl":"3","listed[].fkdiv":["1","2","3","4","5"]}
推荐答案
你可以在js中更改你的submitform函数
u can change ur submitform function in js
$('#submitForm').submit(function(e) {
var frm = $('#submitForm');
e.preventDefault();
var data = {};
var dt=[];
var newdt = {};
var Form = this;
$.each(this, function(i, v) {
var input = $(v);
if(data.hasOwnProperty("fkcl")) //mapped all dropdown values to fkcl and fkdiv inorder to use `TestDto` variables
{
if(data.hasOwnProperty("fkdiv"))
{
dt.push(data);
data={};
}
}
data[input.attr("name")] = input.val();
delete data["undefined"];
});
newdt['list']=dt;
alert(JSON.stringify(newdt));
$.ajax({
async : false,
global : false,
contentType : 'application/json; charset=utf-8',
type : 'post',
url : frm.attr('action'),
data : JSON.stringify(newdt),
success : function(callback) {
window.location.reload();
},
error : function() {
$(this).html("Error!");
}
});
@Controller
@RequestMapping(value = "/save", method = RequestMethod.POST)
public void save(@RequestBody ClassDes testprof) {
for (TestDto t : testprof.getList()) {
serv.save(t);
}
return new "redirect:/testing";
}
这篇关于使用DTO绑定多个动态创建的字段的相同属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!