You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Gupta <gu...@nous.soft.net> on 2002/01/06 19:10:44 UTC

Struts Question of select and options tag

Hi Guys,
I am new to struts  frame work and I am facing little difficult.

I have a select control in html form using  below code to display in html
form (not using html tag lib) in MVC2 model by reading key by key and
reading its value ,
Now i want to do it in struts framework with struts taglib,I am halfway
through but not full,I hope u guys help me out in this using tag libs.
<%            Hashtable hashmap=(Hashtable)request.getAttribute("codesMap");
              String
selectedOption=(String)request.getAttribute("selectedOption");
              //MVC2 approach
%>
          <% if(hashmap!=null){   //it can be null some cases
                //fetch one object and keep it in
//          %><select name="codes">
            <%  Enumeration enum=hashmap.keys();
                while(enum.hasMoreElements()){
                    Object key=enum.nextElement();
                    Object value=hashmap.get(key);
			  String selected="";
                    if(selectedOption!=null){
	                    if(value.equals(selectdOption)){
      	                     selected="Selected";
            	        }else{
                  	         selected="";
                    	  }
                    }
	     %>
                 <option name ="<%=key%>" <%=selected%> > <%=value%>
</option>
           <%     }//while ends %>
                </select>
          <%  }if ends
              else{
                     //if hashmap is null then print some thing
          %>
                  <b>SOrry no codes available.</b>
          <% } //else ends%>

in some sequeces the hashmap object may be null(if request comes from some
other pages) so how would i check an hashmap is null or not and if hashmap
is not null in taglibs I would like to dispaly these hashmap values into
select html control with default selection, If i dont specify which option
should be selected default,It should select the first option in select box
as default.Becoz hashmap values are not fixed.

In short i am looking for a taglib solution for above piece of code

TAI
Yaman & guptha



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Struts Question of select and options tag

Posted by Ted Husted <hu...@apache.org>.
Have you looked at using the options tag with the select tag?

http://jakarta.apache.org/struts/struts-html.html#options

And then use the logic:present/logic:notPresent tags to check for the
null instance. 

See also 

http://jguru.com/faq/view.jsp?EID=549097

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Building Java web applications with Struts.
-- Tel +1 585 737-3463.
-- Web http://www.husted.com/struts/

Gupta wrote:
> 
> Hi Guys,
> I am new to struts  frame work and I am facing little difficult.
> 
> I have a select control in html form using  below code to display in html
> form (not using html tag lib) in MVC2 model by reading key by key and
> reading its value ,
> Now i want to do it in struts framework with struts taglib,I am halfway
> through but not full,I hope u guys help me out in this using tag libs.
> <%            Hashtable hashmap=(Hashtable)request.getAttribute("codesMap");
>               String
> selectedOption=(String)request.getAttribute("selectedOption");
>               //MVC2 approach
> %>
>           <% if(hashmap!=null){   //it can be null some cases
>                 //fetch one object and keep it in
> //          %><select name="codes">
>             <%  Enumeration enum=hashmap.keys();
>                 while(enum.hasMoreElements()){
>                     Object key=enum.nextElement();
>                     Object value=hashmap.get(key);
>                           String selected="";
>                     if(selectedOption!=null){
>                             if(value.equals(selectdOption)){
>                              selected="Selected";
>                         }else{
>                                  selected="";
>                           }
>                     }
>              %>
>                  <option name ="<%=key%>" <%=selected%> > <%=value%>
> </option>
>            <%     }//while ends %>
>                 </select>
>           <%  }if ends
>               else{
>                      //if hashmap is null then print some thing
>           %>
>                   <b>SOrry no codes available.</b>
>           <% } //else ends%>
> 
> in some sequeces the hashmap object may be null(if request comes from some
> other pages) so how would i check an hashmap is null or not and if hashmap
> is not null in taglibs I would like to dispaly these hashmap values into
> select html control with default selection, If i dont specify which option
> should be selected default,It should select the first option in select box
> as default.Becoz hashmap values are not fixed.
> 
> In short i am looking for a taglib solution for above piece of code
> 
> TAI
> Yaman & guptha
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>