For Newbie.... Like me guide...

Reference: 
http://jumpstart.doublenegative.com.au:8080/jumpstart/examples/select/easyid
Select ID  

Index.tml


        <form t:type="form">
                Person: <select t:type="select" t:model="personIds"
t:value="personId"></select>
                <input type="submit" value="Submit"/>
                You chose personId: ${personId}
        </form>



Index.java


import com.dash.tapestryselect2.model.IdSelectModel;
import com.dash.tapestryselect2.model.Userbean;
import java.util.ArrayList;
import java.util.Date;


import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;

public class Index
{
        public Date getCurrentTime() 
        { 
                return new Date(); 
        }

        // The activation context

        @Property
        private Long personId;

        // Screen fields

        @SuppressWarnings("unused")
        @Property
        private IdSelectModel<Userbean> personIds;

        @Inject
        private PropertyAccess _propertyAccess;

        // The code

        Long onPassivate() {
                return personId;
        }

        void onActivate(Long personId1) {
                personId = personId1;
        }

        // Form triggers the PREPARE event during form render and form 
submission.

        void onPrepare() {
                // Get all persons - ask business service to find them (from 
the database)
                ArrayList<Userbean> persons = new ArrayList<Userbean>();
                persons.add(new Userbean(Long.valueOf(1),"Mirko","Mirko"));
                persons.add(new
Userbean(Long.valueOf(2),"Slavko","Slavko"));
                persons.add(new Userbean(Long.valueOf(3),"Jozo","Jozo"));
                personIds = new IdSelectModel<Userbean>(persons, Userbean.class,
"username", "uid", _propertyAccess);
        }

}




IdSelectModel.java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.dash.tapestryselect2.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.OptionGroupModel;
import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.internal.OptionModelImpl;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.ioc.services.PropertyAdapter;
import org.apache.tapestry5.util.AbstractSelectModel;

public class IdSelectModel<T> extends AbstractSelectModel {

        private List<T> list;
        private PropertyAdapter labelFieldAdapter;
        private PropertyAdapter idFieldAdapter;

        /**
         * @param list the list of objects you want modeled in a Select 
component.
These objects MUST implement
         *        equals(Object obj) and hashCode(). If the objects are JPA
entities, ensure their implementations of
         *        equals(Object obj) and hashCode() return the same thing for
different instances of the same detached
         *        entity.
         * @param clazz the class of objects in the list.
         * @param labelField the name of the field you want displayed as the 
label
in the selection list, eg. "name".
         * @param idField the name of the field which is the unique identifier 
of
each object in the list, eg. "id". This is
         *        used in the value property of the Select component.
         * @param access Declare a PropertyAccess injected into your page (eg.
         * @Inject private PropertyAccess _access) then pass it in here.
         *
         */
        public IdSelectModel(List<T> list, Class<T> clazz, String labelField,
String idField, PropertyAccess access) {
                if (clazz == null) {
                        throw new IllegalArgumentException("clazz is 
required.");
                }
                if (idField == null) {
                        throw new IllegalArgumentException("idField is 
required.");
                }
                if (labelField == null) {
                        throw new IllegalArgumentException("labelField is 
required.");
                }

                this.list = list;
                this.idFieldAdapter =
access.getAdapter(clazz).getPropertyAdapter(idField);
                this.labelFieldAdapter =
access.getAdapter(clazz).getPropertyAdapter(labelField);

                if (idFieldAdapter == null) {
                        throw new IllegalArgumentException("idField " + idField 
+ " does not
exist in class " + clazz + ".");
                }
                if (labelFieldAdapter == null) {
                        throw new IllegalArgumentException("labelField " + 
idField + " does not
exist in class " + clazz + ".");
                }
        }

        public List<OptionGroupModel> getOptionGroups() {
                return null;
        }

        public List<OptionModel> getOptions() {
                List<OptionModel> optionModelList = new 
ArrayList<OptionModel>();
                for (T obj : list) {
                        optionModelList.add(new 
OptionModelImpl(nvl(labelFieldAdapter.get(obj)),
idFieldAdapter.get(obj)));
                }
                return optionModelList;
        }

        public List<T> getList() {
                return list;
        }

        private String nvl(Object o) {
                if (o == null)
                        return "";
                else
                        return o.toString();
        }
}



Userbean.java



public class Userbean {

    Long uid;
    String username;
    String userpassword;

    public Userbean(Long uid, String username, String userpassword) {
        this.uid = uid;
        this.username = username;
        this.userpassword = userpassword;
    }

    public Long getUid() {
        return uid;
    }
    public void setUid(Long uid) {
        this.uid = uid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUserpassword() {
        return userpassword;
    }
    public void setUserpassword(String userpassword) {
        this.userpassword = userpassword;
    }
}




Note: This example will use the select component of Tapestry5 then will
return the ID.

-- 
View this message in context: 
http://tapestry.1045711.n5.nabble.com/T5-Component-Select-ID-Example-tp3375346p3375346.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to