Jsp基础(二)

JSP转发动作标签:

携带的参数相当于在网址后面进行拼接
<jsp:forward page="/2.jsp">
    <jsp:param value="zhangsan" name="username"/>
    <jsp:param value="123" name="password"/>
jsp:forward>

pageContext域对象:

该域对象只在本页面有效,出了该页面获取不到。

域的查找顺序:

<%
    /* 一般系统方法中,需要一个int参数,一般会是常量 */
    /* 只要有pageContext就能向各个域中添加值,参数三表示对应域的常量 */
    /* 同名时,查询域中的值 */
    //pageContext.setAttribute("p", "pageContext", PageContext.PAGE_SCOPE);
    //pageContext.setAttribute("p", "request", PageContext.REQUEST_SCOPE);
    //pageContext.setAttribute("p", "session", PageContext.SESSION_SCOPE);
    pageContext.setAttribute("p", "application", PageContext.APPLICATION_SCOPE);
%>

<%=pageContext.findAttribute("p") %> 

查找结果依次:pageContext < request < session < application

EL表达式:

expression language: 表达式语言。
作用:简化jsp中java代码开发。
它不是一种开发语言,是jsp中获取数据的一种规范。

获取数据:


<%  
    User user = new User();
    user.setUsername("zhangsan");
    user.setPassword("123");
    Address address = new Address();
    address.setCity("shanghai");
    user.setAddress(address);
    request.setAttribute("u", user);
%>

${u.username}

${u.address.city}


${u["username"]}
${u['username']}

<%
    ArrayList<String> list = new ArrayList<>();
    list.add("aaaa");
    list.add("bbbb");
    list.add("cccc");
    request.setAttribute("list", list);

    HashMap<String, String> map = new HashMap<>();
    map.put("d", "dddd");
    map.put("e", "eeee");
    map.put("f", "ffff");
    request.setAttribute("map", map);
%>

${list[0]}
${map.d}
${map['d']}

判断空值:

<%
    String str1 = null;
    request.setAttribute("str1", str1);

    String str2 = "";
    request.setAttribute("str2", str2);

    ArrayList<String> list1 = new ArrayList<>();
    list1.add("aaaa");
    request.setAttribute("list1", list1);

    ArrayList<String> list2 = new ArrayList<>();
    request.setAttribute("list2", list2);

    ArrayList<String> list3 = null;
    request.setAttribute("list3", list3);
    /* 测试三目运算 */
    String sex = "nan";
    request.setAttribute("s", sex);
%>

${empty str1}
${empty str2}
${empty list1}
${empty list2}
${empty list3}

${empty list3 ? "我是前面的":"我是后面的"}
三目运算符应用:

<input type="radio" value="nan" name="sex" ${s.equals("nan") ? "checked='checked'":""} />男
radio"
value="nv" name="sex" ${s.equals("nv") ? "checked='checked'":""} />女

隐式对象:

pageContext
pageScope
requestScope
sessionScope
applicationScope

可以直接在el表达式中使用

<%
    pageContext.setAttribute("p", "pageContext", PageContext.PAGE_SCOPE);
    pageContext.setAttribute("p", "request", PageContext.REQUEST_SCOPE);
    pageContext.setAttribute("p", "session", PageContext.SESSION_SCOPE);
    pageContext.setAttribute("p", "application", PageContext.APPLICATION_SCOPE);
%>
${pageScope.p}
${requestScope.p}
${sessionScope.p}
${applicationScope.p}



<form action="${pageContext.request.contextPath}/2.jsp" method="post">
    用户名:<input type="text" name="username" />
    <br />
    密码:<input type="password" name="password" />
    <br />
    <input type="checkbox" name="hobby" value="唱歌" />唱歌
    <input type="checkbox" name="hobby" value="篮球" />篮球
    <br />
    <input type="submit" value="提交" />
form>

<%
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
%>


${param.username}<br />
${paramValues.hobby[0]}<br />

${header["User-Agent"]}<br />

${cookie.JSESSIONID.value}

JSTL:

JSTL(JavaServerPages Standard Tag Library)JSP标准标签库。
作用:使用JSTL实现JSP页面中逻辑处理。如判断、循环等。
使用条件:需要在JSP页面添加taglib指令 添加核心库。
通用标签: setoutremove

导入的包:

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

set、out、remove标签:


<c:set var="num" value="10" scope="session">c:set>

<c:out value="${num }">c:out><br />

<c:remove var="num" scope="session"/>
${num }

条件标签:


<c:set var="n" value="10">c:set>

<c:if test="${n<5}">
    aaaa
c:if>
<c:if test="${n>5}">
    bbbb
c:if>

<c:choose>
    <c:when test="${n==5}">5c:when>
    <c:when test="${n==10}">10c:when>
    <c:otherwise>
        xxxx
    c:otherwise>
c:choose>

迭带标签:

<%
    ArrayList<String> list = new ArrayList<>();
    list.add("aaa");
    list.add("bbb");
    list.add("ccc");
    list.add("ddd");
    request.setAttribute("list", list);
%>


<c:forEach items="list" var="l">
    ${l}<br />
c:forEach>

<c:forEach var="i" begin="0" end="10" step="1">
    ${i}<br />
c:forEach>

<c:forEach var="i" begin="0" end="${list.size()}" step="1">
    ${list[i]}<br />
c:forEach>    

你可能感兴趣的:(Java,JavaWeb,JSP)