Question

In my web application I have to write the URL of the web application and specify the home.jsp page that I want to be the home page, I want to know how I can access the the application in the browser just by typing the project's root folder name.

I have set the welcome-file-list in web.xml to the home.jsp that I want to get to every time I access the application, but it is not helping. Probably because I am using Struts 2 framework, If it is possible should I set the welcome-file-list in struts.xml? How do I do it?

Another question, I have so far. For example in the execute method of one action class, I want two different results SUCCESS or ERROR, as

if(message.getMessageBody() != null &&  
          message.getMessageDestinationEmail() != null &&
     message.getMessageHead() != null){
         return SUCCESS;
} else
     return ERROR;

I mapped the error to error.jsp page and success to a different .jsp page in struts.xml as

<action name="message" class="com.mule.basik.action.PostOfficeAction" 
                                                     method="execute">
        <result name="success">/status.jsp</result>
        <result name="error">/error.jsp</result>
</action>

But whether (in the browser form) I fill in the messageHead messageBody or not the success page (/status.jsp) is returned.

I am not sure how(when) struts2 instantiates the objects members of an ActionClass, so I decided to declare a non parameter constructor that instantiates the bean message (member of the action class) because I thought for every request an instance of the action class is created and thus there is a new instance of the beans the action class has (depends on). But it didn't help anything, what am I doing wrong? I guess I should try to use Log4j and print something in execute method before the return SUCCESS and before return ERROR to determine if the if statement is evaluating to true or false, but even if I find that it is evaluating to true when inputs are entered or not , I still don't understand what I will have to do next, to make the execute return ERROR beside testing as I showed above.

Was it helpful?

Solution

It's possible to use a welcome file list, you should configure it in web.xml or web server default configuration file the files used as welcome files. When you access the web folder that contains this file the content of the the file will be loaded by web server regardless struts2 maps this folder or not as its action. Then in the welcome file you should place the code that redirect to the action. For example index.jsp is welcome file

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

index.jsp

<% response.sendRedirect("showcase.action"); %> 

Second question already discussed here, initializing the beans in the constructor is a bad idea, the constructor is used by the container to instantiate the action. At the moment when it's doing it you can't access some features or services because they are not available. Better do lazy initialization after the action is created and features are available, for example when the action is executed. If you want all actions to initialize before their execution than there's Preparable interface which is by default implemented by ActionSupport which you action should extend and override prepare method.

It's your responsibility to initialize the objects. Other objects that are submitted could be created by OGNL to populate your collections.

You could use Log4j with struts2 but it requires configuration in the project and on the server. For debug purposes you could write to the System.out which is redirected in the most IDEs to the console.

Really, I don't know what you are trying to do but normally return SUCCESS if no errors are found, or ERROR if there are errors.

OTHER TIPS

You can use welcome-file-list with struts also in your web.xml:

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

Since second part of your question is answered above , so i will not go in to details, i will try suggest a solution for the first part.

Since you want everything to be handle by Struts2 when some one hit your URL, so in that case as it is required that we should follow/execute complete flow by action so that Framework should get option to initialize its internal structure.

Here is one way to do that.

you can always declare Struts2 action name in you web.xml file and i am assuming that your first action name is welcome.action, here is what we need to have in web.xml

<welcome-file-list>
   <welcome-file>welcome</welcome-file>
 </welcome-file-list>
  1. Now create an empty welcome file inside your web-app folder.
  2. Create your welcome action say `

In Last part you need to configure you Welcome action in struts.xml file or through annotation like.

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

You can define all you logic which you want to execute inside your welcomeAction.Please take care of these points

  1. action name inside struts.xml file should be same as you have in welcome file list.
  2. Don't forget to make a file with same name as your welcome action name in web-app folder as this is a trick to let container call you Action.

Hope this will help you and many others who have same issue.

`

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top