J2EE中EL和JSTL结合运用

这里的例子只是简单的Servlet+JSP,没有用到框架,如果用到Struts的话,只要符合Struts中Action标准,或者直接用Struts标签替代就可以了。

 

ToMainAction

public class ToMainAction extends HttpServlet
{
 private IBoarderDao boardDao = new BoardDaoImpl();
 private ITopicDao topicDao = new TopicDaoImpl();
 private IUserDao userDao = new UserDaoImpl();

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {
  // 板块列表
  List<Board> boards = boardDao.getAllBoard();
  List<Integer> count = new ArrayList<Integer>();
  List<User> users = new ArrayList<User>();
  List<Topic> lastTopic = new ArrayList<Topic>();

  if (null != boards)
  {
   for (Board b : boards)
   {
    // 回帖数
    List<Topic> topic = topicDao.getTopicByBoardId(b.getBorderId());
    if (null != topic)
    {
     int num = topic.size();
     count.add(num);
    }
    else
    {
     count.add(0);
    }

    // 最近更新
    Topic t = topicDao.getLastTopic(b.getBorderId());
    lastTopic.add(t);

    // 最近更新的作者
    User u = userDao.getUserByuId(t.getUid());
    users.add(u);
   }

   request.setAttribute("boards", boards);
   request.setAttribute("count", count);
   request.setAttribute("users", users);
   request.setAttribute("lastTopic", lastTopic);

   RequestDispatcher dis = request.getRequestDispatcher("main.jsp");
   dis.forward(request, response);
  }

 }

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {
  this.doGet(request, response);
 }

}

 

main.jsp

<body>
  <div id="container">
   <div id="divlogin">
    <jsp:include page="checUser.jsp"></jsp:include>
   </div>
   <div class="nav">
    <div align="left" style="font-weight: bold"></div>
   </div>
   <table width="100%" border="3" bordercolor="#D0E3FF"
    style="font-size: 12px">

    <tr style="background-color: #F3F8EF">
     <td height="22" colspan="2" class="noborder">
      &nbsp;
     </td>
     <td width="11%" style="border: 0; font-size: 12px">
      发帖数
     </td>
     <td width="21%" style="border: 0; font-size: 12px">
      最近更新
     </td>
    </tr>

    <c:if test="${requestScope.boards!=null}">
     <c:forEach var="b" items="${requestScope.boards}"  varStatus="status">
      <tr>
       <td width="6%" height="68">
        &nbsp;
       </td>
       <td width="67%">
        <div align="left" class="blueSpan">
         &nbsp;&nbsp;
         <img src="images/topic.gif" width="18" height="21" />
         &nbsp;&nbsp;
         <a href="ToListAction?boardId=${b.borderId}">${b.borderName}</a>
        </div>
       </td>
       <td>
        ${requestScope.count[status.index]}
       </td>
       <td>
        <br />
        <p align="left">
         ${requestScope.lastTopic[status.index].title}
        </p>
        <br />
        <p align="left">
         ${requestScope.users[status.index].userName}
        </p>
        <br />
        <p align="left">
         修改时间:
         <br>
         ${requestScope.lastTopic[status.index].modifyTime}        
        </p>
       </td>
      </tr>
     </c:forEach>
    </c:if>

   </table>

   <div id="divCopy">
    &copy; xy制作—2011-09-01
   </div>
  </div>
 </body>

 

checUser.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<c:if test="${not empty sessionScope.uinfo}">
 <span style="color: blue">欢迎您:${sessionScope.uinfo.userName} </span>
 <span style="color: blue"><a href="GoBackAction">退出</a> </span>
</c:if>
<c:if test="${empty sessionScope.uinfo}">
 <span style=""><a href="ToLoginAction">登录</a></span>
</c:if>


你可能感兴趣的:(EL,jstl)