How can I create dynamic variable names in JSTL or an alternative - sql

I need to use JSTL and ideally I think an Array would be better as I need to create a bunch of variables with a value using SQL data from data.rows but I need to get the variable name from the data using row.type. However I am finding this hard because you cannot define a session variable using "${row.type}" as it raises an exception. It must be a concrete variable name. I would have thought an array would be best but no idea how to do this from my SQL JSTL query:
<sql:query var="data">
SELECT type, balance FROM Account WHERE account_number IN (SELECT account_number FROM CustomerAccount WHERE customer_number = "${sessionScope.CustomerNumber}");
</sql:query>
This is the code:
<c:forEach var="row" items="${data.rows}">
<c:set var="${row.type}" value="${row.balance}" scope="session" />
</c:forEach>
Any help or tips would be brilliant! Thank you.

Try this,
<c:forEach var="row" items="${data.rows}" varStatus="status">
<c:set var="rowType${status.count}" value="${row.balance}" scope="session" />
</c:forEach>
so it will create like rowType1, rowType2 and etc and its corresponding value will be balance.
Here,
${status.count}
is like an index in for loop.
Or try this out,
<c:forEach var="row" items="${data.rows}">
<c:set var="rowType${row.type}" value="${row.balance}" scope="session" />
</c:forEach>

If you're using Spring WebMVC you can use their eval-tag to dynamically assign values:
<spring:eval expression="row.balance" var="rowType${status.count}" scope="session"/>
I'm not sure if session scope is really the right thing to do but that depends on your application, of course. See http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/spring.tld.html#spring.tld.eval for details about the eval tag.

Related

Getting sequence of associated Product - IBM

I want to get 'Rank' value from MASSOCCECE table, since past 2 days I have been searching a lot to get this value but failed I tried 'ProductDatBean', 'RelatedProductDataBean' but couldn't figure out how to get the value of 'rank'.
I have four products as cross-sell of a product and I want to sort them on the basis of 'rank'.
It would be a great help if anyone could tell me how to get this value besides using customized bean.
EDIT: For more clarity, adding my code
<wcbase:useBean id="g_mA" classname="com.ibm.commerce.catalog.beans.ProductDataBean" >
<c:set target="${g_mA}" property="productID" value="${catentry_ID}" />
</wcbase:useBean>
<c:set var="g_associatedProducts" value="${g_mA.productCrossSells}" scope="request"/>
<c:forEach var="associatedProducts" items="${g_associatedProducts}" varStatus="status">
${associatedProducts.rank}
</c:forEach>
Got the value, thanks everyone for responses.
I was doing the right thing (${associatedProducts.rank}) for getting this value but I was misguided a little , They were items that were associated and I was informed that they were Products, so getting blank while implementing this logic for Item. Used ItemBean now everything is working fine.

Accessing field of object given by name JSTL

How do I access field of object which name is given by attribute in jstl?
Let's say in normal case, when it's not given by attribute I'd do it like so:
<c:forEach var='varName' items='${realFormName.listFieldName}'>
...
However since in this case I have to access field of object given by name setup code looks more like this:
<c:set var="formName" value="realFormName" />
Now i want to iterate over listFieldItem of object given by formName variable. How do i do that? These, for example don't work:
<c:forEach var='varName' items='${formName.listFieldName}'>
<c:forEach var='varName' items='${${formName}".listFieldName"}'>
So is there a simple way to do it?
Ok, i solved it. For me this worked:
<c:set var="form" value="${sessionScope[formName]}"></c:set>
It got object represented by formName from session scope and i was able to use it normally.

Public Property as ObjectDataSource Select Parameter

I have an ODS, which is bound to an ADO.NET datatable. There is one select parameter. I would like to use a public property which I have declared in my code-behind as the select parameter, something like:
<SelectParameters>
<asp:ControlParameter ControlID='<%# EInfoProperty %>' Name="quote_header_id"
PropertyName="headerId" Type="Int32" />
</SelectParameters>
The above syntax doesn't work, and I've been unable to find anything here, on MSDN or on Google that might help here. Is there a way to do this, or am I stuck sticking the value in the Session, or something?
From everything I could find, this is not currently possible. For my purposes, I ended up putting the value in question in the session and using a SessionParameter.

jsp foreach on jdbc list<map> format

so im having a little problem with my code. I used jdbc to make a query to our SQL database in our DAO like so:
List<Map<String,Object>> results = namedjdbcTemplate.queryForList(FINDALL, namedParameters);
Which is then passed up from the service to the controller where it is added to our model:
List<Map<String,Object>> locations = cmsAttributeService.getAttributeList(id);
model.addAttribute("locationlist", locations);
I then need to display all the entries in our jsp. Currently i do so like this:
<c:forEach items="${locationlist}" var="list">
<tr>
<td>
<c:out value="${list }"></c:out>
</td>
</tr>
</c:forEach>
My problem is the formatting it returns, for example:
{=UCMDB2.Project.name}
How do i get it to return just 'UCMDB2.Project.name' dropping the {= }? Ive tried all sorts of other calls like using list.value, list.key, all of which either break it entirely or blank the results out. Im fairly sure it should be something simple that im just missing. Anyone got any clues?
Changed it to List <String> and it worked.

Coldfusion, default ORM object?

so I have a form that I'm using for new items, and to edit items.
The input fields therefore may have a value or not. I'm using this code in the value field.
<input name="uuid" value="<cfif isNull(item.GetUuid())>#item.GetUuid()#</cfif>"/>
Is this the best way to do it? I would have thought the ORM returning a blank object or something maybe cleaner, but not sure of a tider way to do it?
This might work
<cfproperty name="uuid" default="">