I am trying to use s:url and the value contains a property. And it's not working.
<s:url value='/js/myJS-<s:property value="locale"/>.js'/>
Expected output:
....../contextPath/js/myJS-en_US.js
Nesting JSP tags like that is illegal.
Use normal JSP EL (assuming you're on a container supporting it):
<s:url value="/js/myJS-${locale}.js"/>
If you're not running a container that supports JSP EL (ew), use OGNL:
<s:url value="/js/myJS-%{locale}.js"/>
It's arguable this is the preferred mechanism since OGNL is S2's default EL.
When reporting something as "not working", you should also include what error(s) you get, for example, in this case, the JSP would never compile.
Related
I'm trying to set a cookie value within a JSP without using Java code directly. I know I could do it by creating a custom tag lib for that, but I wanted to keep it simple so I'm trying to do that the same way I access cookies: with EL expressions.
I know I can read the value of a cookie using JSP EL with the expression ${cookie['cookieName'].value}, but how can I set a particular value to that cookie using EL? I found solutions using java code in the JSP, but I want to avoid that.
So far I found ways to set variables using the c:set tag, but that doesn't accept expressions as the 'var' parameter so I can't do something like:
<c:set var="${cookie['cookieName'].value}" value="123" />
I think the way to go is , but I don't know what expression to use for the var part of it, or how to write it so I can set the cookie value instead of just a variable.
Any help is appreciated!
There is no standard expression to set cookie in JSP. You can use custom tag if you want OR use JSP script-less
<%
javax.servlet.http.Cookie cookie
= new javax.servlet.http.Cookie("name", "value");
// cookie.setXXX()
response.addCookie(cookie);
%>
NOTE: Make sure cookie is added BEFORE the response is committed.
I am trying to output text value of a property of a bean in session in a JSP. I need to output it between tag. I am struck up on the syntax. Here is my code till now
<%EditTicketBean etlBean= (EditTicketBean)session.getAttribute("etBeanInServlet");
<textarea><c:out value = "${etlBean.ticketDesc}" />
However this does not work. If I use out.println it works
<textarea><%out.println(etlBean.getTicketDesc());%> </textarea>
What is the correct syntax for using this JSTL tag
Why are you mixing JSTL and scriplets? You can't access the variables set in scriplets, inside an EL. Better option is to avoid that scriplet alltogether, and just use EL, to access attributes from session.
You can directly use the EL expression:
<textarea>${etBeanInServlet.ticketDesc}</textarea>
you can also explicitly specify that you are fetching the attribute from session scope (only when there is conflict between variables set in various scopes) like so:
<textarea>${sessionScope.etBeanInServlet.ticketDesc}</textarea>
and finally using using JSTL <c:out> tag, which will escape XML for you:
<textarea><c:out value="${etBeanInServlet.ticketDesc}" /></textarea>
I'm using spring to display a jsp page. That's fine. Now I'd like to include another page in it. I know I can use the <jsp:include> tag in my page however I'd like to use a controller to pass some logic to the page which is to be included. Is it possible please?
Thanks,
Krt_Malta
You might want to checkout a template engine like Freemarker or Velocity. Here's a description of how Spring integrates with view technologies.
Your controller can add objects to the Model and Spring will add them to the HTTPRequest as attributes, is that what you mean? Including a jsp fragment doesn't affect that, your jsp fragment can access the request attributes. Otherwise it's unclear to me what you mean by "passing some logic to the page".
Look into JSTL (Jsp Standard Tag Library).
You can implement conditional logic in your JSP using the JSTL <c:if> or <c:choose> tags. Then, instead of splitting up the logic into multiple files and using <jsp:include> to include the logic you want, you can build all the logic into your page and the controller can set request (or other scope) attributes to turn on the logic you desire.
For example:
<c:if test="${Order66}"
<c:forEach items="${JediMembership}" var="jedi">
kill ${jedi}
</c:forEach>
</c:if>
<c:if test="${Order67}"
two large pizza, extra cheese.
</c:if>
The controller then set "Order66" and / or "Order67" in the request (or any other scope).
I'm using Spring Web Flow (v. 1.0.5) and I have a JSP page that makes an AJAX call to a flow and needs to read in the XML results. That flow successfully sets an object into the FlowScope, then calls a JSP page to render the results. In the JSP page, I'd like to test whether the object has a property (say, .firstName) and if so, do something. I can access the variable in the FlowScope using JSTL expression language (by saying ${userObj}), but that just lets me spit it out. I've tried the methods below to get at it and put logic around it, with varying success.
Update: The remaining question is: How do I get the context and flow scope in the scriptlet (<% %>) section?
<rootnode>
<!-- Attempt 1: c:if isn't being interpreted (though ${userObj.firstName} is),
it's just displaying the tags on the page. -->
<!-- Update, I didn't have the <%# taglib directive for jstl/core.
Now I do, and they're being interpreted, but it says
"According to TLD or attribute directive in tag file,
attribute test does not accept any expressions"
How can the if/#test not accept expressions? Isn't that its whole purpose
in life? -->
<!-- Update 2, I also forgot the taglib for the rt language, which is separate,
I guess (jstl/core_rt). <c:if test now works properly. -->
<c:if test="${!empty(userObj.firstName)}">
<test>not empty</test>
</c:if>
<%
/* Attempt 2: This throws an error, can't resolve "context". How do I get the context? */
if (context.getFlowScope().userObj != null) {
out.write("not null");
} else {
out.write("not not null");
}
%>
<!-- Attempt 3: This works, I get the reference to the Object and it
spits out the correct firstName, gives me no control other than
spitting out the value. -->
<userReference>${userObj}</userReference>
<userReference_firstName>${userObj.firstName}</userReference_firstName>
</rootnode>
Attempt 1 should work if you installed JSTL and declared the taglib the right way and declared the web.xml to be at least Servlet 2.4. Also see the questions:
Where do I put jstl.jar and standard.jar so that Netbeans don't give errors/warnings?
ModelAttributes not accessible on jsp
To test if an object is empty or not, you should rather use the following construct:
<c:if test="${not empty userObj.firstName}">
or
<c:if test="${userObj.firstName != null}">
Attempt 2 is strongly discouraged. Scriptlets are a poor practice and should always be replaced by taglibs like JSTL and EL (as you did in attempt 1). If it's not possible for technical reasons, then the coding ought to be done in a real Java class, (indirectly) starting with a Servlet.
Attempt 3 is doable, but I would recommend to use servlet with a Javabean-to-XML serializer like XStream instead. This way you can just feed a collection of Javabeans to the outputstream of the response transparently.
So according to my JSP reference book, as well as every other reference I can find on the web, I'm supposed to be able to do something like:
<%# tag dynamic-attributes="dynamicAttributesVar" %>
and then when someone uses an attribute that I didn't define in an attribute directive, I should be able to access that attribute from the "dynamicAttributesVar" map:
<%= dynamicAttributesVar.get("someUnexpectedAttribute") %>
However, that doesn't work, at all; I just get a "dynamicAttributesVar cannot be resolved" error when I try.
Now, I did discover (by looking at the generated Java class for the tag) that I can "hack" a working dynamic attributes variable by doing:
<% Map dynamicAttributesVar = _jspx_dynamic_attrs; %>
Now, that hack doesn't work unless I also use the dynamic-attributes parameter on my tag directive, so it seems that the parameter is doing something.
But what I want to know is, how can I make it do what it does for every other JSP user out there?
Just trying to get a badge for answering a four year old question.
I have this problem as well and came across some help at O'Reilly to use JSTL instead of scriptlets.
The original poster could have used this code to get all keys/values:
<c:forEach items="${dynamicAttributesVar}" var="a">
${a.key}="${a.value}"
</c:forEach>
This would get a specific value:
<c:out value="${dynamicAttributesVar['someUnexpectedAttribute']}"/>
Isn't "dynamicAttributesVar" the name of the key in the page context that the dynamic attributes are put into? So you could do
<c:out value="${dynamicAttributesVar.someUnexpectedAttributes}"/>
or if you must use scriptlets:
Map dynamicAttributes = (Map) pageContext.getAttribute("dynamicAttributesVar")
(Disclaimer: I haven't tried it, I've just used dynamic attributes in tags with direct Java implementations... but it seems reasonable)