gpt4 book ai didi

java - 如何将数组列表传递给 Controller

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:44 25 4
gpt4 key购买 nike

我有一种情况,我想将数组列表传递给 Controller ​​,我正在附加我的 Controller 和下面的java代码,请给我一个想法或建议来做到这一点模型类...

public class Rules {
String id;
String details;
String parameter;
String value;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Rules(){}
public Rules(String id,String details,String parameter, String value){
this.id=id;
this.details=details;
this.value=value;
this.parameter=parameter;

}

这里是创建列表的java类..

import java.util.ArrayList;
import com.demo.Rules;

public class CompArray {
public ArrayList<Rules> addRule(){
ArrayList<Rules> a1= new ArrayList<Rules>();
Rules rul = new Rules();
rul.setId("1001");
rul.setDetails("khagfkj");
rul.setParameter("lsrkjglkrs");
rul.setValue("lskdjfk");

a1.add(rul);

return a1;

}

Controller

@RequestMapping(value = "/compplan", method = RequestMethod.GET)
public String listRules1(ModelMap model) {
CompArray obj1 = new CompArray();
model.addAttribute("listRule1", obj1.addRule());
return "hello";
}

我需要帮助,请帮助我,谢谢....

最佳答案

使用 json 类似的东西:

 model.addAttribute("listRule1", new JSONArray().addAll(obj1.addRule()));   

编辑: 您可以通过传递整个对象来完成此操作,在您的情况下,您将传递 CompArray 对象,并在接收它时调用您的函数,该函数将返回 arraylist

import java.util.ArrayList;
import com.demo.Rules;

public class CompArray {
public ArrayList<Rules> a1= new ArrayList<Rules>();

public CompArray (){
Rules rul = new Rules();
rul.setId("1001");
rul.setDetails("khagfkj");
rul.setParameter("lsrkjglkrs");
rul.setValue("lskdjfk");

a1.add(rul);


}
public ArrayList<Rules> getRules(){


return a1;
}
}

和 Controller

  @RequestMapping(value = "/compplan", method = RequestMethod.GET)
public String listRules1(ModelMap model) {
CompArray obj1 = new CompArray();
model.addAttribute("listRule1", obj1);
return "hello";
}

关于java - 如何将数组列表传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36446775/

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