You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by SofIAm <so...@yahoo.com> on 2009/05/07 17:54:49 UTC

Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Hi Everyone,

I'm new to Struts. Please help me figure out why the List myList is not
being displayed in JSP, although my String variable s is displayed. Your
help will be greatly appreciated! Thanks!

Here's the code:

struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

   <include file="struts-default.xml"/>

    <package name="default" extends="struts-default">

        <action name="getAllEmployees" method="getAllEmployees"
class="net.struts2demo.action.EmployeeAction">
           <result>employees.jsp</result>
        </action>
        
     </package>   
</struts>

Action class
package net.struts2demo.action;
import com.opensymphony.xwork2.ActionSupport;
import com.sample.PersonController;
import com.sample.Person;


import java.util.List;

import java.util.ArrayList;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class EmployeeAction extends ActionSupport {
	
	//private static Logger logger = Logger.getLogger(EmployeeAction.class);

	private Person person;
	private List<String> myList;
	private String s;
	 
	public List<String> getMyList() {
		return myList;
	}
	
	public String getS(){
		return s;
	}
	public String getAllEmployees() {
		//PersonController pc = new PersonController();
		//List people = pc.getAllPeople();
		
		
		//logger.log(Level.TRACE, "Action Class: And a trace message using log()
method.");
		
		System.out.print("Action Class");
		
		List<String> myList = new ArrayList<String>();
	    myList.add("Fruits");
	    myList.add("Apple");
	    myList.add("Mango");
	    myList.add("Orange");
	    myList.add("Pine Apple");
	
	    s="Hello Struts was in the right method!";
	    
		return SUCCESS;
	}
	public String getPerson() {
		PersonController pc = new PersonController();
		person = pc.getPerson(1);
		return "success";
	}
	public void setPerson(Person person) {
		this.person = person;
	}

	public void setMyList(List<String> myList) {
		this.myList = myList;
	}

}

JSP Page: employee.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
    <title>Iterator Tag Example!</title>
  </head>
  <body>
    <h1>Iterator Tag Example!</h1>
    
      <s:iterator value="myList">
        <s:property /><br>
      </s:iterator>
      
   	<table align="center" class="borderAll">
     	 
         <tr><td class="tdLabel"><s:text name="s"/></td>
         	        <td><s:textfield name="s" size="30"/></td>
         </tr>
    </table>

  </body>
</html> > 
-- 
View this message in context: http://www.nabble.com/Please-help%21-Struts-2-Eclipse---List-Object-is-not-displaying-in-JSP-tp23429087p23429087.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by SofIAm <so...@yahoo.com>.
You're correct. That was exactly the problem.  Thanks for responding! 



