Reputation: 81
I need to show registered user to users. Here am using Struts2 and I am very new it. But I know how to retrieve the elements without using Struts 2 there I will use collection set all the elements inside collection and I will put that in request scope and forwarded to the JSP page there I will use JSTL tag to retrieve all the elements but I don't know how to do it in Struts 2.
package com.uttara.reg;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class FetchRegisterdUserAction extends ActionSupport implements SessionAware{
/**
*
*/
private static final long serialVersionUID = 1L;
private RegBean bean;
private Map session;
private List<RegBean> registerdUsers;
public String execute() throws Exception {
System.out.println("inside execute of FA excut");
Model m = new Model();
registerdUsers= m.getRegisterdUsers();
if(registerdUsers!=null){
setRegisterdUsers(registerdUsers);
System.out.println("inside success");
return SUCCESS;
}
else{
System.out.println("inside failure");
return "failure";
}
}
@Override
public void validate() {
System.out.println("inside validate of LA");
}
public void setSession(Map session) {
// TODO Auto-generated method stub
System.out.println("inside setSession");
this.session = session;
}
public List<RegBean> getRegisterdUsers() {
return registerdUsers;
}
public void setRegisterdUsers(List<RegBean> registerdUsers) {
this.registerdUsers = registerdUsers;
}
}
This my JSP code where am going to retrieve individual elements but it is not working
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Registered Users</h1>
<s:iterator value="registerdUsers">
<s:property value="#{registerdUsers.uname}"/>
</s:iterator>
Upvotes: 0
Views: 3547
Reputation: 5337
Try this create getter and setter for registerdUsers;
public String execute() throws Exception {
System.out.println("inside execute of LA");
Model m = new Model();
**List<RegBean> rg = m.getRegisterdUsers();**
if(rg!=null){
setRegisterdUsers(rg); //set registerdUsers with rg
session.put("user", bean.getEmail());
return SUCCESS;
}
In jsp
Iterate it
<s:iterator value="registerdUsers">
<s:property value="registerdUsers.uname"/>
</s:iterator>
Upvotes: 0
Reputation: 1
You can access the collection objects using OGNL by the index. For example you have a List
collection
private List<String> myList;
public List<String> getMyList(){
return myList;
}
public void setMyList(List<String> myList){
this.myList = myList;
}
now in the JSP you can use an iterator s:iterator
tag to traverse the collection
<s:iterator value="myList"/>
<s:property/>
</s:iterator>
it will write to the JSP all string values of the collection
if you want to get the individual value, you can access it by index
<s:property value="%{myList[index]}"/>
where the index is just another variable in the valueStack
, you can get it from iterator, create in the context via s:set
tag, or access from the action
public int getIndex(){
return 0;
}
remember that a collection should be populated in your action before you use the iterator or access objects by index.
Upvotes: 0