我有一种情况,我想将数组列表传递给 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";
}
我是一名优秀的程序员,十分优秀!