• jstl标签库示例二


    package app05b;

    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;


    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     * 城市控制类
     */
    @WebServlet( name = "BigCitiesServlet", urlPatterns = "/bigCities")
    public class BigCitiesServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public BigCitiesServlet() {
            super();
            // TODO Auto-generated constructor stub
        }

        /**
         *
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 创建map集合
            Map<String, String> capitals = new HashMap<String, String>();
            // 放入键值对
            capitals.put("Indonesia", "Jakarta");
            capitals.put("Malaysia", "Kuala Lumpur");
            capitals.put("Thailand", "Bangkok");
            // 向页面传递参数
            request.setAttribute("capitals", capitals);
            // 创建map集合,值是数组
            Map<String, String[]> bigCities = new HashMap<String,String[]>();
            // 放入值
            bigCities.put("Australia", new String[] {"Sydney",
                    "Melbourne", "Perth"});
            bigCities.put("New Zealand", new String[] {"Auckland",
                    "Christchurch", "Wellington"});
            bigCities.put("Indonesia", new String[] {"Jakarta",
                    "Surabaya", "Medan"});
            
            request.setAttribute("capitals", capitals);
            request.setAttribute("bigCities", bigCities);
            /*Set s = bigCities.keySet();
            Iterator it = s.iterator();
            while(it.hasNext()){
                System.out.println(it.next());
            }*/
            // 重定向
            RequestDispatcher rd = request.getRequestDispatcher("/Cities.jsp");
            rd.forward(request, response);
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }

    }


    jsp页面(需要引入jstl的两个jar包)

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
      <!--   引入jstl标签库 -->
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix = "c" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Big Cities</title>
    <style>
        table,tr,td{
            border: 1px solid #ae7;
            padding: 3px;
        }
    </style>
    </head>
    <body>
    Capitals
    <table>
        <tr style="background:#448755;color:white;font-weight:bold;">
            <td>Country</td>
            <td>Capital</td>
        </tr>
        <!-- 遍历map集合 -->
        <c:forEach items="${ requestScope.capitals}" var="mapItem">
            <tr>
                <td>${mapItem.key }</td>
                <td>${mapItem.value }</td>
            </tr>
        </c:forEach>
    </table>
    <br>
    Big Cities
    <table>
        <tr style="background:#448755;color:white;font-weight:bold;">
            <td>Country</td>
            <td>Cities</td>
        </tr>
        <!-- foreach遍历 -->
        <c:forEach items="${ requestScope.bigCities }" var= "mapItem">
            <tr>
                <td>${mapItem.key }</td>
                <td>
                    <!-- 由于值是String数组,因此需要进一步遍历 -->
                    <c:forEach items="${mapItem.value }" var="city" varStatus="status">
                        ${city}<c:if test="${!status.last }">,</c:if>
                    </c:forEach>
                </td>
            </tr>
        </c:forEach>
    </table>

    </body>
    </html>

  • 相关阅读:
    .Net操作Sharepoint常用方法(1) 判断List或Library是否存在
    Aspose.Cells读取Excel获取DataTable方法
    Nginx系统学习笔记(1)Say Hello
    老刘Hadoop学习笔记(6)Hadoop 单机模式 伪分布式 完全分布式区别
    老刘Hadoop学习笔记(5)安装JDK
    老刘Hadoop学习笔记(4)JDK和Open JDK区别
    老刘Hadoop学习笔记(3)新建用户及用户组
    老刘Hadoop学习笔记(2)修改虚拟机hostname及hosts文件
    老刘Hadoop学习笔记(1)开始学习
    MySQL中变量为空时不执行where过滤
  • 原文地址:https://www.cnblogs.com/wadmwz/p/7545375.html
Copyright © 2020-2023  润新知