<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "FN" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. util. *" %>
<%!
Private Static class user {
Private int ID;
Private string name;
Private string hello;
Public int GETID () {return ID ;}
Public void setid (int id) {This. ID = ID ;}
Public String getname () {return name ;}
Public void setname (string name) {This. Name = Name ;}
Public void sethello (string Hello ){
This. Hello = Hello;
}
Public String gethello (){
Return "hello ";
}
@ Override
Public String tostring () {return "User [ID =" + ID + ", name =" + name + "]";}
Public user (int id, string name) {This. ID = ID; this. Name = Name ;}
Public user (){}
}
%> <%
Map <long, user> usermap = new hashmap <long, user> ();
Usermap. Put (1l, new user (1, "xujsh "));
Usermap. Put (2L, new user (2, "dm "));
Pagecontext. setattribute ("usermap", usermap );
Pagecontext. setattribute ("key", 1l); // This assignment method can clearly understand the key type.
%>
<C: foreach items = "$ {usermap}" Var = "entry">
<C: Out value = "$ {entry. Key}"/>
<C: Out value = "$ {entry. Value}"/>
</C: foreach> <br/>
User: <C: Out value = "$ {usermap [Key]}"/> <br/>
Make a few changes: <br/>
<%
Map <long, user> usermap2 = new hashmap <long, user> ();
Usermap2.put (1l, new user (1, "xujsh "));
Usermap2.put (2L, new user (2, "dm "));
Pagecontext. setattribute ("usermap2", usermap2 );
// Pagecontext. setattribute ("key", 1l); // you cannot set the value in this way.
%>
<C: foreach items = "$ {usermap2}" Var = "entry">
<C: Out value = "$ {entry. Key}"/>
<C: Out value = "$ {entry. Value}"/>
</C: foreach> <br/>
<C: Set Var = "key" value = "1"/> <! -- In this way, no value can be obtained for "1l" or "1". It seems to be a string! -->
<! --
View the servlet corresponding to JSP. We can see that it is indeed a string!
_ Jspx_th_c_005fset_005f0.setvar ("key ");
_ Jspx_th_c_005fset_005f0.setvalue (new string ("1 "));
-->
User: <C: Out value = "$ {usermap2 [Key]}"/> <br/>
User: <C: Out value = "$ {usermap2 [1]}"/> <br/> <! -- The number 1 can be obtained directly. -->
<PRE>
For this question, refer:
Http://stackoverflow.com/questions/924451/jstl-access-a-map-value-by-key
Basically autoboxing puts an integer object into the map. ie:
Map. Put (New INTEGER (0), "myvalue ")
That is to say, if the map key is of the numeric type, the number will be converted to an integer in automatic packing.
El (expressions versions) evaluates 0 as a long and thus goes looking for a long as the key in the map. ie It evaluates:
Map. Get (New Long (0 ))
When it is retrieved from the map, it regards the number key type as long.
[Conclusion] When we put data into the map, we need to specify the key type as long to retrieve the data correctly.
Just another helpful hint in addition to the above comment wocould be when you have a string value contained in some variable such as a request parameter.
In this case, passing this in will also result in jstl keying the value of say "1" as a sting and as such no match being found in a map hashmap.
One way to get around this is to do something like this.
<C: Set Var = "longkey" value = "$ {Param. selectedindex + 0}"/> <! -- This ugly solution! -->
This will now be treated as a long object and then has a chance to match an object when it is contained withing the map or whatever.
Then, continue as usual with something like: $ {map [longkey]} Similarly, for our problem: Obviously, <C: set Var = "key" value = "1"/> is to treat 1 as a string, so <C: out value = "$ {usermap2 [Key]}"/> no data is available! What we need to do is to forcibly convert the key type to the numeric type: <C: Out value = "$ {usermap2 [Key + 0]}"/>.
Although it is very uugly, it is eventually a solution!
</PRE>
-------------- Quick Reference ---------------- <br/>
--------------- String --------------- <br/>
<%
Pagecontext. setattribute ("op", "add ");
%>
<C: If test = "$ {op = 'add'}">
OP = "add"
</C: If>
<C: If test = "$ {op! = 'Add'} ">
Op! = "Add"
</C: If> <br/>
<%
Pagecontext. setattribute ("email ","");
%>
<C: If test = "$ {empty email}">
Email = empty
</C: If>
<C: If test = "$ {! Empty email} ">
Email! = Empty
</C: If> <br/>
------------- Null --------------- <br/>
<%
Pagecontext. setattribute ("host", null );
%>
<C: If test = "$ {Host = NULL}">
Host = NULL
</C: If>
<C: If test = "$ {Host! = NULL} ">
Host! = NULL
</C: If> <br/>
<C: If test = "$ {empty Host}">
Host = empty
</C: If>
<C: If test = "$ {! Empty Host} ">
Host! = Empty
</C: If> <br/>
[Note] You can use empty or null to determine null. <br/>
------------- Boolean --------------- <br/>
<%
Pagecontext. setattribute ("verified", true );
%>
<C: If test = "$ {verified}">
Verified = true
</C: If>
<C: If test = "$ {! Verified} ">
Verified = false
</C: If> <br/>
<C: Set Var = "haserror" value = "true"/>
<C: If test = "$ {haserror}">
Haserror = true
</C: If>
<C: If test = "$ {! Haserror} ">
Haserror = fasle
</C: If> <br/>
------------- Int --------------- <br/>
<%
Pagecontext. setattribute ("type", integer. valueof (1 ));
%>
<C: If test = "$ {type = 1}">
Type = 1
</C: If>
<C: If test = "$ {type! = 1} ">
Type! = 1
</C: If> <br/>
<C: If test = "$ {type> 0 and type <2}">
Type> 0 and type <2
</C: If> <br/>
------------- If/else --------------- <br/>
<%
Pagecontext. setattribute ("user", null );
%>
<C: Choose>
<C: When test = "$ {empty user or user. ID = 1}">
Empty user or user. ID = 1
</C: When>
<C: otherwise>
Not empty user and user. ID! = 1
</C: otherwise>
</C: Choose> <br/>
------------- List ---------------- <br/>
<%
List <string> List = new arraylist <string> ();
List. Add ("hello ");
List. Add ("world ");
List. Add ("yes ");
Pagecontext. setattribute ("list", list );
%>
<C: If test = "$ {fn: length (list)> 0}"> <! -- Why can't I directly use size for access? Sorrow! -->
List. size> 0
</C: If>
<C: If test = "$ {fn: length (list) <= 0}">
List. Size <= 0
</C: If>
<Br/>
<C: foreach Var = "out_item" items = "$ {list}" varstatus = "out_status" step = "2" begin = "0">
$ {Out_item}
<C: foreach Var = "in_item" items = "$ {list}" varstatus = "in_status" begin = "$ {out_status.index + 1}" End = "$ {out_status.index + 1}">
$ {In_item}
</C: foreach>
<Br/>
</C: foreach>
[Note] multiple elements are output each time through two loops <br/> really uugly!