satyanarayana katta wrote:
> 
> Take a close look at your  getAllEmployees()method.  You have declared the
> list with same name myList, which is a local variable.  You need to either
> call setList and pass this or use the samelist.
>        public String getAllEmployees() {
>        //remove the List<String>
>                List<String> myList = new ArrayList<String>();
> 
>        }
> 
> 
> Cheers.
> 
> Satya
> 
> On Thu, May 7, 2009 at 8:54 AM, SofIAm <so...@yahoo.com> wrote:
> 
>>
>> Hi Everyone,
>>
>> I'm new to Struts. Please help me figure out why the List myList is not
>> being displayed in JSP, although my String variable s is displayed. Your
>> help will be greatly appreciated! Thanks!
>>
>> Here's the code:
>>
>> struts.xml
>>
>>  <?xml version="1.0" encoding="UTF-8" ?>
>>
>> <!DOCTYPE struts PUBLIC
>>        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>>        "http://struts.apache.org/dtds/struts-2.0.dtd">
>>
>> <struts>
>>
>>   <include file="struts-default.xml"/>
>>
>>    <package name="default" extends="struts-default">
>>
>>        <action name="getAllEmployees" method="getAllEmployees"
>> class="net.struts2demo.action.EmployeeAction">
>>           <result>employees.jsp</result>
>>        </action>
>>
>>     </package>
>> </struts>
>>
>> Action class
>> package net.struts2demo.action;
>> import com.opensymphony.xwork2.ActionSupport;
>> import com.sample.PersonController;
>> import com.sample.Person;
>>
>>
>> import java.util.List;
>>
>> import java.util.ArrayList;
>>
>> import org.apache.log4j.Level;
>> import org.apache.log4j.Logger;
>>
>> public class EmployeeAction extends ActionSupport {
>>
>>        //private static Logger logger =
>> Logger.getLogger(EmployeeAction.class);
>>
>>        private Person person;
>>        private List<String> myList;
>>        private String s;
>>
>>        public List<String> getMyList() {
>>                return myList;
>>        }
>>
>>        public String getS(){
>>                return s;
>>        }
>>        public String getAllEmployees() {
>>                //PersonController pc = new PersonController();
>>                //List people = pc.getAllPeople();
>>
>>
>>                //logger.log(Level.TRACE, "Action Class: And a trace
>> message
>> using log()
>> method.");
>>
>>                System.out.print("Action Class");
>>
>>                List<String> myList = new ArrayList<String>();
>>            myList.add("Fruits");
>>            myList.add("Apple");
>>            myList.add("Mango");
>>            myList.add("Orange");
>>            myList.add("Pine Apple");
>>
>>            s="Hello Struts was in the right method!";
>>
>>                return SUCCESS;
>>        }
>>        public String getPerson() {
>>                PersonController pc = new PersonController();
>>                person = pc.getPerson(1);
>>                return "success";
>>        }
>>        public void setPerson(Person person) {
>>                this.person = person;
>>        }
>>
>>        public void setMyList(List<String> myList) {
>>                this.myList = myList;
>>        }
>>
>> }
>>
>> JSP Page: employee.jsp
>> <%@ taglib prefix="s" uri="/struts-tags" %>
>>
>> <html>
>>  <head>
>>    <title>Iterator Tag Example!</title>
>>  </head>
>>  <body>
>>    <h1>Iterator Tag Example!</h1>
>>
>>      <s:iterator value="myList">
>>        <s:property /><br>
>>      </s:iterator>
>>
>>        <table align="center" class="borderAll">
>>
>>         <tr><td class="tdLabel"><s:text name="s"/></td>
>>                        <td><s:textfield name="s" size="30"/></td>
>>         </tr>
>>    </table>
>>
>>  </body>
>> </html> >
>> --
>> View this message in context:
>> http://www.nabble.com/Please-help%21-Struts-2-Eclipse---List-Object-is-not-displaying-in-JSP-tp23429087p23429087.html
>> Sent from the Struts - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/Please-help%21-Struts-2-Eclipse---List-Object-is-not-displaying-in-JSP-tp23429087p23449084.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by satyanarayana katta <sa...@gmail.com>.
Take a close look at your  getAllEmployees()method.  You have declared the
list with same name myList, which is a local variable.  You need to either
call setList and pass this or use the samelist.
       public String getAllEmployees() {
       //remove the List<String>
               List<String> myList = new ArrayList<String>();

       }


Cheers.

Satya

On Thu, May 7, 2009 at 8:54 AM, SofIAm <so...@yahoo.com> wrote:

