如何知道Spring MVC中的控制器来自哪个jsp页面

Him*_*olu 0 spring spring-mvc

我正在开发一个应用程序,其中LoginForm.jspUserRegistration.jsp会导致UserAccount jsp网页上的特定action.In LoginForm当用户按下"登录"按钮- > UserAccount形式是当输入并提交信息displayed.In放在userRegistration表- >显示UserAccount形式.以下是请求时的控制器代码UserAccount.html

 @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;


}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

现在对我来说,我需要知道UserAccount调用的jsp页面,以便根据它执行不同的操作.我是Spring MVC的新手,并在互联网上搜索解决方案.

ste*_*oss 5

HTTP是无状态协议.这意味着您无法对先前的状态做出假设.

我认为你有两种方法可以获得有关上一页的信息.

  1. 在表单中添加隐藏字段,并使用请求发送页面名称.

    HTML:

     <form>
         <input type="hidden" name="page" value="[the name of the current page]"/>
     </form>
    
    Run Code Online (Sandbox Code Playgroud)

    控制器:

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

    现在您可以检查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";
     }
    
    Run Code Online (Sandbox Code Playgroud)

    检查以下lastPageuserAccountForm():

     @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");
    
         [...]
     }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 检查请求标头的referer字段.

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

    如果有的话,这会将referer作为方法参数.小心.可能没有引用者或者该字段是由客户操纵的.

  • 是的,做到了.我是这个网站的新手,所以不知道我需要接受它 (2认同)