Thank you very much Dustin, Paul and Richard for your detailed explanation
and for your valued time. I will try this.

 

Thanks

/Bandula

 

From: Dustin Pearce [mailto:[EMAIL PROTECTED] 
Sent: Monday, June 02, 2008 8:57 PM
To: users@appfuse.dev.java.net
Subject: Re: [appfuse-user] Drop-Down List

 

That approach is fine if you are working with a single page and a single
drop-down, but when you need to share a drop-down definition several times
it starts crumble a bit.  What usually do for all of my applications is I
first add a DataDictionary POJO with the purpose of creating all the lists I
need to use in the application.  I will get to the code, but I need to take
a tangent first....

 

So a DataDictionary has a Set<String> values.  So I create a DataDictionary
named "Status" and I add the String status values to its values Set.  Notice
that the values have no key, so the String gets saved for the drop-down
field.  I went to this model more and more recently as I found 99% of the
time, option values were not changing and when they did, a simple String
replace was just fine since it was usually a big decision to change it.  The
upside is that I have the actual value in my record for queries and reports
and don't have to constantly "lookup" the text for Status #1.     All this
being said, I have a couple implementations where instead of Set<String>
values I have used <Set>DataDictionaryItem values and the item POJO has
optValue and optKey properties....

 

 

To the code (Some code/syntax paired for brevity)

****DataDictionary.java********

@Entity

@Table(name = "datadictionary")

public class DataDictionary{

    @Id

    @GeneratedValue

    private Long id;

    private int version;

    private String name;

    @CollectionOfElements(fetch = FetchType.EAGER)

        @JoinTable(name = "datadictionary_values",

        joinColumns = @JoinColumn(name = "datadictionary_id"))

        @Column(name = "value")

        @IndexColumn(name = "position")

    private List<String> values;

 

Note, the position @IndexColumn.  If you wish you could just as well add an
@OrderBy instead but realize that will then sort all your lists
alphabetically.  I traded the flexibility of being able to order my lists
however I want for having to manually alphabetize if I that is what I want.

 

****sample-data.xml****

    <table name="datadictionary">

        <column>id</column>

        <column>version</column>

        <column>name</column>

            <row>

                        <value description="id">1</value>

                        <value description="version">0</value>

                        <value description="name">Status</value>

            </row>

    </table>

 

    <table name="datadictionary_values">

        <column>datadictionary_id</column>

        <column>value</column>

        <column>position</column>

 

        <!--Status-->

        <row>

            <value>1</value>

            <value>Pending</value>

            <value>0</value>

        </row>

        <row>

            <value>1</value>

            <value>Open</value>

            <value>1</value>

        </row>

        <row>

            <value>1</value>

            <value>Closed</value>

            <value>2</value>

        </row>

   </table>   

 

I just use xml and dbunit to define and load my keyword lists into the
database.  A UI would be nice, but I found in the real world its just once
or twice and then never again.  

 

 

****ApplicationContext,xml**

    <!--DataDictionaryManager-START-->

    <bean id="dataDictionaryManager"
class="com.jmh.foundation.service.impl.GenericManagerImpl">

        <constructor-arg>

            <bean
class="com.jmh.foundation.dao.hibernate.GenericDaoHibernate">

                <constructor-arg
value="com.jmh.foundation.model.DataDictionary"/>

                <property name="sessionFactory" ref="sessionFactory"/>

            </bean>

        </constructor-arg>

    </bean>

 

 

Create a GenericMananger/Dao for DataDictionary to look them up when we
startup the application.

 

 

 

 

**MyAppStartupListener.java**

public class MyAppStartupListener extends StartupListener implements
ServletContextListener {

    private static Log log = LogFactory.getLog(MyAppStartupListener.class);

 

    public void contextInitialized(ServletContextEvent event) {

        super.contextInitialized(event);

        log.debug("initializing Myapp context...");

        ServletContext context = event.getServletContext();

        setupJmicContext(context);

    }

 

    public static void setupJmicContext(ServletContext servletContext) {

        ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

        GenericManager gmgr = (GenericManager)
ctx.getBean("dataDictionaryManager");

        List<DataDictionary> dictionaries = gmgr.getAll();

        Set<DataDictionary> uniqueDictionaries = new
HashSet<DataDictionary>(dictionaries);

        for (DataDictionary dic : uniqueDictionaries) {

            log.debug("Loading dictionary  values for :" + dic.getName());

            servletContext.setAttribute(dic.getName(), dic.getValues());

        }

    }

}

 

Extends the Appfuse startup listener and loads all of the defined
DataDictionaries into the ServletContext.  This will let us access the lists
using the <s:select list="#attr.Status".../> later.  The super(...) calls
makes sure that Appfuse gets initialized properly before your app does its
thing.

 

***web.xml**

    <listener>

 
<listener-class>com.jmh.hcc.webapp.listener.MyAppStartupListener</listener-c
lass>

    </listener>

 

Swap your StartupListener in for the Appfuse listener.  

 

***form.jsp***

 

<s:select list="#attr.Status" emptyOption="true" ...../>

 

That is when your dictionary values are a regular Set<String>.  If you went
for the full value/key DataDictionaryItem child object then ......

 

<s:select list=#attr.Status" listValue="optValue" listKey="optKey" ..../>

 

Remember the property names of our child object way back at the top?  ......


The #attr.[nameoflist] uses the name of the dictionary object you defined in
the xml.  

 

 

Now you can create as many lists as you want and share them anywhere in the
application.  They are all loaded into memory for great performance.   So
now we just need some people to post the dbunit xml for inserting a list of
state, country, etc Strings.......

 

 

-D

 

 

 

 

 

 

 

 

On Jun 1, 2008, at 3:16 PM, Paul Were wrote:





Bandula,
To be consistent.
It is better practice to make your view layer interact with the action
support controller struts MVC framework.
If this is the case the your taskForm.jsp  should be tied to TaskAction.java
which should be your action class.

In you Action class declare


private List availableStatuses;
private static final PENDING = "PENDING";
private static final OPEN = "OPEN";
private static final DONE = "DONE";
private static final HOLD = "HOLD";


Then have a method in your action class.

public List getAvailableStatuses(){
        if(availableStatuses != null){
             return availableStatuses;
        } else {
            availableStatuses= new ArrayList();
            availableStatuses.add(PENDING);
            availableStatuses.add(OPEN);
            availableStatuses.add(DONE);
            availableStatuses.add(HOLD);
          return availableStatuses;
        }
    }


In your taskForm.jsp

you add 

<s:select label="Available Status" 
                name="StatusPojo.status" 
                headerKey="1"
                headerValue="-- Please Select --"
                list="availableStatuses"
    />


Make the appropriate declarations in your struts config and
applicationContext to hook you action, pojo and jsp.




Hope this helps.

Paul Were.




"Richard Mixon (CustCo)" <[EMAIL PROTECTED]> wrote:

I'm pretty new to Struts2 myself, but all of my Appfuse Struts2 pages use a
select tag something like this:

        <s:select name="storyText.status.id"
         list="activeStatusList" listKey="id" listValue="name"
key="storyText.status"/> 
I think your tag looks more like a Struts(1) tag.

Here is the Struts2 reference page for the select tag:
    http://struts.apache.org/2.0.11.1/docs/select.html

Hope this helps.



  _____  

From: Bandula Rathnasekara <[EMAIL PROTECTED]>
Reply-To: <users@appfuse.dev.java.net>
Date: Sun, 1 Jun 2008 12:36:01 +0530
To: <[EMAIL PROTECTED]>
Cc: <users@appfuse.dev.java.net>
Subject: [appfuse-user] Drop-Down List

Hi all,
 
I want to add simple drop down list in a JSP page with my Struts2 web
application on AppFuse framework. I went through related post in web and
tried to do it with StartupListener class.
 
I created/modified following classes/jsp.
 
1.      Created Statuses POJO. 

Not used for the time being

 

2.      Constant

public static final String AVAILABLE_STATUSES =  "availableStatuses";

 

3.      StartupListner

context.setAttribute(Constants.AVAILABLE_STATUSES ,
mgr.getObjects(TaskStatuses.class));

 

4.      LookupManager

public List<LabelValue> getObjects(Class<TaskStatuses> name);

 

5.      LookupManagerImpl

public List<LabelValue> getObjects(Class<TaskStatuses> name) {

     List<LabelValue> list = new ArrayList<LabelValue>();

     list.add(new LabelValue("PENDING", "PENDING"));

     list.add(new LabelValue("OPEN", "OPEN"));

     list.add(new LabelValue("DONE", "DONE"));

     list.add(new LabelValue("HOLD", "HOLD"));

     return list;

}

 
6.      taskForm.jsp

<html:select property="status">  

     <html:options collection="availableStatuses" property="status"
labelProperty="name"/>  

</html:select>

 



 
I was struggling with this whole yesterday and I could not populate drop
down-list in my jsp. I don't even get error in JSP page. Is it do something
with tag libraries in? Any configuration XML file? Can you tell me where
else I need to change/correct. 
 
If anyone has sample code on this or a place where I can find them pls let
me know.
 
Thanks &Regards,
Bandula

 

 

 

Reply via email to