Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...