• 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>

  • 相关阅读:
    Druid Monitor开启登录界面
    nginx强制使用https访问(http跳转到https)
    技术与架构
    SQLProfiler_SQL抓包
    SQL UPDATE with INNER JOIN
    MYSQL业务数据简单脱敏方案
    阿里云CodePipeline vs Jenkins
    Java Profiling & Profilers
    LINUX操作系统(centos6.9)安装与配置
    分布式ID生成系统 UUID与雪花(snowflake)算法
  • 原文地址:https://www.cnblogs.com/wadmwz/p/7545375.html
Copyright © 2020-2023  润新知