Accessing field of object given by name JSTL - struts

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.

Related

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

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.

How to display data in struts form if object has another object

I'm working on struts 1.3 framework application.
I have one object which I'm setting in request attribute.
request.setAttribute("school",school);
And I'm trying to display that object through <bean:define> tag.
E.g.
School is Value Object
School school;
in school VO object I have another two object
Student student;
Teacher teacher;
And I'm trying to display value of student as well as teacher object
<bean:define id="summary" name="school" />
<bean:define id="StudentSummary" name="summary" property="student"/>
<bean:define id="TeacherSummary" name="summary" property="teacher"/>
And writing this element through tag
<bean:write name="StudentSummary" property="name" />
<bean:write name="StudentSummary" property="class" />
<bean:write name="TeacherSummary" property="name" />
But it is giving
javax.servlet.jsp.JspException: Cannot find message resources under key org.apache.struts.action.MESSAGE
what would be wrong in the code.
I have never done it using bean tag but you can do it using Expression language(EL). EL, I believe is a more standard way to do things.
Take a look at this previous post. I think it helps Link
I think in your case you can do something along the line of
<c:out value="${school.student.name}"/>
The above statement will print the value of "name", if you have a "name" property in your student object.

Variable in an attribute in Struts custom tag

I am trying to use a variable inside a custom Struts tag something like follows -
for(String currentMacro : (List<String>)(request.getAttribute("individualMacros"))) {
name = currentMacro.<some-operation>
<html:mce name = "hmtl_<%= name %>" />
Something like this. But <%=name%> is not replaced with the variable value. It works when I am using the variable with a pure HTML tags.
Is there any any way to accomplish this in this case?
Thanks.
Use JSP EL (assuming JSP 2.0, and you put "name" into scope). You could also check to the if the TLD allows rtexprs.
<html:mce name="html_${name}"/>
But why use scriptlets? There's rarely (ever?) a good reason.
Since we are taking about a custom tag, my guess is that in the TLD file there isn't the rtexprvalue option set to true for that particular tag attribute:
<attribute>
<name>name</name>
<rtexprvalue>true</rtexprvalue>
.......
</attribute>
The rtexprvalue specifies that the attribute value may be dynamically evaluated at runtime.
If set to "false" it means that the attribute has a static value which is evaluated at translation; if set to "true" it means the value can be determined dynamically at runtime. Default is "false".
If the scriptlet does not work, it most likely means rtexprvalue is false. If you don't have the liberty to change that, then expressions won't work on that particular attribute.

struts form and JSTL

How to access a form variable using JSTL ?
e.g.
<html:text property="abc" ... />
<c:out value="${abc}"/>
abc is always blank, even though I have value set by action sending to this page.
If you refer with ActionForm, you can retrieve the value. "abc" is one of the property of ActionForm(you created) which will be available in request scope as it has been set in ActionServlet during processing request.
So get the value like this, suppose your ActionForm is 'TestForm', then retrieve as ${TestForm.abc} or <c:out value="${TestForm.abc}"/>

Struts 1.x tags usage question

I have a jsp page with the tags:
<logic:iterate id="var" ...
....
<bean:write name="var" property="p1" ...
etc.
And I need, on each iteration, to generate a href composed from the various bean's properties. I even need to URLEncode some of them so the link works.
Something like
<logic:iterate id="var" ...
....
<html:link action="otheraction.do?_X_
<bean:write name="var" property="p1" ...
etc
where X is generated by collecting the bean's properties; something like
String X="p1="+URLEncode(p1)+"&p2="+SimpleDateFormatof(p2)+"&p3="+p3;
How can I do that ?
Thanks in advance.
Better to make one POJO class.
1. Assign all your values to the object in Action which is being called before your jsp page comes in the picture.
2. Keep the object of POJO to request attribute.
3. Get the value from request attribtue on JSP using <bean:write> tag.