JSP/JSTL possibility

From GruppoZenit
Jump to: navigation, search

In Z-CMS it is possible to use JSTL and JSP scripting supports.


The communication between Z-CMS and JSTL/JSP can halp in achieving many useful dynamis supports. Some examples for that are shown in the following parts.


Contents

Example 1: Scriptlet inside a webpage using JSP

In this example, communication between Z-CMS and JSP is possible via "pageContext".


Create a webpage "anyname.xhtml" with below shown content and then click preview or publish:

<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>
<zcms:store var="strvar1" value="test" />
<zcms:store var="strvar2" value="test" />
 
<zcms:escape>
<%
  out.println("In scriptlet strvar1 -> "+strvar1+"<br />");
  out.println("In scriptlet strvar2 -> "+strvar2+"<br />");
  String result = (String)strvar1;
  result = result.concat(" ").concat((String)strvar2);
  out.println("In scriptlet using in a variable -> "+result+"<br />");   
  pageContext.setAttribute("result", result);
%>
</zcms:escape>
                    
<br />
In zcms taglib strvar1 ->  <zcms:use name="strvar1" /><br />
In zcms taglib strvar2 ->  <zcms:use name="strvar2" /><br />
In zcms taglib result ->  <zcms:use name="result" />
</body>
<zcms:pageend />


Example 1: Scriptlet inside a webpage using JSTL

In this example, communication between Z-CMS and JSTL is possible via "pageContext".

Create a webpage "anyname.xhtml" with below shown content and then click preview or publish:

 
<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>
<zcms:store var="strvar1" value="test" />
<zcms:store var="strvar2" value="tested" />
                    
<zcms:escape>
In scriptlet strvar1 -> <c:out value="${strvar1}" /><br />
In scriptlet strvar2 -> <c:out value="${strvar2}" /><br />
<c:set var="result" value='${strvar1} ${strvar2}' />
In scriptlet using in a variable -> <c:out value="${result}" /><br />
</zcms:escape>
                    
<br />
In zcms taglib strvar1 ->  <zcms:use name="strvar1" /><br />
In zcms taglib strvar2 ->  <zcms:use name="strvar2" /><br />
In zcms taglib result ->  <zcms:use name="result" />
</body>
<zcms:pageend />


Example 2: Scriptlet in a Resource file included inside a webpage using JSP

In this example, communication between Z-CMS and JSP is possible via "pageContext".


Create a JSP file "test.jsp" in website Resources section or create using another editor and import the "test.jsp" file into website Resources.


The content of "test.jsp" file is reported below:

<%
 out.println("In scriptlet strvar1 -> "+strvar1+"<br />");
 out.println("In scriptlet strvar2 -> "+strvar2+"<br />");
 String result = (String)strvar1;
 result = result.concat(" ").concat((String)strvar2);
 out.println("In scriptlet using in a variable -> "+result+"<br />");   
 pageContext.setAttribute("result", result);
%>

