开发者

Struts2 + getParameterValues()

开发者 https://www.devze.com 2023-03-25 00:51 出处:网络
I was able to make this to work in Struts, but in Struts2 I can not understand what the problem is, I am getting the null as result.

I was able to make this to work in Struts, but in Struts2 I can not understand what the problem is, I am getting the null as result.

My Action code:

    String[] stuff = request.getParameterValues("stuff");

    if (stuff != null) {

        for (int i = 0; i < stuff.length; i++) {

            Integer id = new Integer(stuff[i]);

            System.out.println("stuff id: " + id);

            Stuff stuffObj = stuffService.find(id);

            System.out.println("stuff name: " + stuffObj.getStuffName());
        }
    }

My JSP code:

<s:form action="add-menus" method="POST" enctype="multipart/form-data" theme="simple">

Stuff <s:checkboxlist name="stuff" list="stuffList.{stuffName}"/>

</s:form>

Also I used the method, with setter and getter, the same thing, I am getting the null result from JSP, is this working with Struts2? P.S. I am trying to get the checkboxes that was selected by the use开发者_开发技巧r


I would suggest perhaps implementing ParameterAware in your struts action class. This will allow you to traverse the servlet request parameters as a map:

public class ActionParam extends ActionSupport implements ParameterAware { 
    private Map<String, String[]> paraMap;

    @Override
    public void setParameters(Map<String, String[]> parameters) {
        this.paraMap= parameters;
    }
    ...

    for (Iterator<String> it = params.keySet().iterator(); it.hasNext();){
        String key = it.next();
        if (key.startsWith("stuff") && (map.get(key)!=null && !map.get(key).isEmpty())) {
             String[] stuff = map.get(key);
             // do magic
        }
    }
}


I am not sure what you are trying to do and morover why you are using this.

String[] stuff = request.getParameterValues("stuff");

Struts2 has out of the box functionality to transfer data to your action class/bean, all you are the respected setter method in your action class.

For e.g you have this checkbox list in your jsp:

<s:checkboxlist label="What's your favor color" list="colors" 
name="yourColor" value="defaultColor" />

in your action class all you need something like this

private String yourColor;

public void setYourColor(String yourColor) {
    this.yourColor = yourColor;
}

If multiple options are checked, you can store it via a String object.


Well, was easier than I thought, Struts2 is really a powerful framework, some things seem more simpler than you think. I was able to get selected boxes by declaring the "stuff" as

List<String> stuff; 

and voila, here we have the list of selected boxes. Oh yes, and the getter and setter methods. That's all.


I used this code to get the values which were there on the checkboxes' value field. These checkboxes had the id field id = "Checkbox". The String[] values will only contain those values for which the checkbox is ticked. This class extends ActionSupport and implements HttpServletRequest,HttpServletResponse

public String execute(){
    HttpServletRequest request = ServletActionContext.getRequest();
    this.setServletRequest(request);
    return "success";
}
public void setServletRequest(HttpServletRequest request) {
    String[] values= request.getParameterValues("Checkbox");}
0

精彩评论

暂无评论...
验证码 换一张
取 消