Struts 2 s:include tag not working for header jspf file

2.1k views Asked by At

I have index.jsp which contains header.jspf,main content and footer jspf. I am using s include to include header and footer jspf.

My header jspf contains surl tags with param values in it. I have included s tagslib in it.

My index.jsp/header.jspf both contains the taglib declaration.

<%@taglib prefix="s" uri="/struts-tags"%>

My header action file looks like below,

public class Header extends ActionSupport {

    public Header() {
    }

    @Override
    public String execute() throws Exception {
         return SUCCESS ;
    }

}

my struts xml declaration is as below

  <action name="Header" class="org.mypackage.mypackagename.Header" method="execute" > 
            <result name="SUCCESS">/WEB-INF/views/header/header.jspf</result>
            <result name="error">/WEB-INF/views/error-page.jsp</result>
        </action>

In the index jsp, I m including the jspf as ,

  <s:include value="/WEB-INF/views/header/header.jspf"/>

but for some reasons this never works, it shows the source code/broken html in header section. I also have struts 2 html tags in index.jsp which seems to work perfectly fine.

The same set up works for child pages which are not welcome page.

Do I need to use prepare interface to include header jspf. My footer jspf does not contain any struts tag so probably it seems to work fine.

I am unable to see whats going on here so need some other thoughts.

Index jsp is declared as welcomme page in web xml.

2

There are 2 answers

4
Andrea Ligios On

If Head is just a JSP Fragment, and your real, full page is Index.jsp, then you should map that page to an Action, not the fragment itself:

Index.jsp

<!DOCTYPE html>
<html>
    <head>
        <s:include value="/WEB-INF/views/header/header.jspf"/>
    </head>
    <body>
        <h2> This is Index.jsp </h2>

        <s:include value="/WEB-INF/views/header/footer.jspf"/>
    </body>
</html>

Struts.xml

<action name="Login" class="org.mypackage.mypackagename.Login" method="execute">
    <result name="success">/WEB-INF/views/login.jsp</result>
    <result name="error">/WEB-INF/views/error-page.jsp</result>
</action>

Note that "SUCCESS" is wrong: it should be "success", or Action.SUCCESS (a constant, also in ActionSupport, that is mapped to "success").

1
Sreekanth On

Use either sitemesh or tiles for page decoration like header and footer, etc. Sitemesh is the one i recommend. Its nothing do with struts2, no need to create header and footer struts2 actions to achieve this.

You can search on net for example implementation of sitemesh + struts2.

If you are not willing to check on sitemesh or any other decorating framework, best create header and footer jsp or html and include(jsp:inlcude tag) it in your index.jsp or which ever the jsp you want to show header and footer.