>
> Hi Everyone,
>
> I'm new to Struts. Please help me figure out why the List myList is not
> being displayed in JSP, although my String variable s is displayed. Your
> help will be greatly appreciated! Thanks!
>
> Here's the code:
>
> struts.xml
>
>  <?xml version="1.0" encoding="UTF-8" ?>
>
> <!DOCTYPE struts PUBLIC
>        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>        "http://struts.apache.org/dtds/struts-2.0.dtd">
>
> <struts>
>
>   <include file="struts-default.xml"/>
>
>    <package name="default" extends="struts-default">
>
>        <action name="getAllEmployees" method="getAllEmployees"
> class="net.struts2demo.action.EmployeeAction">
>           <result>employees.jsp</result>
>        </action>
>
>     </package>
> </struts>
>
> Action class
> package net.struts2demo.action;
> import com.opensymphony.xwork2.ActionSupport;
> import com.sample.PersonController;
> import com.sample.Person;
>
>
> import java.util.List;
>
> import java.util.ArrayList;
>
> import org.apache.log4j.Level;
> import org.apache.log4j.Logger;
>
> public class EmployeeAction extends ActionSupport {
>
>        //private static Logger logger =
> Logger.getLogger(EmployeeAction.class);
>
>        private Person person;
>        private List<String> myList;
>        private String s;
>
>        public List<String> getMyList() {
>                return myList;
>        }
>
>        public String getS(){
>                return s;
>        }
>        public String getAllEmployees() {
>                //PersonController pc = new PersonController();
>                //List people = pc.getAllPeople();
>
>
>                //logger.log(Level.TRACE, "Action Class: And a trace message
> using log()
> method.");
>
>                System.out.print("Action Class");
>
>                List<String> myList = new ArrayList<String>();
>            myList.add("Fruits");
>            myList.add("Apple");
>            myList.add("Mango");
>            myList.add("Orange");
>            myList.add("Pine Apple");
>
>            s="Hello Struts was in the right method!";
>
>                return SUCCESS;
>        }
>        public String getPerson() {
>                PersonController pc = new PersonController();
>                person = pc.getPerson(1);
>                return "success";
>        }
>        public void setPerson(Person person) {
>                this.person = person;
>        }
>
>        public void setMyList(List<String> myList) {
>                this.myList = myList;
>        }
>
> }
>
> JSP Page: employee.jsp
> <%@ taglib prefix="s" uri="/struts-tags" %>
>
> <html>
>  <head>
>    <title>Iterator Tag Example!</title>
>  </head>
>  <body>
>    <h1>Iterator Tag Example!</h1>
>
>      <s:iterator value="myList">
>        <s:property /><br>
>      </s:iterator>
>
>        <table align="center" class="borderAll">
>
>         <tr><td class="tdLabel"><s:text name="s"/></td>
>                        <td><s:textfield name="s" size="30"/></td>
>         </tr>
>    </table>
>
>  </body>
> </html> >
> --
> View this message in context:
> http://www.nabble.com/Please-help%21-Struts-2-Eclipse---List-Object-is-not-displaying-in-JSP-tp23429087p23429087.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by Timothy Orme <to...@genome.med.harvard.edu>.
This is actually not correct, he defined the method as "getAllEmployees" in his struts.xml

That said, I'm not sure of the default behavior of the s:property tag. Have you tried something like s:property value="top"?

I'm not huge on the struts tag libs, but this would be the first place I'd look, at a quick glance your action setup looks fine.

Have you tried setting a breakpoint on the getter action to see that the list is actually populated?

-Tim

