Displaytag does not recognize struts element - struts2

I'm trying to pass column names as action parameter and use it inside of display tag.
the display:column does not grab any value in value stack, but just print whatever is in title attribute as plain string.
With following code:
<display:table name="pList" uid="chartRow" id="sTable" pagesize="10" class="table" requestURI="/some.action">
<s:iterator value="columns" var="column" status="cStatus">
<display:column title="${column}">
<s:property value="#attr.sTable[#column]" />
</display:column>
</s:iterator>
</display:table>
I get ${column} as column names for whole table.
Any suggestion?
sturts-2.2
displaytag-1.2

After much wasting of my time with jstl, el all that.
<%# page isELIgnored="false" %>
resolved my issue.

Related

Struts2 Iterate value from a textfield

I am trying to iterate over the textfield's value, for eg. 5. Is there a way to get the value?
I have a textfield with value 5:
<s:textfield
theme="simple"
cssClass="form-control"
name="instCount"
value="5"
style="width:25%;"
onkeyup="javascript:isNumber(this);"
/>
I need to iterate the value 5:
<s:if test='<s:property value="%{instCount}"/> > 0'>
<s:iterator value='<s:property value="%{instCount}"/>' var="count" status="countStatus">
</s:iterator>
</s:if>
You cannot nest JSP tags like that, and it appears that you haven't wrapped your head around OGNL or ELs in general, even though you use it correctly in the <s:property> tag.
Let's take a step back: what is this doing?
<s:property value="%{instCount}" />
It's referring to an action property named instCount.
How? Via the OGNL expression %{instCount}.
How does the <s:if> tag work? By evaluating an OGNL expression in the test property. Ah, OGNL expression, which we've already seen.
<s:if test='instCount > 0'>
How does the <s:iterator> tag work? By evaluating an OGNL expression in the value tag.
<s:iterator value='%{instCount}' etc...>
I would highly recommend taking some time to figure out the framework you're working in, even a minor reading of the documentation (and a basic understanding of how JSP works) will be highly beneficial, and avoid questions like this.

Removing single quotes in struts2

I have the following code in my JSP.
<td style="text-align:center;"><s:property value="accountCode"/></td>
when i get the values from the back-end, i am passing it with the single quotes(eg. '1234'). However, when i display it, i want to remove these quotes and display just the number.(eg.1234). How do i do this?
I tried <s:property value="accountCode.replace('\'','')"/> and <s:property value="accountCode.replaceAll('\'','')"/>, but it does not work!(Did not show me the value itself!)
If using replaceAll method is OK, then
<s:property value="accountCode.replaceAll('\\'', '')" />.
Use the escapeJavaScript attribute of the tag:
<s:property value="accountCode" escapeJavaScript="true"/>
EDIT:
Use this:
<s:property value="accountCode.replaceAll('\'','')" />

struts2 <s:iterator> with <s:param> not working

I am displaying the list values with tag.At the same time i want to provide a hyperlink for the displaying values to provide action for the displaying value.I am using <s:param> for that.but the values are not passing.I am writing like below
<s:iterator status="stat" value="transactionList">
<s:url id="open" action="openTransaction">
<s:param name="transactionCode" value="<s:property value='monthName'/>"/>
</s:url>
<tr class="gradeB">
<td>
<s:a href="%{open}"><s:property value='transactionCode'/></s:a>
</td>
<td><s:property value="monthName"/></td>
<td><s:property value="transactionDesc"/></td>
</tr>
</s:iterator>
Now the transactionCode property is displaying with hyperlinks and by clicking on that the action is forwarding to openTransaction method.but the value i passed with <s:param> is not going,it is giving null. In iteration for that particular transaction code i want to pass that particular transaction code only. In struts 1.x I used display tag table decorator for this purpose, it takes the current row object.Here also i want to pass the current row values to the action. Please help me.
If you want to use <s:property> tag to put a param inside an url you have to do it like that:
<s:url ...>
<s:param name="foo"><s:property value="bar"/></s:param>
</s:url>
The documentation of <s:param> explains the difference between using this way of putting a param and your way.
Note: When you declare the param tag, the value can be defined in either a value attribute or as text between the start and end tag. Struts behaves a bit different according to these two situations. This is best illustrated using an example:
<param name="color">blue</param> <-- (A) -->
<param name="color" value="blue"/> <-- (B) -->
In the first situation (A) the value would be evaluated to the stack as a java.lang.String object. And in situation (B) the value would be evaluated to the stack as a java.lang.Object object.
For more information see WW-808.
Edit: Also remember that if you are using 2.1.x or higher, the id attribute of <s:url> is deprecated and it has been replaced with var. You can read it here.
<c:url var="search" value="/image/search.action">
<c:forEach items="${filtersMap}" var="map">
<c:param name="filtersMap.${map.key}" value="${map.value}"/>
</c:forEach>
${filtersMap} is a map param from action

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.

struts2: problem in assigning a variable value to checkbox

I am working on struts2. In my jsp page I want to assign the value of a string variable to a checkbox (when it is checked by user). I tried it many times like -
<% String code = "decompose"; %>
First example:
<tr><td>
<s:checkbox name="codeCkBox" fieldValue="%{‘code’}" onclick="submit()"/>
</td></tr>
Second example:
<tr><td>
<s:checkbox name="codeCkBox" value="%{‘code’}" onclick="submit()"/>
</td></tr>
Third example:
<tr><td>
<s:set name="setCkBoxValue" value="%{‘code’}"/>
<s:checkbox name="codeCkBox" fieldValue="# setCkBoxValue" onclick="submit()"/>
</td></tr>
But everytime when I tried to get this value by checkbox name it returns variable name i.e “code”.
Looking for a solution.
Thanks in advance.
Have you tried doing ${code} instead of ${'code'}?
did u include your struts directive? i ask because its only showing 'code' which could mean its ignoring the struts
<tr><td> <s:checkbox name="codeCkBox" value="%{#code}"
onclick="submit()"/> </td></tr>
try the above code. since code is the JSP variable therefore it should be accessed with a # in front of its name rather than with a quote. Hope this helps

Resources