使用struts,在action 中如何获得js传过来的数组?

2025-04-15 05:23:58
推荐回答(3个)
回答1:

Struts2接受请求参数:

package com.bird.action; 
public class HelloWorld {
       private Integer id;
       private String name;
      
       public Integer getId() {
              return id;
       }
 
       public void setId(Integer id) {
              this.id = id;
       }
 
       public String getName() {
              return name;
       }
 
       public void setName(String name) {
              this.name = name;
       }
 
       public String execute(){
              System.out.println("id="+id);
              System.out.println("name="+name);
              return "success";
       }
      
       public String addUI(){
      
              return "success";
       }
}

要提供set  和get方法使其变为属性,从而令struts能够调用,还有就是在struts.xml文件中要把这个Action配置好


js代码:
location.href="/helloworld.action?id=1&name=tom";
即可将值传递到Action的指定属性中

回答2:

传数组不可以,http协议传的都是文本,如果楼主一定要传数组的东西进去,把里面的值都拼起来以字符串放到url里面,然后location传过去了,再action中getParameter中拿了。

回答3:

在你的action里面定义一个String idlist;
用idlist = Request.getParameter(“idlist”);
idlist就是一个字符串数组了