Showing posts with label jstl. Show all posts
Showing posts with label jstl. Show all posts

Thursday, November 15, 2012

Cheat sheet (quick tutorial) on JSTL

Insert tags

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

 _______

Set a JSTL variable from Java

<c:set scope="session" value="<%=propertyMap.get("debugsql")%>" var="debugsql"></c:set>

_______

Print value of all httprequest parameters:

<c:forEach var="par" items="${paramValues}">
    <c:out value="${par.key}=${par.value[0]}"></c:out>
</c:forEach>

_______

Read in Java a JSTL property with session scope

Object mydomainObj = session.getAttribute("mydomain");
String mydomain = (mydomainObj == null) ? "" : mydomainObj.toString();



_______

Check is a JSTL variable is null:

<c:if test="${not empty param.nesoadomain}">


_______

Run a SQL query:

<sql:query var="rsCMDBDS" dataSource="jdbc/cmdb">
select A.JDBCDRIVER, B.URL, B.USERNAME, B.ENCRYPTED_PASSWORD from DATASOURCES A, DATASOURCES_ENV B where A.DSNAME = B.DSNAME and A.DSNAME = 'wlsbjmsrpDataSource' and B.DOMAINNAME = '${mydomain}'
</sql:query>
_______

test for equal

<c:if test="${par.key eq 'synchronizeLocalTable'} ">

test for equal

<c:if test="${par.key ne 'synchronizeLocalTable'} ">
_______

increment a counter in a loop

<c:set var="count" value="0" scope="page" />
<c:set var="count" value="${count + 1}" scope="page"/>

_______



Sunday, May 27, 2012

Extracting a single resultset row from sql:query resultset

I know, JSTL SQL breaks the MVC pattern. Who cares. It's quick and dirty and it works.
I simply hate complicated setups of MVC products, gimme the result now with minimal effort and I will be grateful to you.

So, all the examples are about retrieving a ResultSet with a sql:query:

<sql:query var="rsInterfaceNames" dataSource="jdbc/soainfra_dev2">
select unique InterfaceID from WLI_QS_REPORT_VIEW order by InterfaceID DESC
</sql:query>


and then do a

<c:forEach var="rowIF" items="${rsInterfaceNames.rows}">
do something with ${rowIF.someAttribute}
</c:forEach>


This simply doesn't apply when you retrieve a ResultSet with a single row, suck as in SELECT COUNT(*) FROM SOMETABLE;

Here is the javadoc of the object returned into the resultset var:

http://tomcat.apache.org/taglibs/standard/apidocs/org/apache/taglibs/standard/tag/common/sql/ResultImpl.html

and here is how you can get the value of count(*):

${rsInterfaceCount.getRowsByIndex()[0][0]}


in case you have trouble with conversions, just use select TO_CHAR(count(*)) ...