2012-10-31 71 views
3

我只需在點擊提交按鈕後重定向到一個頁面,而不是直接在地址欄中輸入url。如果直接在地址欄中輸入,則應顯示主頁。如何在彈出的java中單擊提交按鈕之後才能進入下一頁

Iam使用session.getAttribute來完成上述過程。我想這是否有任何altrnative,因爲我需要這樣做每個職位的方法...

下面的方法是爲第一頁,其中我創建了一個會話屬性,在下一頁使用。

@RequestMapping(value = "/payment", method = RequestMethod.POST) 
public String submitForPayment(@ModelAttribute("deposit") Deposit deposit, ModelMap model, HttpServletRequest request) throws IOException 
{ 
      try { 
       HttpSession sessionForNextPage = request.getSession(true); 
       sessionForNextPage.setAttribute("vNumber", 
         deposit.getValidityNumber()); 

        return "redirect:success"; 

      } catch (NullPointerException exception) { 
       return "redirect:payment"; 
      } 
     } 

下面的方法適用於使用上面聲明的會話的下一頁。

@RequestMapping(value = "/success", method = RequestMethod.GET) 
public String showSuccess(ModelMap model, HttpServletRequest request) 
      { 
     try { 

      view = "success"; 
      HttpSession session = request.getSession(false); 
      int vNumber = (int) session.getAttribute("vNumber"); 
      System.out.println(vNumber); 
      if (vNumber != 0) { 
       request.getSession(false).removeAttribute("vNumber"); 
       return view; 
      } 

      else 
       return "pay"; 
     } catch (Exception e) { 

      return "redirect:pay"; 
     } 
    } 

是否有任何其他的方式來做到這一點,因爲我必須爲所有的方法做到這一點...

回答

3

每當我需要做的提交後,我總是用一箇中間頁捕捉POST數據並將數據存儲在數據庫中,並將記錄密鑰存儲在會話中,並將其重定向到顯示頁面,在那裏檢查記錄ID(如果有),然後從數據庫檢索數據並顯示它,如果不顯示錯誤消息。

因此,即使有人訪問你的顯示頁面直接(以URL類型化),它會顯示一個錯誤味精,而且在大多數情況下,人們不會看到中間頁的URL,但即使他們這樣做,你可以使用隨機令牌爲您HTML FORM並存儲在會話中並在中間頁面上進行驗證。

希望這會幫助你。

+0

您可以使用過濾器作爲您的中間頁面。 – Shurmajee

0

使用攔截器並確定攔截器方法中的引用URL。 調用攔截器爲每個動作或一些你需要攔截的動作。

public class AppInterceptor extends HandlerInterceptorAdapter{ 

    //before the actual handler will be executed 
    public boolean preHandle(HttpServletRequest request, 
    HttpServletResponse response, Object handler) throws Exception { 
     return true; 
    } 

//after the handler is executed 
    public void postHandle(HttpServletRequest request, HttpServletResponse response, 
    Object handler, ModelAndView modelAndView) throws Exception { 
     String referrer = request.getHeader("referer"); 
     // if the referrer string is null, it means the url is invoked by typing into address bad and then you can decide of redirecting user to home page. 
    } 
} 

這將工作。

相關問題