EL&&JSTL使用

来源:互联网 发布:淘宝天猫商家数量 编辑:程序博客网 时间:2024/06/02 01:11

EL标签

1.新建el.jsp

<%@pageimport="java.util.ArrayList"%>

<%@pageimport="java.util.List"%>

<%@pageimport="java.util.HashMap"%>

<%@pageimport="java.util.Map"%>

<%@pageimport="p1123.Books"%>

<%@ page language="java"contentType="text/html;charset=UTF-8"

    pageEncoding="UTF-8"%>

<%

Stringpath = request.getContextPath();

StringbasePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

 

pageContext.setAttribute("ctx", basePath);

request.setAttribute("username","zhangsan_request");

session.setAttribute("username","zhangsan_session");

application.setAttribute("username","zhangsan_app");

 

Booksbooks = new Books();

books.setName("think in java");

pageContext.setAttribute("book", books);

 

Map names = newHashMap();

names.put("one","LiYang");

names.put("two","WangHua");

pageContext.setAttribute("names",names);

 

List ages = newArrayList();

ages.add(123);

ages.add(456);

pageContext.setAttribute("ages",ages);

 

%>   

<!DOCTYPEhtmlPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<basehref="<%=basePath%>">

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h3>从不同范围取值</h3>

${ctx} <br>

${username}<br>

${applicationScope.username}<br>

复合对象的属性提取<br>

${book.name}=${book['name'] }

<h3>Map操作</h3>

${names['one']}

<h3>List/数组操作</h3>

${ages[1]}

 

<h3>el的范围前缀可以省略,可以是四个范围,还可以是:param/params</h3>

${param.name} = request.getParameter("name");

</body>

</html>

2.访问地址

http://localhost:8085/Practise/p1123/el.jsp?name=12

3.效果


JSTL标签

1.新建jstl.jsp

<%@ page language="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

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

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>   

<!DOCTYPEhtmlPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<basehref="<%=basePath%>">

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

${param.age }

<%

int age = Integer.parseInt(request.getParameter("age"));

if(age>40)

    out.print("中年");

else

    out.print("青年");

%>

 

<h3>JSTL配合EL表达式将大大简化代码</h3>

<c:iftest="${param.age>50 }">

    中年

</c:if>

<c:iftest="${param.age<50 }">

    青年

</c:if>

 

<h3>通用标签</h3>

<c:setvar="example"value="${100+1}"scope="session" />  

 

<c:outvalue="${example}"default="1000"/>==${example}

 

<c:setvar="example"value="《》&&&<>" scope="session" /> 

<c:outvalue="${example}"escapeXml="true"/>

 

<c:removevar="example"scope="session"/>

 

<h3>迭代标签</h3>

<%

String[] strArr = new String[]{"a","b","c","d"} ;

request.setAttribute("list", strArr);

%>

<tableborder="1">

    <trstyle="">

       <td>内容1</td>

       <td>操作</td>

    </tr>

    <c:forEachitems="${list }"var="item"varStatus="status">

    <tr

<c:iftest="${status.index%2==0}">style="background-color:red;"</c:if>>

       <td>${item }</td>

       <td>删除</td>

    </tr>

    </c:forEach>

</table>

</body>

</html>

2.访问地址

http://localhost:8085/Practise/p1123/jstl.jsp?age=12

3.效果


0 0
原创粉丝点击