jsp中C标签c:forEach的使用

JSTL中提供了与URL相关的标签,分别是c:import , c:url , c:redirect , c:param .其中c:parm标签通常与其他标签配合使用.
jsp页面需要引入标签代码如下
jar包及maven引入地址
http://mvnrepository.com/artifact/javax.servlet/jstl/1.2

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

循环标签 forEach使用

forEach>

User.java

public class User {

    String name;//姓名

    int telephone;//电话号

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTelephone() {
        return telephone;
    }

    public void setTelephone(int telephone) {
        this.telephone = telephone;
    }


}

T.java

    import java.io.IOException;
import java.util.ArrayList;

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

/**
 * Servlet implementation class T
 */
public class T extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public T() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("----");
        ArrayList arrayList = new ArrayList();
        System.out.println("进入");
        User user = new User();
        user.setName("崔崇东");
        user.setTelephone(10086);
        User user2 = new User();
        user2.setName("lily");
        user2.setTelephone(1008625);
        arrayList.add(user);
        arrayList.add(user2);
        User user3 = new User();
        user3.setName("DUCK");
        user3.setTelephone(1008625);
        arrayList.add(user3);

        User user4 = new User();
        user4.setName("bUC8");
        user4.setTelephone(1008625);
        arrayList.add(user4);

        request.setAttribute("userlist", arrayList);
        RequestDispatcher rd = request.getRequestDispatcher("index2.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);
    }

}
    }

index.jsp部分内容

<c:forEach items="${userlist}" var="user">


        <div class="sort_list">
            <div class="num_logo">
                <img src="img/img.jpg" alt="">
            div>
            <div class="num_name">${user.name}   <a href="${user.telephone}">拨打电话a>    <a href="sms:18945086283?body=短信内容" data-role="button" data-theme="a">测试发短信a>div>
        div>
c:forEach>

jsp中C标签c:forEach的使用_第1张图片
默认

<option value="0"}>全部option>
  forEach items="${dopli}" var="DopName">

     <option if test="${DopName.no==no}">selected="selected"if>value=${DopName.no}>${DopName.name}option>


forEach>

你可能感兴趣的:(java)