java 使用 struts 2 标签检索 ArrayList 的元素而不使用 s:iterate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5180213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:51:28  来源:igfitidea点击:

Retrieve the elements of ArrayList using struts 2 tag without using s:iterate

javajsptagsstruts2

提问by Shashi

Source Code of LoginAction.java

LoginAction.java 源代码


package com.test;

import java.util.ArrayList;
import java.util.List;

public class LoginAction {

    private List list;

    public void setList(List list) {
        this.list = list;
    }

    public List getList() {
        return list;
    }

    public String execute() {

        list = new ArrayList();

        list.add(new Questions("Pet Name", "Junk"));

        list.add(new Questions("Nick Name", "Bunk"));

        list.add(new Questions("Real Name", "Hunk"));

        return "SUCCESS";
    }

}

Source Code of Questions.java

Questions.java源代码


package com.test;
public class Questions {

    private String question;
    private String answer;

    public Questions(String question, String answer) {
        // TODO Auto-generated constructor stub

        this.question = question;
        this.answer = answer;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getQuestion() {
        return question;
    }
}

In JSP:

在 JSP 中:

The given statements

给定的陈述

 <s:property="list[0]"/>

give outputs

给出输出

com.test.Questions@32bf232e1

com.test.Questions@32bf232e1

How can i fetch the value Question object using struts2 tag without using iterator?

如何在不使用迭代器的情况下使用 struts2 标签获取值 Question 对象?

回答by Thomas

Try <s:property="list[0].question"/>.

试试<s:property="list[0].question"/>

Or <s:set name="question" value="list[0]"/>and then <s:property="#question.question"/>.

或者<s:set name="question" value="list[0]"/>然后<s:property="#question.question"/>

回答by Fernando

Another way to get the value of a Questionobject is to add this tag:

另一种获取Question对象值的方法是添加这个标签:

<s:property value="list" />

And then adding a toStringmethod in the Questionsclass.

然后toStringQuestions类中添加一个方法。