Umesh Awasthi
Umesh Awasthi

Reputation: 23587

Best way to include dynamic jsp in Struts2

In my struts2 based application i need to include working template (a jsp file).I have designed the layout and has everything in place.only part of application which will change throughout the application is working area.

On my index.jsp file i need to include menu which are database driven and for which i need to hit the action one way which is coming to my mind is like using

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=welcome.action">

in the head section of my index.jsp,but i don't want to have redirect at first place in my application.

is there any other way to achieve this?

Upvotes: 0

Views: 977

Answers (2)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

create an empty file name welcome in your web-content folder.Add follwinf entry to your web.xml

 <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
 <filter-mapping>
        <filter-name>action2</filter-name>
         <url-pattern>/*</url-pattern>
         <dispatcher>REQUEST</dispatcher>    
         <dispatcher>FORWARD</dispatcher>
         <dispatcher>INCLUDE</dispatcher>
         <dispatcher>ERROR</dispatcher>
    </filter-mapping>

replace your welcome list file in web.xml as

<welcome-file-list>
        <welcome-file>welcome</welcome-file>
</welcome-file-list>

and finally in your strus.xml do something like

<action name="welcome" class="welcome.action">
        <result>/index.jsp</result>
    </action>

what we are trying to do is that when we hit example.com instead of showing the welcome jsp file we are hitting the action and using its result.In this case your URL will remain same say www.mysite.com

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160181

That's a normal approach; nothing wrong with it.

Another is to define an action as a welcome page, but you'll need to define dispatcher elements on the filter as per this SO answer.

Upvotes: 1

Related Questions