2015-04-07 31 views
0

我正在開發一個應用程序,其中當用戶按下「登錄」按鈕LoginForm.jspUserRegistration.jsp會導致UserAccount jsp網頁上的特定action.In LoginForm - >用戶帳戶表單顯示。在用戶註冊表單中輸入詳細信息並提交時 - 顯示用戶帳戶表單。 下面是控制器代碼,當請求爲UserAccount.html如何知道從哪個jsp頁面請求已經到控制器中的Spring MVC

@RequestMapping(value="/UserAccount.html", method = RequestMethod.POST) 
     public ModelAndView userAccountForm(@Valid @ModelAttribute("user") UserDetails user,BindingResult result) { 

      if(result.hasErrors()) 

      { System.out.println(result.getAllErrors()); 
       ModelAndView model1=new ModelAndView("UserRegistration"); 
       return model1; 
      } 
      // User validation 


      System.out.println(user.getAccountType()); 
      userDAO.create(user); 

      ModelAndView model1 = new ModelAndView("UserAccount"); 
      return model1; 


} 

loginForm.jsp中

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<html> 
<body> 
    <h1 
     style="background-color: green; color: Yellow; padding: 10px; text-align: center;">Mybank 
     Online Login</h1> 

    <form:errors path="user.*" /> 
    <form action="/MyBankProject/UserAccount.html" method="post"> 
     <div 
      style="background-color: yellow; color: black; padding: 10px; text-align: center;" 
      align="center"> 
      <p> 
       User ID <input type="text" name="userName" /> 
      </p> 
      <p> 
       Password <input type="password" name="password" /> 
      </p> 
      <input type="submit" value="Login" /> <a 
       href="/MyBankProject/userRegistration.html">New User?</a> 
     </div> 
    <input type="hidden" name="page" value="LoginForm"/> 

    </form> 


</body> 
</html> 

UserRegistration.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<html> 
<body> 
    <h1>${headerMessage}</h1> 
    <h3>USER REGISTRATION</h3> 

    <form:errors path="user.*" /> 

    <form action="/MyBankProject/UserAccount.html" method="post"> 
     <table border="1" style="width:100%" > 

      <tr> 
       <td>FirstName :</td> 
       <td><input type="text" name="firstName" /></td> 
      </tr> 
      <tr> 
       <td>LastName :</td> 
       <td><input type="text" name="lastName" /></td> 
      </tr> 

      <tr> 
       <td>User's EmailId :</td> 
       <td><input type="text" name="emailId" /></td> 
      </tr> 
      <tr> 
       <td>user's gender :</td> 
       <td><select name="gender" multiple> 
         <option value="M">Male</option> 
         <option value="F">Female</option> 
       </select></td> 
      </tr> 
      <tr> 
       <td>user's age :</td> 
       <td><input type="text" name="age" /></td> 
      </tr> 
      <tr> 
       <td>user's DOB :</td> 
       <td><input type="text" name="dOB" /></td> 
      </tr> 
      <tr> 
       <td>Account Type :</td> 
       <td><select name="accountType" multiple> 
         <option value="Savings">Savings</option> 
         <option value="Current">Current</option> 
         <option value="FixedDeposit">Fixed Deposit</option> 
       </select> 
       <td> 
      </tr> 
      <tr> 
       <td>Amount :</td> 
       <td><input type="text" name="amount" /></td> 
      </tr> 
     </table> 
     <table> 
      <tr> 
       <td>UserName</td> 
       <td><input type="text" name="userName" /></td> 
      </tr> 
      <tr> 
       <td>Password</td> 
       <td><input type="password" name="password" /></td> 
      </tr> 
     </table> 
     <table> 
      <tr> 
       <td>User's Address :</td> 
      </tr> 
      <tr> 
       <td>country: <input type="text" name="address.country" /></td> 
       <td>city: <input type="text" name="address.city" /></td> 
       <td>street: <input type="text" name="address.street" /></td> 
       <td>pincode:<input type="text" name="address.pincode" /></td> 
      </tr> 

      <tr> 
       <td><button type="submit">Submit</button></td> 
       <td><input type="button" onclick="history.back();" 
        value="Cancel"></td> 
      </tr> 
     </table> 
    <input type="hidden" name="page" value="UserRegistration"/> 

    </form> 

</body> 
</html> 

現在對我來說,我需要知道從jsp頁面UserAccount被調用,基於此執行不同的操作。 我是Spring MVC的新手,並在整個互聯網上搜索解決方案。

回答

4

HTTP是一種無狀態協議。這意味着你不能對之前的狀態做出假設。

我想你有兩個選擇來獲取有關前一頁的信息。

  1. 向表單中添加一個隱藏字段,並將該頁面的名稱與請求一起發送。

    HTML:

    <form> 
        <input type="hidden" name="page" value="[the name of the current page]"/> 
    </form> 
    

    控制器:

    @RequestMapping(value="/UserAccount.html", method = RequestMethod.POST) 
    public ModelAndView userAccountForm(@RequestParam(value="page", required=false) String page) { 
        [...] 
    } 
    

    現在你可以檢查page值。

  2. 使用會話。在渲染表單控制方法保存當前頁面:

    @RequestMapping("/login") 
    public String login(HttpServletRequest request) { 
        request.getSession().setAttribute("lastPage", "login"); 
    
        return "redirect:/UserAccount.html"; 
    } 
    
    @RequestMapping("/register") 
    public String register(HttpServletRequest request) { 
        request.getSession().setAttribute("lastPage", "register"); 
    
        return "redirect:/UserAccount.html"; 
    } 
    

    入住userAccountForm()lastPage值:

    @RequestMapping(value="/UserAccount.html", method = RequestMethod.POST) 
    public ModelAndView userAccountForm(HttpServletRequest request) { 
        HttpSession session = request.getSession(); 
        String lastPage = (String) session.getAttribute("lastPage"); 
    
        session.removeAttribute("lastPage"); 
    
        [...] 
    } 
    
  3. 檢查請求頭的引薦字段。

    @RequestMapping(value="/UserAccount.html", method = RequestMethod.POST) 
    public ModelAndView userAccountForm(@RequestHeader(value = "referer", required = false) String referer) { 
        [...] 
    } 
    

    這給了referer作爲方法參數,如果有的話。小心。有可能沒有引薦人或該領域被客戶操縱。

+0

第一個沒有工作一些how.it返回空值 –

+0

您是否在窗體中爲頁面參數添加了隱藏字段? – stevecross

+0

是的我已經在我的兩個頁面中添加了LoginForm和UserRegistration,如下所示:

1

希望這能解決您的需求。

URL url = new URL(request.getHeader("referer")); 

System.out.println("last page url"+ url.getPath()); 
if(url.getPath().equals("your last url"){ 
//code 
} 
相關問題