Lukasz Lenart wrote:
> 2009/5/7 SofIAm <so...@yahoo.com>:
>>        public String getAllEmployees() {
> 
> This method is never called, renamed it to execute() and should be ok
> 
> 
> Regards

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by SofIAm <so...@yahoo.com>.
That's great! I'll look into that. Thanks again.


Jim Kiley wrote:
> 
> The checkstyle, FindBugs, or PMD plugin does this for sure.
> 
> On Thu, May 7, 2009 at 8:50 PM, Dave Newton <ne...@yahoo.com> wrote:
> 
>> SofIAm wrote:
>>
>>> Thank YOU ALL! What a dumb JAVA 101 mistake! I was never populating the
>>> class
>>> member variable myList, but the local one in my method, unintentionally
>>> of
>>> course. DUH!!! It's working like a charm!  Thanks again!!!!!
>>>
>>
>> There is a warning available in Eclipse to notify you when a local
>> variable
>> shadows a property. (I think.)
>>
>> Dave
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> 
> -- 
> Jim Kiley
> Senior Technical Consultant | Summa
> [p] 412.258.3346
> http://www.summa-tech.com
> 
> 

-- 
View this message in context: http://www.nabble.com/Please-help%21-Struts-2-Eclipse---List-Object-is-not-displaying-in-JSP-tp23429087p23444897.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by Jim Kiley <jh...@summa-tech.com>.
The checkstyle, FindBugs, or PMD plugin does this for sure.

On Thu, May 7, 2009 at 8:50 PM, Dave Newton <ne...@yahoo.com> wrote:

> SofIAm wrote:
>
>> Thank YOU ALL! What a dumb JAVA 101 mistake! I was never populating the
>> class
>> member variable myList, but the local one in my method, unintentionally of
>> course. DUH!!! It's working like a charm!  Thanks again!!!!!
>>
>
> There is a warning available in Eclipse to notify you when a local variable
> shadows a property. (I think.)
>
> Dave
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Jim Kiley
Senior Technical Consultant | Summa
[p] 412.258.3346
http://www.summa-tech.com

Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by Dave Newton <ne...@yahoo.com>.
SofIAm wrote:
> Thank YOU ALL! What a dumb JAVA 101 mistake! I was never populating the class
> member variable myList, but the local one in my method, unintentionally of
> course. DUH!!! It's working like a charm!  Thanks again!!!!! 

There is a warning available in Eclipse to notify you when a local 
variable shadows a property. (I think.)

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by SofIAm <so...@yahoo.com>.
Thank YOU ALL! What a dumb JAVA 101 mistake! I was never populating the class
member variable myList, but the local one in my method, unintentionally of
course. DUH!!! It's working like a charm!  Thanks again!!!!! 



Timothy Orme wrote:
> 
> Ah good catch, this is whats causing it. Although as I said, you dont need
> to rename the method if you've setup your struts.xml as it is.
> 
> Jim Kiley wrote:
>> Even then it won't be OK.  The OP is declaring myList as a local variable
>> in
>> getAllEmployees(), so getMyList won't return it.  Change the line
>> "List<String>
>> myList = new ArrayList<String>();" in getAllEmployees() to just be
>> "myList =
>> new ArrayList<String>();" and do as Lukasz suggests and you should be OK.
>> jk
>> 
>> On Thu, May 7, 2009 at 11:57 AM, Lukasz Lenart
>> <lukasz.lenart@googlemail.com
>>> wrote:
>> 
>>> 2009/5/7 SofIAm <so...@yahoo.com>:
>>>>        public String getAllEmployees() {
>>> This method is never called, renamed it to execute() and should be ok
>>>
>>>
>>> Regards
>>> --
>>> Lukasz
>>> http://www.lenart.org.pl/
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>> 
>> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Please-help%21-Struts-2-Eclipse---List-Object-is-not-displaying-in-JSP-tp23429087p23430437.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by Timothy Orme <to...@genome.med.harvard.edu>.
Ah good catch, this is whats causing it. Although as I said, you dont need to rename the method if you've setup your struts.xml as it is.

Jim Kiley wrote:
> Even then it won't be OK.  The OP is declaring myList as a local variable in
> getAllEmployees(), so getMyList won't return it.  Change the line "List<String>
> myList = new ArrayList<String>();" in getAllEmployees() to just be "myList =
> new ArrayList<String>();" and do as Lukasz suggests and you should be OK.
> jk
> 
> On Thu, May 7, 2009 at 11:57 AM, Lukasz Lenart <lukasz.lenart@googlemail.com
>> wrote:
> 
>> 2009/5/7 SofIAm <so...@yahoo.com>:
>>>        public String getAllEmployees() {
>> This method is never called, renamed it to execute() and should be ok
>>
>>
>> Regards
>> --
>> Lukasz
>> http://www.lenart.org.pl/
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by Jim Kiley <jh...@summa-tech.com>.
Even then it won't be OK.  The OP is declaring myList as a local variable in
getAllEmployees(), so getMyList won't return it.  Change the line "List<String>
myList = new ArrayList<String>();" in getAllEmployees() to just be "myList =
new ArrayList<String>();" and do as Lukasz suggests and you should be OK.
jk

On Thu, May 7, 2009 at 11:57 AM, Lukasz Lenart <lukasz.lenart@googlemail.com
> wrote:

> 2009/5/7 SofIAm <so...@yahoo.com>:
> >        public String getAllEmployees() {
>
> This method is never called, renamed it to execute() and should be ok
>
>
> Regards
> --
> Lukasz
> http://www.lenart.org.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Jim Kiley
Senior Technical Consultant | Summa
[p] 412.258.3346
http://www.summa-tech.com

Re: Please help! Struts 2/Eclipse - List Object is not displaying in JSP

Posted by Lukasz Lenart <lu...@googlemail.com>.
2009/5/7 SofIAm <so...@yahoo.com>:
>        public String getAllEmployees() {

This method is never called, renamed it to execute() and should be ok


Regards
-- 
Lukasz
http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org