tags:

views:

367

answers:

1

Here is a small test application that does following things

  1. ask user to enter his name and submit - (index.jsp)
  2. as a result of index.jsp is the welcome.jsp page that asks user to select his/her blood group

index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>   
<%@ taglib prefix="s" uri="/struts-tags" %>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>   </head>   
   <body>   
    <form action="MyName">   
    <s:textfield name="UserName" label="Enter Your Name"/>   
    <s:submit/>   
    </form><br>   
  </body>   
</html>    

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>   
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"&gt;   
<struts>   
<package name="module1" namespace="" extends="struts-default">   
<action name="MyName" class="module1.User">   
    <result>/Welcome.jsp</result>   
</action>   
<action name="Blood_Group" class="module1.SelectBloodGroup" method="bloodGroupList"/>   
</package>   

</struts>

SelectBloodGroup.java

package module1;   

import java.util.ArrayList;   
import com.opensymphony.xwork2.ActionSupport;   
public class SelectBloodGroup extends ActionSupport{   
    private ArrayList<BloodGroup> bglist;   

    public String bloodGroupList(){   
        bglist = new ArrayList<BloodGroup>();   
        bglist.add(new BloodGroup("1","A+"));   
        bglist.add(new BloodGroup("2","B+"));   
        bglist.add(new BloodGroup("3","AB+"));   
        bglist.add(new BloodGroup("4","O+"));   
        bglist.add(new BloodGroup("5","A-"));   
        bglist.add(new BloodGroup("6","B-"));   
        bglist.add(new BloodGroup("7","AB-"));   
        bglist.add(new BloodGroup("8","O-"));   
        return "SUCCESS";   
    }   

    public ArrayList<BloodGroup> getBglist(){   
        return bglist;   
    }   

}   
class BloodGroup{   
    private String id;   
    private String bg;   

    BloodGroup(String id,String bg){   
        this.id=id;   
        this.bg=bg;   
    }   

} 

welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>   
<%@ taglib prefix="s" uri="/struts-tags" %>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>   

  </head>   

  <body>   
    <s:action name="Blood_Group" executeResult="false"/>    

    //***************here is the problem***************   
    <s:select list="bglist" listKey="id" listValue="bg"/>   
   //***********************************************   

  </body>   
</html>   

Struts is unable to identify bglist as a collection or Array or List or iterator. WHAT SHOULD I ASSIGN TO list ATTRIBUTE IN THE s:select TAG IN THE FILE welcome.jsp

What is wrong with the code please tell me in detail. If you could send me the correted version. WHY IS THE s:action Tag not working ?

This is the error i am getting

Apr 13, 2010 1:49:19 PM org.apache.catalina.core.ApplicationDispatcher invoke SEVERE: Servlet.service() for servlet jsp threw exception tag 'select', field 'list': The requested list key 'bglist' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

A: 

It seems you are misunderstanding the basic struts2 flow.

The page welcome.jsp is the result page (view) for the action "MyName" (bad name, BTW). It means, when the welcome.jsp page is being generated, the action "MyName" (class module1.User) has just been "executed", and it's that object (an instance of class module1.User) the one which is in the present "scope" (the valu stack) when the result is shown. So, the welcome.jsp is looking for the "bglist" list in the module1.User class.

You need to rethink you action mappings.

(Your confusion can be related to your statement "as a result of index.jsp is the welcome.jsp page" ... you must think jsp pages as result of ACTIONS, not of other jsps)

leonbloy