应用EL与JSTL在不同jsp页面中获取普通,数组,String,list,set和map对象

逻辑链为:jsp页面链接——》调用相应的Servelt——》在新的jsp页面上显示。

1.首先,先写一个jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>

<a href="./GetServlet">获取</a>

</body>
</html>

./GetServlet就是点击之后会调用的servlrt。

2.在GetServlet中书写我们的各类对象

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Map;

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

import sun.jd.role.student;

@WebServlet("/JumpServlet")
public class GetServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        request.setAttribute("name","小华");//字符串对象
        request.setAttribute("student",new Student("01","梅梅","海南"));//类对象
        request.setAttribute("names",new String[]("苏苏","陌陌","珊瑚"));//数组对象
       
        List<Integer> list=new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        request.setAttribute("list",list);//list对象

        Set<Integer> set=new HashSet<Integer>();
        set.add(4);
        set.add(5);
        set.add(6);
        request.setAttribute("set",set);//Set对象
        
        Map<String,Integer> map=new HashMap<String,Integer>();
        map.put("pipi",150);
		map.put("jiangwu",100);
		map.put("mm",300);
        request.setAttribute("maps",map);//map对象

        request.getRequestDispatcher("target.jsp").forward(request,response);
}

}

通过Servelt向target.jsp发送了大量对象。

3.在target页面获取到。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${name}
${student.id}  ${student.name}
<br/>

${names[0]}  
${names[1]}   
${names[2]} 
<br/>
<!--普通对象 ${key}  类对象${key.属性值}  数组对象${key[number]}-->

<c:forEach var="n" items="${names}">
${n} 
</c:forEach>
<br/> 
<!--数组的另一种获取方法 利用加强for循环 
-->

${lists[0]} 
<br/> 
<c:forEach var="l" items="${lists }">
${l} 
</c:forEach>
<br/>
<!--list可以通过${key[number]}和加强for循环获取-->

<c:forEach var="s" items="${set}">
${s} 
</c:forEach> 
<!--Set只能通过加强for循环获取-->

${maps['pipi']}
<c:forEach var="map" items="${maps}">
${map.key}___${map.value} 
</c:forEach> 
<br/> 
<!--map可以通过${key['key']}和加强for循环获取-->
      
</body>
</html>

要注意的是,在使用EL与JSTL时,除了引包外,还需要在jsp表头添加一句

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

这句话中只有"c"可以变动,可以随意命名为"sun"/"nr"/"num",但通用一般为c。

如果不用c,下面的<c: forEach></c:forEach>就要改为<你的命名: forEach></你的命名:forEach>

发布了30 篇原创文章 · 获赞 1 · 访问量 749

猜你喜欢

转载自blog.csdn.net/nairuozi/article/details/103370425
今日推荐