Then, create a webpage "anyname.xhtml" with content shown below and click [[Resources#Resources buttons|preview]] or [[Resources#Resources buttons|publish]]:
 
<pre style="white-space: pre-wrap; 
white-space: -moz-pre-wrap; 
white-space: -pre-wrap; 
white-space: -o-pre-wrap; 
word-wrap: break-word;">
<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>
 
<zcms:store var="strvar1" value="test" />
<zcms:store var="strvar2" value="test" />
                    
<zcms:escape>
<%@include file="test.jsp" %>
</zcms:escape>
                    
<br />
In zcms taglib strvar1 ->  <zcms:use name="strvar1" /><br />
In zcms taglib strvar2 ->  <zcms:use name="strvar2" /><br />
In zcms taglib result ->  <zcms:use name="result" />
 
</body>
<zcms:pageend />


Example 2: Scriptlet in a Resource file included inside a webpage using JSTL

In this example, communication between Z-CMS and JSTL is possible via "pageContext".


Create a JSP file "test.jsp" in website Resources section or create using another editor and import the "test.jsp" file into website Resources.


The content of "test.jsp" file is reported below:

In scriptlet strvar1 -> <c:out value="${strvar1}" /><br />
In scriptlet strvar2 -> <c:out value="${strvar2}" /><br />
<c:set var="result" value='${strvar1} ${strvar2}' />
In scriptlet using in a variable -> <c:out value="${result}" /><br />
 
Then, create a webpage "anyname.xhtml" with content shown below and click [[Resources#Resources buttons|preview]] or [[Resources#Resources buttons|publish]]:
 
<zcms:pagestart />
<head>
 <zcms:pagehead />
 <title> Title goes here </title>
</head>
<body>

<zcms:store var="strvar1" value="tested" />
<zcms:store var="strvar2" value="test" />

<zcms:escape>
<%@include file="test.jsp" %>
</zcms:escape>

<br />
In zcms taglib strvar1 ->  <zcms:use name="strvar1" /><br />
In zcms taglib strvar2 ->  <zcms:use name="strvar2" /><br />
In zcms taglib result ->  <zcms:use name="result" />

</body>
<zcms:pageend />


Example 3: Using a scriptlet Resource file inside a webpage with JSP "include" tag with possibility of static parameter passing using JSP

In this example, "pageContext" of webpage and included JSP are different from previous ones so communication via "pageContext" is not possible.


Create a JSP file named "test.jsp" in website Resources section or create it using another editor and import the file into website Resources.


Content of "test.jsp" file is shown below:

<%
 String p1 = request.getParameter("p1")==null?"null came":request.getParameter("p1");
 String p2 = request.getParameter("p2")==null?"null came":request.getParameter("p2");
 out.println("In scriptlet p1 -> "+p1+"<br />");
 out.println("In scriptlet p2 -> "+p2+"<br />");
%>

Then, create a webpage "anyname.xhtml" with content shown below and click preview or publish:

<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>

<zcms:escape>
<jsp:include page="test.jsp"/>
</zcms:escape>

</body>
<zcms:pageend />

After clicking Preview or Publish it is possible to visualize the output as:

  • In scriptlet p1 -> null came
  • In scriptlet p2 -> null came

Provide "?p1=a&p2=b" at the end of the URL and see:

  • In scriptlet p1 -> a
  • In scriptlet p2 -> b

Modify the webpage "anyname.xhtml" with below reported content and then select Preview or Publish:

<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>

<zcms:escape>
<jsp:include page="test.jsp?p1=abc&p2=pqr" />
</zcms:escape>

</body>
<zcms:pageend />

After clicking Preview or Publish you can see output as:

  • In scriptlet p1 -> abc
  • In scriptlet p2 -> pqr

Modify the webpage "anyname.xhtml" with below shown content and then click Preview or Publish:

<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>

<zcms:escape>
<jsp:include page="test.jsp">
<jsp:param name="p1" value="abc" />
<jsp:param name="p2" value="pqr" />
</jsp:include>
</zcms:escape>

</body>
<zcms:pageend />

After clicking Preview or Publish you can see output as:

  • In scriptlet p1 -> abc
  • In scriptlet p2 -> pqr.


Example 3: Using a scriptlet Resource file inside a webpage with JSP "include" tag with possibility of static parameter passing using JSTL

In this example, "pageContext" of webpage and included JSP are different from previous ones so communication via "pageContext" is not possible.


Create a JSP file named "test.jsp" in website Resources section or create it using another editor and import the file into website Resources.


Content of "test.jsp" file is shown below:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
In scriptlet p1 -> <c:out value="${param.p1}" /><br />
In scriptlet p2 -> <c:out value="${param.p2}" /><br />

Then, create a webpage "anyname.xhtml" with content shown below and click preview or publish:

<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>
      
<zcms:escape>
<jsp:include page="test.jsp"/>
</zcms:escape>

</body>
<zcms:pageend />

After clicking Preview or Publish you can see output as:

  • In scriptlet p1 ->
  • In scriptlet p2 ->

Provide "?p1=a&p2=b" at the end of the URL and see:

  • In scriptlet p1 -> a
  • In scriptlet p2 -> b

Modify the webpage "anyname.xhtml" with below content and then select Preview or Publish:

<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>
         
<zcms:escape>
<jsp:include page="test.jsp?p1=abc&p2=pqr" />
</zcms:escape>

</body>
<zcms:pageend />

After clicking Preview or Publish it is possible to see the following output:

  • In scriptlet p1 -> abc
  • In scriptlet p2 -> pqr

Modify the webpage "anyname.xhtml" with below reported content and then click Preview or Publish:

<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>

<zcms:escape>
<jsp:include page="test.jsp">
<jsp:param name="p1" value="abc" />
<jsp:param name="p2" value="pqr" />
</jsp:include>
</zcms:escape>

</body>
<zcms:pageend />

After clicking preivew or publish you can see output as:

  • In scriptlet p1 -> abc
  • In scriptlet p2 -> pqr.


Example 4: Using a scriptlet Resource file inside a webpage with JSP "include" tag with possibility of dynamic parameter passing using JSP

In this example, "pageContext" of webpage and included JSP are different so communication via "pageContext" is not possible.


Create a JSP file "test.jsp" in website Resources section or create it using another editor and import it into the website Resources.


The "test.jsp" file content is reported below:

<%
 String p1 = request.getParameter("p1")==null?"null":request.getParameter("p1");
 String p2 = request.getParameter("p2")==null?"null":request.getParameter("p2");
 String p3 = request.getParameter("p3")==null?"null":request.getParameter("p3");
 out.println("In scriptlet p1 -> "+p1+"<br />");
 out.println("In scriptlet p2 -> "+p2+"<br />");
 out.println("In scriptlet p3 -> "+p3+"<br />");
%>

Then, create the webpage "anyname.xhtml" with below shown content and then click Preview or Publish:

<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>
<zcms:store var="testvar" value="testvalue" />
<zcms:escape>
<jsp:include page="test.jsp">
<jsp:param name="p1" value="<%=request.getParameter("p1")%>" />
<jsp:param name="p2" value="<%=session.getAttribute("site")%>" />
<jsp:param name="p3" value="<%=(String)pageContext.getAttribute("testvar")%>" />
</jsp:include>
</zcms:escape>

</body>
<zcms:pageend />


After clicking Preivew or Publish it is possible to see the output as:

  • In scriptlet p1 -> null
  • In scriptlet p2 -> 102
  • In scriptlet p3 -> testvalue

Provide "?p1=8888" at the end of the URL and see:

  • In scriptlet p1 -> 8888
  • In scriptlet p2 -> 102
  • In scriptlet p3 -> testvalue.


Example 4: Using a scriptlet Resource file inside a webpage with JSP "include" tag with possibility of dynamic parameter passing using JSTL

In this example, "pageContext" of webpage and included JSP are different so communication via "pageContext" is not possible.


Create a JSP file "test.jsp" in website Resources section or create it using another editor and import it into the website Resources.


The "test.jsp" file content is reported below:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
In scriptlet p1 -> <c:out value="${param.p1}" /><br />
In scriptlet p2 -> <c:out value="${param.p2}" /><br />
In scriptlet p3 -> <c:out value="${param.p3}" /><br />

Then, create the webpage "anyname.xhtml" with below shown content and then click Preview or Publish:

<zcms:pagestart />
<head>
<zcms:pagehead />
<title> Title goes here </title>
</head>
<body>
<zcms:store var="testvar" value="testvalue" />
<zcms:escape>
<jsp:include page="test.jsp">
<jsp:param name="p1" value="<%=request.getParameter("p1")%>" />
<jsp:param name="p2" value="<%=session.getAttribute("site")%>" />
<jsp:param name="p3" value="<%=(String)pageContext.getAttribute("testvar")%>" />
</zcms:escape>

</body>
<zcms:pageend />

After clicking Preivew or Publish it is possible to view output as:

  • In scriptlet p1 -> null
  • In scriptlet p2 -> 102
  • In scriptlet p3 -> testvalue

Provide "?p1=8888" at the end of the URL and see:

  • In scriptlet p1 -> 8888
  • In scriptlet p2 -> 102
  • In scriptlet p3 -> testvalue.


See also

Personal tools
Namespaces

Variants
Actions
Toolbox