用JSTL标签访问list且判断list的选中项

上一篇 / 下一篇  2008-01-02 09:36:00 / 个人分类:JAVA地带

一般而言,list对象会存储在request对象,session对象中,一般采用框架(比如说Struts框架中的Action)完成把list对象置入request对象中,
  
  XXAction{
  
  public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) {
  
  /*从session中获得用户信息*/
  
  HttpSession session=request.getSession();
  User user =(User)session.getAttribute("user");
  request.setAttribute("user",user);
  
  /*建立rolelist*/
  
  List rolelist =new ArrayList();
  for(int i=0;i< 5;i++){
  Role role= new Role(i,"role"+i);
  rolelist.add(role);
  }
  
  request.setAttribute("role_list",rolelist);
  
  }
  
  本文的讲解重点是jstl标签部分:
  
  < %@ taglib uri="/jstl/core" prefix="c"%>
  
  < c:set var="user" value="${user}" />
  
  < tr bgcolor="#FFFFFF">
  < td align="center" valign="top">
  
  < !--
  
  因为涉及到中文字符问题,所以通过使用struts标签来读者资源文件中的adduser.role项,对应的中文编码为:角色-->
  
  < bean:message key="adduser.role" />< /td>
  < td>
  < select name="role">
  < c:forEach var="ro" items="${role_list}">< !--访问request中的role_list对象-->
  < c:choose>
  < c:when test="${user.role==ro.role_name}">< !--判断,相当于if(user.getrole().equlas(rolgetrolename()))假如user中的role和list中的role相同就在select中选中(高亮)-->
  
  < option selected="selected"
  value="< c:out value="${ro.role_id}"/>">< !--设定value项,这样保证页面中看到的是name,当选中
  
  以后,传到接受端实际为id-->
  < c:out value="${ro.role_name}"/>< !--看到的是role_name-->
  < /option>
  < /c:when>
  < c:otherwise>< !--相当于else-->
  < option value="< c:out value="${ro.role_id}"/>">< !--原理同上-->
  < c:out
  value="${ro.role_name}">< /c:out>< /option>
  < /c:otherwise>
  < /c:choose>
  < /c:forEach>< !--循环end-->
  
  < /select>< /td>
  < /tr> 

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-08-30  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 1727
  • 日志数: 101
  • 建立时间: 2007-06-14
  • 更新时间: 2008-07-11

RSS订阅

Open Toolbar