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

5

5

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.

Phenol answered 11/11, 2011 at 10:37 Comment(0)
B
4

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}
Bradley answered 11/11, 2011 at 10:45 Comment(4)
Thanks it's display but i want to compare this value. how can i compare this value ?Phenol
you have to access them in jsp via the HttpServletRequest object or from a JSP page via a scriptlet.So you have to access these parameters using the plain HTTPServlet request something like <%= request.getParameter("type") %>. Hope this will help youBradley
can we set ${param.ParamName} in to any variable or in <s:set />Phenol
i have not tried that.only problem i can see that since this value is not in value Stack and only in request parameters so we can not use OGNL syntax which will try to resolve it against Value Stack but you can try the scriplet syntax to set value in some field i believe that can work out.Bradley
M
8

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>
Mean answered 11/11, 2011 at 10:42 Comment(3)
Thanks it's display but i want to compare this value. how can i compare this value ?Phenol
why can't we use <s:set .../> ?Citystate
I got the s:if test= to work with the c:set var="variable" value="'test string'" BUT I could not get the param to pass from <s:include ...><s:param name="var">myvalue etc... the c:set var="variable" value="${param.var}"... no use of ${param.var} would work for meCitystate
B
4

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}
Bradley answered 11/11, 2011 at 10:45 Comment(4)
Thanks it's display but i want to compare this value. how can i compare this value ?Phenol
you have to access them in jsp via the HttpServletRequest object or from a JSP page via a scriptlet.So you have to access these parameters using the plain HTTPServlet request something like <%= request.getParameter("type") %>. Hope this will help youBradley
can we set ${param.ParamName} in to any variable or in <s:set />Phenol
i have not tried that.only problem i can see that since this value is not in value Stack and only in request parameters so we can not use OGNL syntax which will try to resolve it against Value Stack but you can try the scriplet syntax to set value in some field i believe that can work out.Bradley
C
4

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>
Cyrille answered 17/12, 2012 at 13:50 Comment(0)
C
2

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>
Callison answered 8/3, 2013 at 19:16 Comment(0)
U
1

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.

Uralian answered 25/11, 2014 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.