Struts2 +获取参数值()

发布于 2024-11-27 15:13:23 字数 835 浏览 0 评论 0原文

我能够使其在 Struts 中工作,但在 Struts2 中我无法理解问题是什么,我得到的结果为 null。

我的 Action 代码:

    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());
        }
    }

我的 JSP 代码:

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

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

</s:form>

我也使用了该方法,带有 setter 和 getter,同样的事情,我从 JSP 中得到 null 结果,这可以与 Struts2 一起使用吗? PS我正在尝试获取用户选择的复选框

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 user

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌梦深 2024-12-04 15:13:23

我建议也许在你的 struts 操作类中实现 ParameterAware。这将允许您以映射的形式遍历 servlet 请求参数:

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 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
        }
    }
}
秋风の叶未落 2024-12-04 15:13:23

我不确定你想做什么,也不知道你为什么要使用它。

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

Struts2 具有开箱即用的功能,可以将数据传输到您的操作类/bean,所有您都是操作类中受人尊敬的 setter 方法。

例如,您的 jsp 中有此复选框列表:

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

在您的操作类中,您需要类似这样的内容

private String yourColor;

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

如果选中了多个选项,则可以通过 String 对象存储它。

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.

‘画卷フ 2024-12-04 15:13:23

嗯,比我想象的要简单,Struts2确实是一个强大的框架,有些事情看起来比你想象的更简单。我能够通过将“东西”声明为来获取选定的框

List<String> stuff; 

,瞧,这里我们有选定框的列表。哦,是的,还有 getter 和 setter 方法。就这样。

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.

平定天下 2024-12-04 15:13:23

我使用此代码来获取复选框值字段中的值。这些复选框的 id field id = "Checkbox"String[] 值将仅包含勾选复选框的那些值。
此类扩展 ActionSupport 并实现 HttpServletRequestHttpServletResponse

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

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");}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文