Batakj
Batakj

Reputation: 12743

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

Source Code of 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


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:

The given statements

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

give outputs

com.test.Questions@32bf232e1

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

Upvotes: 2

Views: 15457

Answers (2)

Fernando
Fernando

Reputation: 1

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

<s:property value="list" />

And then adding a toString method in the Questions class.

Upvotes: -2

Thomas
Thomas

Reputation: 88707

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

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

Upvotes: 9

Related Questions