el 表达式问题这里是jsp文件里的服务器端脚本。 整个程序是struts架构 ,现在想把以下的代码换成el表达式 <%
  List<Record> alist=(List<Record>)request.getAttribute("alist");
if(alist != null && alist.size()>0) 
{
                out.println("<table border=\"2\" cellspacing=\"0\" cellpadding=\"0\">");    
                out.println("<tr>");
                out.println("<th BGCOLOR=\"#00FF00\"><b>id</b></th>");
                out.println("<td BGCOLOR=\"#00FF00\"><b><a href=User.do?act=2&sort=Score>Score</b></td>");
                out.println("<td BGCOLOR=\"#00FF00\"><b><a href=User.do?act=2&sort=UserName>UserID</b></td>");
                                out.println("</tr>");
                int j;
                int id1;
                String fullname;
                String firstName;
                String secondName;
                          
                for(int i=0;i<alist.size();i++)
                {
                        Record bean=(Record)alist.get(i);                    
                        out.println("<tr>");
                        j=i+1;
                        
                        fullname=bean.getFullName();
                        firstName=bean.getFirstName();
                        secondName=bean.getSecondName();               out.println("<td class=text>"+j+ "</td>" );  
                        out.println("<td class=text>"+ bean.getScore() + "</td>");
                        out.println("<td class=text>"+ bean.getUserName() + "</td>");
                        out.println("<td class=text>"+ firstName + "</td>");
                        out.println("<td class=text>"+ secondName + "</td>");
}我的架构是 action里处理业务逻辑 
(List<Record>)request.getAttribute("alist");
这个事情是绝对可以做的 
(第一句可以参考 c:if test="${fn:length(myActionForm.containerMap.aList) > 0}">
    <script>
        var betProfitVar='${myActionForm.containerMap.firstName}';)
while 可以 被<c:forEach var="myBean" items="${myActionForm.containerMap.aList}" varStatus="stat"> 替换
for(int i=0;i<alist.size();i++)
可以被 c:if    《/c:if》--替换  这里不是很清晰
这些代码半懂不懂,有思路但不是很精确
不知道这里如何被完整的将上面的代码替换。 希望熟悉el的结合上面的代码分析如何解决                        

解决方案 »

  1.   

    首先一个大疑问
    如果一般的input , jsp可以用 el表达式 $(param.yourinput)来解决
    现在一个复杂的对象List <Record》  (record还是一个类 也就是一个对象链表)
    List <Record> alist=(List <Record>)request.getAttribute("alist"); ----这个请问该如何来转化,很头疼,可否直接$(param.alist)????(如果不可以为什么?) 
      

  2.   

    <c:if test="${alist!=null}">
    <table border="2" cellspacing="0" cellpadding="0"> 
    <tr> 
    <th BGCOLOR="#00FF00"> <b>id </b> </th> 
    <td BGCOLOR="#00FF00"> <b> <a href=User.do?act=2&sort=Score>Score </b> </td> 
    <td BGCOLOR="#00FF00"> <b> <a href=User.do?act=2&sort=UserName>UserID </b> </td>
    </tr> 
    <c:forEach var="myBean" items="${aList}">
    <tr>
    <td class=text>${myBean.score}</td>
    <td class=text>${myBean.username}</td>
    <td class=text>${myBean.firstName}</td>
    <td class=text>${myBean.secondName}</td>
    </tr>
    </c:forEach>
    </table>
    </c:if>
      

  3.   

    <td class=text>
    <a href=User.do?act=1&firstName="+java.net.URLEncoder.encode(firstName)+"&secondName="+java.net.URLEncoder.encode(secondName)+"&birthday="+java.net.URLEncoder.encode(birthday)------这里如何转换
    ${myBean.secondName} </td> 之前是服务器端脚本《%。。%》所以可以用java.net.URLEncoder.encode函数
    1现在是客户端脚本 这里不知道该如何转化(需要找到对应的.encode函数
    2<a href=User.do?act=1&firstName="+ ${myBean.firstName}+。--请问可以这样用吗?) 
      

  4.   

    <c:forEach var="myBean" items="${aList}"> 
    <tr> 
    <td class=text>${myBean.score} </td> 
    <td class=text>${myBean.username} </td> 
    <td class=text>${myBean.firstName} </td> 
    <td class=text>${myBean.secondName} </td> 
    </tr> 
    </c:forEach> 
      

  5.   

    URLEncding可以使用jstl
    <c:url var="test" value="www.baidu.com">
    <c:param name="id" value="3"></c:param>
    <c:param name="name" value="张三"></c:param>
    </c:url>
    <c:out value="${test}"></c:out>
      

  6.   

    jsp代码用el表达式如下
    <c:forEach var="myBean" items="${aList}"> 
    <tr> 
    现在要加入行号如 --- <td class=text>"+j+ " </td>"   ==这里如何用el表达
    <td class=text>${myBean.score} </td> 
    <td class=text>${myBean.username} </td> 
    <td class=text>${myBean.firstName} </td> 
    <td class=text>${myBean.secondName} </td> 
    </tr> 
    </c:forEach> 
    原来的代码请参考   <td class=text>"+j+ " </td>" );   如何在el表达式里写?     
       for(int i=0;i <alist.size();i++) 
                    { 
                            Record bean=(Record)alist.get(i);                    
                            out.println(" <tr>"); 
                            j=i+1; 
                            
                            fullname=bean.getFullName(); 
                            firstName=bean.getFirstName(); 
                            secondName=bean.getSecondName();               out.println(" <td class=text>"+j+ " </td>" );  
      

  7.   

    <c:forEach var="myBean" items="${aList}" indexId="i"> <td>${i+1}</td>
      

  8.   

    这里你也可以用logic中的迭代标签做
      

  9.   

    发现是不成功的 
    下面el表达式
    c:if test="${alist!=null}"> 
    <table border="2" cellspacing="0" cellpadding="0"> 
    <tr> 
    <th BGCOLOR="#00FF00"> <b>id </b> </th> 
    <td BGCOLOR="#00FF00"> <b> <a href=User.do?act=2&sort=Score>Score </b> </td> 
    <td BGCOLOR="#00FF00"> <b> <a href=User.do?act=2&sort=UserName>UserID </b> </td> 
    </tr> 
    <c:forEach var="myBean" items="${aList}"> 
    <tr> 
    <td class=text>${myBean.score} </td> 
    <td class=text>${myBean.username} </td> 
    <td class=text>${myBean.firstName} </td> 
    <td class=text>${myBean.secondName} </td> 
    </tr> 
    </c:forEach> 
    </table> 
    </c:if>jsp页面上的标题 <td BGCOLOR="#00FF00"> <b> <a href=User.do?act=2&sort=Score>Score </b> </td> 
    <td BGCOLOR="#00FF00"> <b> <a href=User.do?act=2&sort=UserName>UserID </b> </td> 
    </
    能出来,  但是没有数据 。 (实际上是有很多的数据的)
      

  10.   

     原创  jsp EL表达式结合JSTL标准标签实现分页 收藏<%@ page contentType="text/html; charset=GBK" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
    <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql_rt" %>
    <%@taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
    <html>
    <head>
    <title>使用SQL标签</title>
    </head><body bgcolor="white">
    <c:set var="noOfRows" value="10" />
    <sql:setDataSource driver =
    "sun.jdbc.odbc.JdbcOdbcDriver" url =
    "jdbc:odbc:accp" user="sa" password="" var="conn"/>
    <c:if test="${custList == null}">
    <sql:query var="custList" scope="session"
    sql="SELECT * FROM employee ORDER BY LName"
    dataSource="${conn}"/>
    </c:if>
    <c:choose>
    <c:when test="${custList.rowCount == 0}">
    此处不再有其他客户...
    </c:when>
    <c:otherwise>
    <b>以下是客户列表:</b>
    <p>
    <table border="1">
    <th>姓氏</th>
    <th>名字</th>
    <th>年龄</th>
    <c:forEach items="${custList.rows}" var="row"
    begin="${param.start}" end="${param.start +
    noOfRows - 1}">
    <tr>
    <td><c:out value="${row.LName}" /></td>
    <td><c:out value="${row.FName}" /></td>
    <td><c:out value="${row.job_id}" /></td>
    </tr>
    </c:forEach>
    </table>
    </c:otherwise>
    </c:choose>
    <p>
    <c:choose>
    <c:when test="${param.start > 0}">
    <a href="foreachexample.jsp?start=<c:out
    value="${param.start - noOfRows}" />">
    上一页</a>
    </c:when>
    <c:otherwise>
    上一页
    </c:otherwise>
    </c:choose>
    <c:choose>
    <c:when test="${param.start + noOfRows <
    custList.rowCount}">
    <a href="foreachexample.jsp?start=<c:out
    value="${param.start + noOfRows}" />">
    下一页</a>
    </c:when>
    <c:otherwise>
    下一页
    </c:otherwise>
    </c:choose>
    共${custList.rowCount}条记录&nbsp;
    共 <fmt:formatNumber type="number" value="${custList.rowCount%noOfRows==0?custList.rowCount/noOfRows:(custList.rowCount%noOfRows>4?(custList.rowCount/noOfRows):(custList.rowCount/noOfRows+1))}" maxFractionDigits="0"/> 页 &nbsp;第 <fmt:formatNumber type="number" value="${(param.start%noOfRows==0?param.start/noOfRows:(param.start%noOfRows>4?(param.start/noOfRows):(param.start/noOfRows+1)))+1}" maxFractionDigits="0"/> 页
    </body>
    </html>说明:这里使用到了标准标签库中SQL标签去查询数据记录。使用条件标签判断分页中上下页的是否链接。使用EL表达式显示总记录数,<c:set var="noOfRows" value="10" />用来设置每页的显示记录数。通过总记录数和每页记录数来计算总页数。因为EL表达式中‘/’结果是实际的浮点数,而不是整除,也没有函数来完成取整,这里就结合格式化标签<fmt:formatNumber type="number" value="${custList.rowCount%noOfRows==0?custList.rowCount/noOfRows:(custList.rowCount%noOfRows>4?(custList.rowCount/noOfRows):(custList.rowCount/noOfRows+1))}" maxFractionDigits="0"/>来完成格式化取整,maxFractionDigits="0"表示保留0位小数,即取整,但是这的取整也只是四舍五入,所以又给计算页数带来困难,最后我通过${custList.rowCount%noOfRows==0?custList.rowCount/noOfRows: (custList.rowCount%noOfRows>4?(custList.rowCount/noOfRows): (custList.rowCount/noOfRows+1))}两个条件表达式嵌套完成页数计算用同样的方法实现计算第几页的目的