Search code examples
javajspstruts2log4jwelcome-file

Using the project's name to access the home page


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.


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.