EL is not working in a jsp page - struts2

I have a JSP page,In that page,I am trying to use the page scope attributes using jstl and struts2 tags.
The following piece of code is,
<%# taglib uri="/struts-tags" prefix="s" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="test" value="ramesh"/>
<c:set var="test1" value="${test}"/>
<s:set var="test2" value="${test}" />
the <s:set> tag yields the following exception " According to TLD or attribute directive in tag file, attribute value does not accept any expressions".
I have two questions.
1)${test} works when it is used in <c:set> tag.
2)${test} does not works when it is used in <s:set> tag. Why?

EL and JSTL are now in Java EE standards, so they can cooperate well, I think.
However, when in struts 2 tags, because struts 2 have their expression language - OGNL,
so I think they prefer to use OGNL to EL in their tags, and that is the reason why they do not support EL. These are my guess.

From Apache link
From Apache FAQ link
As of Struts version 2.0.9 the JSTL/JSP expression language (EL) has been disabled for Struts tag attributes which evaluate OGNL. This is a precaution against security vulnerabilities that can result from the double-evaluation that occurs when an attribute is first processed as a JSTL/JSP EL expression and then the result is processed as an OGNL expression. The solution is to express all dynamic attribute values in Struts tags using OGNL expressions directly.

Related

What is the equivalent tag for <bean:page> in struts 2

I am working on code migration from Struts 1.x to Struts 2.0
Can anyone please tell me the equivalent tag for the below line in Struts 2.0
<bean:page id="req" property="request" /> is the tag in Struts 1.x
Now I want to convert the above line in Struts 1.x to the equivalent line in Struts 2.0.
There isn't a direct equivalent.
The <bean:page> tag just exposes a page-scoped bean for scriptlets, which you shouldn't be using.
If you want to use a page scope of the variable you can use scope attribute of the s:set tag
<s:set var="req" value="#request" scope="page"/>
Note, that the #request points to the Struts request map, it's not JSP's implicit request object, but it has access to request attributes.

Struts2 tag <s:text> I need to lower case the string returned

So I have a tag like this
<s:text name="furniture_logs" />
Where s is defined:
<%# taglib prefix="s" uri="/WEB-INF/tld/struts-tags.tld"%>
and "furniture_logs" is a key from the database (returnes a string) . I want to show the lower case of this string, what can I do ? I am new to Struts2 and i didnt find any reference for that.
The <s:text> tag is for rendering a I18n text messages. If you really want to use it then you could try calling getText method and toLowerCase in <s:property> tag like that:
<s:property value="getText('furniture_logs').toLowerCase()"/>
Struts2 uses OGNL that means you can call Java methods inside Struts2 tags. Read about OGNL http://commons.apache.org/ognl/.
Update
If using <s:text> tag is a must to you, then use var attribute of it.
<s:text var="flogs" name="furniture_logs"/>
<s:property value="#flogs.toLowerCase()"/>

access struts2 include param value in another jsp page in struts if tag

I am passing param value in include tag in jsp page like below
<s:include value="../../commonjspf/status.jspf">
<s:param name="mystatus" value="%{status}">
</s:param>
</s:include>
where status variable come from action class .
I want to access that mystatus param in status.jspf page in struts if tag to compare with my default values.
<s:if test ="">
</s:if>
or
<s:set name="" value =""/>
any of above tags.
how can i access ?
please suggest me .
Thanks.
Use the ${param.ParamName} notation to access them, as mentioned in the reference below:
http://struts.apache.org/2.0.14/docs/include.html
A sample code:
Page 1:
<s:include value="testPage.jsp">
<s:param name="mystatus">TestThis</s:param>
</s:include>
Page 2:
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="mystatus" value="${param.mystatus}" scope="page"/>
<s:if test='%{#attr.mystatus == "TestThis"}'>
This is what we want
</s:if>
<s:else>
This is not what we want
</s:else>
Any additional params supplied to the included page are not accessible within the rendered page through the tag since no valuestack will be created.
refer to the Struts2 documentation for details.
Struts2 Include tag
You can, however, access them in a servlet via the HttpServletRequest object or from a JSP page via a scriptlet.something like
${param.ParamName}
I would just like to throw this in as an alternative to using the struts include tag.
You can instead use the jsp:include tag and use the struts s:push tag to push parameters onto the stack and make them available in the included page, it adds an couple of extra lines into the jsp but is much more flexible as you can pass objects rather than just strings into the included JSP.
The nature of the push tag means once your done the parameters are poped from the stack again.
Primary JSP
<s:push value="#{'someStringParam':'some string', 'someObjectParam': someObject}">
<jsp:include page="../includes/user-tabs.jsp" />
</s:push>
Included JSP
<s:property value="someStringParam" />
<s:if test="someObjectParam.someValue > 10">
Result!
</s:else>
Building from James' answer, this helped me on my Page 2:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page isELIgnored="false" %>
<c:set var="mystatus" value="${param.mystatus}" scope="page"/>
<c:if test="${empty mystatus}">
<c:set var="mystatus" value="default status here" />
</c:if>
If you are passing a value from one page to another (as below) be careful about the syntax. You need to put single quotes around the name text you are passing, otherwise it's considered the name of a variable.
<s:include value="other.jsp">
<s:param name="thevar" value="'text i want to see'" />
</s:include>
Then in the "other.jsp" page you will see the "text i want to see" if you do as follows:
${param.thevar}
If, instead you do NOT place the single quotes in the param value attribute, you see nothing in the other.jsp page.
I only mention it as I've seen this a lot of times.

Does JSF 2.0 strip out HTML attributes when rendering?

I have to work with existing HTML and CSS and convert it to JSF app. So there are pure <li> elements (no JSF tags) with class="" attributes. When rendered with JSF 2.0 under Glassfish 3.1.1 the class="" attribute is removed from the <li> elements and the CSS breaks, i.e. the site breaks.
Probably the setting class="" is not the best to do but why does JSF strip it off when it affects the display of the page?
Facelets does only do that for attributes with empty values. It does not do that for attributes with a value like class="some". So nothing would break at all (expect of some hypothetically poor JS which rely on the presence of the attribute instead of the presence of an attribute value).
Note that GF 3.1.1 ships with JSF 2.1, not JSF 2.0 (to be precise, Mojarra 2.1.3).
use <f:verbatim> tag
<f:verbatim>
<div class="" custom-attribute="x"></div>
</f:verbatim>
this would print content without filtering.

why sec:authorize doesn't work?

I have a JSF 2 page based on Facelets and use Spring Security 3 behind the application. When I put some tags like this within my page:
<sec:authorize access="hasRole('SS')" >
<h:outputText value="X" /></sec:authorize>
the X will display at runtime anyway. The auto completion feature of eclipse work correctly to show the "sec:" tags and their properties at programming time. what's the problem?
Have you got:
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
at the top of the file?
Also, you need use-expressions="true" in the http tag in securityBeans.xml. Doing this means that any old style access="ROLE_BLAH" tags in securityBeans or wherever also need to change to use expressions.

Resources