Correct url flow for web Java-spark application

时间:2018-02-03 09:40:52

标签: web spark-framework

My web application is created with Spark Framework (Connecting on same page as server) the url of the first page is http://localhost:4567/start From here the user clicks on a button to decide one of four tasks. The form action button is /start

The server checks everything is okay and then returns the new page for this task (e.g fixsongs) (i.e returns the page contents as a string ) from the page.

post(RoutePath.START, (request, response)       -> new ServerStart().processRoute(request, response));//User has picked task on start page

ServerFixSongs ssfs = new ServerFixSongs();
path(RoutePath.STARTFIXSONGS, () ->
{
    //Display Page
    post(RoutePath.FIX,             (request, response) -> ssfs.startTask(request, response)); //User submits form on Options page
}); 

The problem is url stays the same, i.e is one behind where the user is

Now I have worked out how to solve this, instead of the server returning page it now does a redirect to /fixsongs.go (this is mapped in routes) which calls method that then returns the page contents as a string and modifys the url.

post(RoutePath.START, (request, response)       -> new ServerStart().processRoute(request, response));//User has picked task on start page
ServerFixSongs ssfs = new ServerFixSongs();
path(RoutePath.STARTFIXSONGS, () ->
{
    get(RoutePath.GO,               (request, response) -> new FixSongsPage(request.session().attribute(FOLDER)).createPage(null, null)); //Display Page
    post(RoutePath.FIX,             (request, response) -> ssfs.startTask(request, response)); //User submits form on Options page
});   

But I have two questions

  1. Is this more cumbersome approach the correct way to do this
  2. Do these additional redirect steps impact performance

Note I am not using templating but creating webpages using j2html

I cannot do a redirect directly to a html file in the first call since the html does not actually exist, the pages are created dynamically.

I have also realised that although when I submit start task from START page i submit a POST request because Im redirecting to STARTFIXSONGS this means at the next stage user can use BACK button to go back to STARTFIXSONGS. I would prefer they could not do this, so does this mean i shoud not be using redirects in this case.

1 个答案:

答案 0 :(得分:0)

您的问题与Spark或j2html无关,而是与网络应用程序有关。

  

这是采用正确方法执行此操作的更加繁琐的方法

这取决于你想要什么。如果您希望用户能够使用浏览器按钮进行导航,请使用Post-Redirect-Get流程。如果你不想要这个,你可能应该使用ajax requests,你使用JavaScript发布到服务器,服务器响应更新DOM的指令(通常是JSON格式)

  

这些额外的重定向步骤是否会影响效果

不够,你应该担心它。你再往一次往返,但99.9%的情况并不重要。如果您的目标用户群在不同的大陆上使用GPRS浏览您的网页,那么情况可能并非如此。