jsp 按ID分组

按ID获取组件

componentByID--Taglib.jsp
<%@ taglib prefix="cms" uri="http://hybris.com/tld/cmstags"%>
componentByID--Tag.jsp
<cms:component uid="IDofTheComponent" />

jsp 如果用户有任何角色

hasAnyRole.jsp
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>

<sec:authorize access="hasAnyRole('ROLE_ANONYMOUS')">
  // TODO
</sec:authorize>

jsp 请求范围

从页面范围中获取所有变量

requestScope.jsp
<c:forEach items="${requestScope}" var="par">
  <li>${par.key} - ${par.value}</li>
</c:forEach>

jsp 对话

dialog.js
var $dialog = $('.js-dialog-class'),
    $dialogLink = $('.js-dialog-link');
    
$dialog
  .on('show.bs.modal', function() {
    // do something
  })
  .on('hide.bs.modal', function() {
      // do something
  });

$dialogLink.on('click' ,function(e){
  $dialog.modal('show');
  // $dialog.modal('hide');
});
dialog.html
<a class="btn btn-primary js-dialog-link" href="#" data-toggle="modal" data-target="js-dialog-class">Link</a>
dialog.jsp
<%@ taglib prefix="component" tagdir="/WEB-INF/tags/shared/component" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<spring:theme code="title" var="dialogHeader" />

<c:set var="dialogContent">
	Dialog Content
</c:set>

<component:dialog
  dialogID="dialogID"
  dialogClass="fade dialog-class js-dialog-class"
  dialogCloseTitle="Exit"
  dialogHeader="${dialogHeader}"
  dialogContent="${dialogContent}"/>

jsp 查找并替换字符串中的内容

findAndReplace.jsp
<c:set var="str" value="${fn:indexOf(str, ' ') > -1 ? fn:replace(str, ' ', '') : str}"/>

jsp <S:escapeBody>标签

<S:escapeBody>标签是一个通用的转义标签,可以进行HTML或者JavaScript的标签

转义标签.jsp

jsp 获取URL中参数的值

获取URL中参数的值.jsp
例如跳转到页面时的URL为: /login?error=2
那么在页面中这样获取error参数的值: ${param.error}

jsp 在JSP页面中列出会议中所有的键值对

在JSP页面中列出session中所有的键值对.jsp
<%@ page import="java.util.Enumeration" %>


<%
    Enumeration keys = session.getAttributeNames();
    while (keys.hasMoreElements())
    {
        String key = (String)keys.nextElement();
        out.println(key + ": " + session.getAttribute(key) + "<br>");
    }
%>

jsp ADF索赔商店页面;来自https://github.com/TridionPractice/tridion-practice/blob/master/AmbientDataFramework/ClaimStore.jsp

ADF索赔商店页面;来自https://github.com/TridionPractice/tridion-practice/blob/master/AmbientDataFramework/ClaimStore.jsp

ClaimStore.jsp
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"
         import="com.tridion.ambientdata.*, 
		 com.tridion.ambientdata.claimstore.*, 
		 java.net.*, 
		 java.util.*, 
		 java.util.Map.Entry,
		 net.minidev.json.JSONObject"%>
<%!
		public String serializeClaimStore() {
			ClaimStore store = AmbientDataContext.getCurrentClaimStore();
			if (store == null) {
				return "{}";
			}
			
			TreeMap<String, Object> claims = new TreeMap<String, Object>();
			Map<URI, Object> allClaims = store.getAll();
			for (URI claimUri : allClaims.keySet()) {
				claims.put(claimUri.toString(), allClaims.get(claimUri));
			}
			
			JSONObject jsonObj = new JSONObject(claims);
			return jsonObj.toJSONString();
		}
%>
<%	
	if ("json".equals(request.getParameter("format"))) {
		out.println(serializeClaimStore());
		out.flush();
		out.close();
		return;
	}
