4

I have been looking how to do this properly but cannot find a definitive guide on how to do with.

I know that you cannot use an expression within the tag, but I am unsure how else I am meant to approach it.

I've seen a multitude of answers for this without much explanation or help.

Essentially I want something like the following to work, but obviously doesn't.

<c:forEach items="${dataposition.rows}" var="lineposition" begin="0" varStatus="status">
    <c:set var="name_${status.count}" value="${lineposition.value}" scope="session"/>
</c:forEach>

The exact error message is

"According to TLD or attribute durective in tag file, attribute var does not accept any expressions"

What is the proper way to accomplish this?

(I changed the variable names from my actual code, but hopefully you still get the idea)

If I need to create java objects or something I am fine with that, but I would need to know how to include them in my project and how to use them within the code. Something like a list sounds about right.

I have created an object to hold my values for me.

<jsp:useBean id="myid" class="myclass" scope="session"/>

and I want to use it, but am unsure how:

<c:forEach items="${dataposition.rows}" var="lineposition" begin="0" varStatus="status">
    <%
        myid.add_position(lineposition.var1, lineposition.var2, lineposition.var3, lineposition.var4, lineposition.var5);
    %>
</c:forEach>
TheOneWhoPrograms
  • 629
  • 1
  • 8
  • 19
  • I don't understand why you need to set a variable with key `name_1`, `name_2` etc. since you are iteratting over values you need. Why not just do what you want to do in this `forEach`? – lpratlong Jun 06 '14 at 12:36
  • I am on a jsp page that sets session variables to be used within a few other pages. This jsp page gets included from another one to get data from a database. If I have my thinking all wrong, can you suggest me which way to adjust ;) – TheOneWhoPrograms Jun 06 '14 at 12:38
  • 1
    Why is work like that being done at the JSP level at *all*? – Dave Newton Jun 06 '14 at 13:38
  • I was thrown onto this project and am not quite sure about all of this, I am still a junior in the business and its my first time touching web stuff. How do you suggest I get data from a db and store it as objects on the session level? I have java, html and jsp at my disposal. I am very open to suggestions – TheOneWhoPrograms Jun 06 '14 at 13:48

1 Answers1

4

Using an MVC framework to separate the code to setup your data from how you want to present it would be the ideal way to go. If you have a controller of any kind that executes on the server before your JSP is rendered then I would recommend putting your logic there. Besides the controller being the more appropriate place to prepare this sort of data, the syntax will almost definitely look more clean than anything you could put on the JSP to make this work.

If you don't have a controller and you're only using JSPs then I guess I would recommend writing a tag to replace <c:set>. This is not the cleanest approach but if JSPs are what you're stuck with then it seems like a decent compromise to me (I think it's better than scriptlets and hacking the JSTL core TLD to allow expressions in the "var" attribute anyway). It would do all of the same things as <c:set> except the TLD could be written to allow expressions in the "var" attribute.

MyTag.java

package example;

import org.apache.taglibs.standard.tag.rt.core.SetTag;

public class MySetTag extends SetTag { }

WEB-INF/my.tld

<taglib>
    <tag>
        <name>set</name>
        <tag-class>example.MySetTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>var</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        ...
    </tag>
</taglib>

rownamer.jsp

<%@ taglib prefix="my" uri="WEB-INF/my.tld"%>
<c:forEach items="${dataposition.rows}" var="lineposition" begin="0" varStatus="status">
    <my:set var="name_${status.count}" value="${lineposition.value}" scope="session"/>
</c:forEach>
Erik Gillespie
  • 3,929
  • 2
  • 31
  • 48
  • Would this overwrite the tag for other people using it? Or is this something seperate that I can use myself and other people don't have to use?...btw thank you for an amazingly detailed answer, I truly do appreciate it. (Sadly I am stuck with JSP) – TheOneWhoPrograms Jun 06 '14 at 15:50
  • This tag would be something separate and new that you can use in the project but others don't have to. It also leaves the original <c:set> tag intact so you can continue to use it any time you don't need to put an expression in the "var" attribute. – Erik Gillespie Jun 06 '14 at 15:57
  • I will accept it once I try it on Tuesday (holiday on monday here in Germany). Thanks for all the help! – TheOneWhoPrograms Jun 06 '14 at 16:50
  • 1
    It occurred to me that you really don't even need to write the MyTag class. You can just configure your .tld file to point directly at org.apache.taglibs.standard.tag.rt.core.SetTag. – Erik Gillespie Jun 09 '14 at 13:36
  • 1
    Ended up working perfectly once I added the other attributes and the jsp version and tlib version to the tag file. Thanks :) – TheOneWhoPrograms Jun 10 '14 at 06:58