Skip to content Skip to sidebar Skip to footer

How Can You Execute Struts2 Actions That Need To Be In The Main Opening Jsp

When accessing the website at the main (index) page it has to load information that comes through actions. I have been doing some research but I do not see really clear how to do i

Solution 1:

To start your application, instead of pointing directly on a JSP like this:

http://localhost:8080/myapp/index.jsp

You can point to an action which forwards on index JSP.

http://localhost:8080/myapp/

Web.xml

<welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>

Index.html (placed under webContent, this is just an example):

<!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><METAhttp-equiv="Content-Type"content="text/html; charset=UTF-8"><METAHTTP-EQUIV="Refresh"CONTENT="0;URL='start.do'"><TITLE>My application</TITLE></HEAD></HTML>

struts-config.xml

<actionname="start"class="com.myapp.startAction"method="init"><resultname="success">/main.jsp</result></action>

It can be other way to do this, specially with struts2 (for instance using an index.jsp instead of index.html as suggested by @coding_idiot). But this should work.

Solution 2:

You can use Struts2 action in your web.xml file.Follow these steps

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

Create an empty file with name index in my web-app folder and finally placed the following entry in my struts.xml file

<actionname="index"class="welcomeAction"><result>/ab.jsp</result></action>

So in this case when You will be hitting this URL www.myapp.com/myApp,its calling index action of Struts2 and you will be able to do all init work for your welcome page.

Solution 3:

This is what I do :

index.jsp

<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" language="java"%><% response.sendRedirect("index"); %>

Assuming that you have a index named action defined in your struts.xml

Solution 4:

Suppose we have things like this:

Action: CalcAction.action

jsp: Calculator.jsp

Add this line in your JSP page's head tag e.g:

<meta http-equiv="Content-Type" CONTENT="text/html; URL='CalcAction.action'"/>

web.xml:

<welcome-file-list><welcome-file>/Calculator.jsp</welcome-file></welcome-file-list>

@kloe's answer also solves the problem but it will cause your page to refresh after every second, which in return cause overhead by calling your page after every second. No offense! Cheers!

Solution 5:

Better Use Normal Servlet mapping for that index.jsp in URL Mapping it will work.

Otherwise Javascript in onload of page is another option.

Post a Comment for "How Can You Execute Struts2 Actions That Need To Be In The Main Opening Jsp"