%>
<html>
    <head>
        <title>Ambient Data Claim Store</title>

        <link rel="stylesheet" type="text/css" href="http://tridionpractice.github.io/tridion-practice/stylesheets/AmbientDataFramework/ClaimStore.css"></link>
		
		<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script>
		<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-3.3.0.js"></script>
		<script src="http://tridionpractice.github.io/tridion-practice/javascripts/AmbientDataFramework/Knockout-LocalStorage.js"></script>
		<script src="http://tridionpractice.github.io/tridion-practice/javascripts/AmbientDataFramework/ClaimModel.js"></script>
		<script src="http://tridionpractice.github.io/tridion-practice/javascripts/AmbientDataFramework/ClaimsViewModel.js"></script>
    </head>
    <body>
		<div class='section'>
			<div class="topBar">
				Filter: <input type="text" placeholder="Enter search term" data-bind="textInput: filter" />
			</div>
			<div class="refreshArea">
				<span data-bind="visible: loading">Loading...</span>
				<button class="refreshButton" data-bind="click: refresh, visible:!autoRefresh()">Refresh</button>
				<input id="chkAutoRefresh" type="checkbox" data-bind="checked: autoRefresh"><label for="chkAutoRefresh">Refresh automatically</label></input>
			</div>
			<table>
				<thead>
					<th>Name</th>
					<th>Value</th>
				</thead>
				<tbody data-bind="foreach: claims">
					<tr>
						<td data-bind="text: key"></td>
						<!-- ko if: (nestedValues.length == 0) -->
						<td data-bind="text: value"></td>
						<!-- /ko -->							
						<!-- ko if: (nestedValues.length > 0) -->
						<td>
							<ul data-bind="foreach: { data: nestedValues, as: 'entry'}">
								<li>
									<span data-bind="text: entry.key"></span><!-- ko if: entry.value -->: <span data-bind="text: entry.value"></span><!-- /ko -->
								</li>
							</ul>
						</td>
						<!-- /ko -->
					</tr>
				</tbody>
			</table>
		</div>
    </body>
</html>

jsp 分页JSP

pagination-loop.jsp
<%-- page range --%>
<c:choose>
	<%-- List all page links if total number of pages for the category is 4 or below --%>
	<c:when test="${pagesAvailable <= 4}">
		<c:forEach begin='1' end='${pagesAvailable}' varStatus="loop">
			<li class="page-link" role="presentation">
				<a href="${paginationLink}?pgN=${loop.index}" class="ajax-pagination-action ${currentPage eq loop.index? 'inactive' : ''} paginationLink ${bottomPagination ? 'forceTop' : ''} " role="menuitem">${loop.index}</a>
			</li>
		</c:forEach>
	</c:when>
	<c:otherwise>
					
		<c:choose>
			<%-- If user is on page 1 or 2 and total Num of pages is more than 4--%>
			<c:when test="${currentPage < 3}">
				<c:forEach begin='1' end='3' varStatus="loop">
					<li class="page-link" role="presentation">
						<a href="${paginationLink}?pgN=${loop.index}" class="ajax-pagination-action ${currentPage eq loop.index ? 'inactive' : ''} paginationLink ${bottomPagination ? 'forceTop' : ''} " role="menuitem">${loop.index}</a>
					</li>
				</c:forEach>
				<li class="page-more after" role="presentation"> ... </li>
				<li class="page-link" role="presentation">
					<a href="${paginationLink}?pgN=${pagesAvailable}" class="ajax-pagination-action paginationLink ${bottomPagination ? 'forceTop' : ''} " role="menuitem" tabindex="-1">${pagesAvailable}</a> 
				</li>
			</c:when>
			<%-- Display "..." between (N+1) and (L) only if the difference between them is more than 1 --%>
			<c:when test="${currentPage >= (pagesAvailable - 2)}">
				<li class="page-link" role="presentation">
					<a href="${paginationLink}?pgN=1" class="ajax-pagination-action paginationLink ${bottomPagination ? 'forceTop' : ''} " role="menuitem">1</a>
				</li>
				<li class="page-more before" role="presentation"> ... </li>
				<c:forEach begin='${pagesAvailable - 2}' end='${pagesAvailable}' varStatus="loop">
					<li class="page-link" role="presentation">
						<a href="${paginationLink}?pgN=${loop.index}" class="ajax-pagination-action ${currentPage eq loop.index? 'inactive' : ''} paginationLink ${bottomPagination ? 'forceTop' : ''} " role="menuitem">${loop.index}</a>
					</li>
				</c:forEach>
			</c:when>
			<c:otherwise>
				<li class="page-link" role="presentation">
					<a href="${paginationLink}?pgN=1" class="ajax-pagination-action paginationLink ${bottomPagination ? 'forceTop' : ''} " role="menuitem" tabindex="-1">1</a>
				</li>
				<li class="page-more before" role="presentation"> ... </li>
				<c:forEach begin='${currentPage}' end='${currentPage + 1}' varStatus="loop">
					<li class="page-link" role="presentation">
						<a href="${paginationLink}?pgN=${loop.index}" class="ajax-pagination-action ${currentPage eq loop.index ? 'inactive' : ''} paginationLink ${bottomPagination ? 'forceTop' : ''} " role="menuitem">${loop.index}</a>
					</li>
				</c:forEach>
				<li class="page-more after" role="presentation"> ... </li>
				<li class="page-link" role="presentation">
					<a href="${paginationLink}?pgN=${pagesAvailable}" class="ajax-pagination-action paginationLink ${bottomPagination ? 'forceTop' : ''} " role="menuitem">${pagesAvailable}</a>
				</li>
			</c:otherwise>
		</c:choose>
	</c:otherwise>
</c:choose>