How to avoid WSOD (blank screen) while loading long-running initialization data in Struts2?

守給你的承諾、 提交于 2019-11-30 09:47:44

问题


I need to do the following:

  1. User logs in.
  2. Redirected to welcome screen.
  3. Looks at the welcome screen while lots of records are loaded.
  4. Redirected to the working screen.

I am looking for a way to do in Action class something like this:

public class LinkAction extends ActionSupport implements SessionAware {
        @Autowired
        private ServiceDelegate myService;

    public String welcome()
        {
            new Runnable() {
                @Override
                public void run() {
                    myService.getLoadsOfData();

                    //redirect to the next action

                }
            }.run();
            // this is where the user 
            // goes to look at the welcome screen        
            return "welcome";
        }
    }

May be it's a wrong approach, please tell if so, I am new at Struts.


回答1:


The right way is the one already suggested by AleksandrM in comments: open the page, show an indicator while you call an ajax action (let's say with jQuery, for convenience), then render the result and remove the indicator. It's easier than you think:

public class MainAction extends ActionSupport {    
    public String execute() {
        return SUCCESS;
    }
}
public class AjaxAction extends ActionSupport {    
    @Autowired
    private ServiceDelegate myService;

    private Stuff myStuff; // with getter

    public String execute() {
        myStuff = myService.loadLongStuff();
        return SUCCESS;
    }
}

Your AJAX action can either return JSON data, a JSP snippet or a Stream of binary data. Choose the way you prefer. For example, if you map SUCCESS of AjaxAction to a JSP snippet, your JSP snippet will be:

ajaxSnippet.jsp

<%@ taglib prefix="s" uri="/WEB-INF/struts-tags.tld" %>
Stuff: <s:property value="myStuff" />

Then in your main.jsp, show the indicator in the div you will overwrite with the AJAX call's result:

main.jsp

<body>
    <div id="main">          
        <img src="/images/mesmerizingProgressBar.gif" />
    </div>    

    <script>
        $(function(){ // onDocumentReady...
            $.ajax({ // call ajax action...
                type : 'GET',
                url : '/ajaxAction.action', 
                success : function(data,textStatus,jqXHR){
                    // then render your result in "main" div, 
                    // overwriting the loading GIF
                    $("#main").html(data); 
                },
                error : function(jqXHR, textStatus, errorThrown){
                    $("#main").html("Error ! " + textStatus); 
                }
            });
        });
    </script>
</body>



回答2:


Thank you for the AJAX idea.

However, the answer I was looking for was in fact Struts interceptor "execAndWait". I decided to use it over AJAX because I am dealing with existing application and all the Struts plumbing is in place. This is the Struts guide on this



来源:https://stackoverflow.com/questions/28275323/how-to-avoid-wsod-blank-screen-while-loading-long-running-initialization-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!