javascript
JSP标签和JSTL标签注意点
1、轉發和重定向問題
當前項目:/Test
轉發路徑:"/"根目錄表示當前項目"/Test","/login.jsp"就是"/Test/login.jsp"。所以只能在web程序內部跳轉
重定向:"/"根目錄代表當前web容器,"/login.jsp"就是"/login.jsp",不符合要求,需要使用"/Test/login.jsp"。所以可以實現外部程序跳轉
轉發:request.getRequestDispacther("/login.jsp").forward(req, res); 重定向:response.sendRedirect(request.getContextPath() + "/login.jsp");2、servlet的response.sentRedirect(String url)和jstl標簽的<c:redirect url="">[<jsp:param />......]</c:redirect>比較
response的重定向方法"/"是相對于web容器來說的,
但是jstl的c標簽的重定向是相當于c標簽的context屬性來說的,而c標簽的context屬性默認值就是${pageContext.request.contextPath }。
<%-- <%if (request.getAttribute("username") == null) {response.sendRedirect(request.getContextPath() + "/login.jsp");}%> --%><c:if test="${empty username }"><c:redirect url="/login.jsp"></c:redirect></c:if> 等價于: <c:redirect context="${pageContext.request.contextPath }" url="/login.jsp"></c:redirect>3、jsp中獲取param請求參數
servlet中:request.getParameter(String pname) String
jsp中:使用jstl隱式對象(看我前面的博客中jstl11個隱式對象)
<%request.setCharacterEncoding("UTF-8"); //設置編碼,防止中文亂碼%>請求參數:${param.username } --%>
?
附錄:jsp和jstl標簽詳解.
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>EL語法</title> </head><body><%String name = "rose"; //放入域中//pageContext.setAttribute("name",name);pageContext.setAttribute("name",name,PageContext.REQUEST_SCOPE); %><%=name %><br/><%--1)從四個域自動搜索--%>EL表達式: ${name }<%--${name } 等價于<%=pageContext.findAttribute("name")%>--%><%--2) 從指定的域中獲取數據--%>EL表達式: ${pageScope.name }<%--${pageScope.name } 等價于<%= pageContext.getAttribute("name",PageContext.PAGE_SCOPE)%>--%></body> </html> <%@ page language="java" import="java.util.*,gz.itcast.b_entity.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>EL輸出不同類型的數據</title> </head><body><%--1)EL輸出對象的屬性 --%><%//保存數據Student student = new Student("eric",20);//放入域中pageContext.setAttribute("student",student);//ListList<Student> list = new ArrayList<Student>();list.add(new Student("rose",18));list.add(new Student("jack",28));list.add(new Student("lucy",38));//放入域中pageContext.setAttribute("list",list);//MapMap<String,Student> map = new HashMap<String,Student>();map.put("100",new Student("mark",20));map.put("101",new Student("maxwell",30));map.put("102",new Student("narci",40));//放入域中pageContext.setAttribute("map",map);%><%--使用EL獲取對象 --%>${student.name} - ${student.age }<%--${student.name} 等價于 (點相對于調用getXX()方法)<%=((Student)pageContext.findAttribute("student")).getName()%>--%><hr/><%--使用EL獲取List對象 --%>${list[0].name } - ${list[0].age }<br/>${list[1].name } - ${list[1].age }<br/>${list[2].name } - ${list[2].age }<%--list[0]等價于 (中括號相對于調用get(參數)方法)((List)pageContext.findAttribute("list")).get(0)--%><hr/><%--使用EL獲取Map對象 --%>${map['100'].name } - ${map['100'].age }<br/>${map['101'].name } - ${map['101'].age }<br/>${map['102'].name } - ${map['102'].age }<br/></body> </html> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>EL表達式計算</title> </head><body><%--1)算術表達式+ - * /--%>${10+5 }<br/>${10*5 }<hr/><%--2)比較運算> < >= <= == !=--%>${10>5 }<br/>${10<5 }<br/>${10!=10 }<hr/><%--3)邏輯運算&& || !--%>${true && false }<br/>${true || false }<br/>${!false }<br/><hr/><%--4)判空null 或 空字符串: empty--%><%//String name = "eric";//String name = null;String name = "";pageContext.setAttribute("name",name);%>判斷null: ${name==null }<br/>判斷空字符: ${name=="" }<br/>判空: ${name==null || name=="" }另一種判空寫法: ${empty name }</body> </html> <%@ page language="java" import="java.util.*,gz.itcast.b_entity.*" pageEncoding="utf-8"%> <%--導入標簽庫 --%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>核心標簽庫</title> </head><body><%--使用標簽 --%><%--set標簽 :保存數據(保存到域中)默認保存到page域 --%><c:set var="name" value="rose" scope="request"></c:set><%String msg = null;pageContext.setAttribute("msg",msg);%>${msg }<br/><%--out標簽: 獲取數據(從域中) default: 當value值為null時,使用默認值escapeXml: 是否對value值進行轉義,false,不轉義,true,轉義(默認)--%><c:out value="${msg}" default="<h3>標題3</h3>" escapeXml="true"></c:out><hr/><%--if標簽 :單條件判斷--%><c:if test="${!empty msg}">條件成立</c:if><hr/><%--choose標簽+when標簽+otherwirse標簽: 多條件判斷 --%><c:set var="score" value="56"></c:set><c:choose><c:when test="${score>=90 && score<=100}">優秀</c:when><c:when test="${score>=80 && score<90}">良好</c:when><c:when test="${score>=70 && score<80}">一般</c:when><c:when test="${score>=60 && score<70}">及格</c:when><c:otherwise>不及格</c:otherwise></c:choose><%-- forEach標簽:循環 --%><%//ListList<Student> list = new ArrayList<Student>();list.add(new Student("rose",18));list.add(new Student("jack",28));list.add(new Student("lucy",38));//放入域中pageContext.setAttribute("list",list);//MapMap<String,Student> map = new HashMap<String,Student>();map.put("100",new Student("mark",20));map.put("101",new Student("maxwell",30));map.put("102",new Student("narci",40));//放入域中pageContext.setAttribute("map",map);%><hr/><%--begin="" : 從哪個元素開始遍歷,從0開始.默認從0開始end="": 到哪個元素結束。默認到最后一個元素step="" : 步長 (每次加幾) ,默認1items="": 需要遍歷的數據(集合) var="": 每個元素的名稱 varStatus="": 當前正在遍歷元素的狀態對象。(count屬性:當前位置,從1開始)--%><c:forEach items="${list}" var="student" varStatus="varSta">序號:${varSta.count} - 姓名:${student.name } - 年齡:${student.age}<br/></c:forEach><hr/><c:forEach items="${map}" var="entry">${entry.key } - 姓名: ${entry.value.name } - 年齡:${entry.value.age }<br/></c:forEach><hr/><%-- forToken標簽: 循環特殊字符串 --%><%String str = "java-php-net-平面";pageContext.setAttribute("str",str);%><c:forTokens items="${str}" delims="-" var="s">${s }<br/></c:forTokens><%--redrict:重定向 --%><c:redirect url="http://www.baidu.com"></c:redirect></body> </html>jsp:include
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>頭部頁面</title> </head><body>通用的頭部頁面的內容<br/>參數: <%=request.getParameter("name") %></body> </html><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>動作標簽</title> </head><body><%--轉發 --%><%//request.getRequestDispatcher("/09.action2.jsp?name=eric").forward(request,response);%><%-- 參數 --%><%--<jsp:forward page="/09.action2.jsp"><jsp:param value="jacky" name="name"/><jsp:param value="123456" name="password"/></jsp:forward>--%><%--包含 --%><%--<jsp:include page="/common/header.jsp"><jsp:param value="lucy" name="name"/></jsp:include>--%><%@include file="common/header.jsp" %>主頁的內容</body> </html>?
轉載于:https://www.cnblogs.com/webyyq/p/7667749.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的JSP标签和JSTL标签注意点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个老程序猿的焦虑
- 下一篇: Python3 property属性