Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

struts2

struts

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.

like image 647
Swapnil Sonawane Avatar asked Nov 11 '11 10:11

Swapnil Sonawane


People also ask

HOW include JSP in another JSP file?

To include JSP in another JSP file, we will use <jsp:include /> tag. It has a attribute page which contains name of the JSP file.

What is the purpose of the include tag*?

Include Tag This is used to include the contents of reusable content.

What is include tag?

The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.


3 Answers

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>
like image 146
James Jithin Avatar answered Oct 16 '22 16:10

James Jithin


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}
like image 39
Umesh Awasthi Avatar answered Oct 16 '22 16:10

Umesh Awasthi


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>
like image 4
3urdoch Avatar answered Oct 16 '22 16:10

3urdoch