【问题标题】:Spring 3.1 MVC: GET & POST on the same mapping, can it work?Spring 3.1 MVC:GET 和 POST 在同一个映射上,它可以工作吗?
【发布时间】:2012-07-05 16:24:24
【问题描述】:

我正在学习 Spring 3.1。

我的网络应用名称是“acme”。

网址大致是https://blah.blah.blah/acme

该 URL 设置为显示 login.jsp

我的控制器中有一个“/login”映射,我的 login.jsp 提交到该映射

如果出现问题,它会将用户返回到 login.jsp,并在浏览器中使用以下 URL: https://blah.blah.blah/acme/login

“/login”映射设置为处理 POST 请求,所以我担心用户书签 https://blah.blah.blah/acme/login,并得到“GET request not supported”的错误信息

所以,我想我会添加一个函数来处理对 /login 的 GET 请求,以通过我的“/”和“/home”的通用映射处理程序重新路由:

Login.Controller.java

package gov.noaa.acme.controller;

import java.security.Principal;
import javax.servlet.http.*;

import org.springframework.stereotype.Controller;
import org.springframework.validation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import org.apache.log4j.Logger;

@Controller
public class LoginController {


    private static final Logger logger  = Logger.getLogger(LoginController.class);

    @RequestMapping({"/","home"})
    public String home(ModelMap model,HttpSession session,HttpServletRequest request) {

        // Do some stuff

        return "login";
    }
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login_get(){
        logger.debug("started...");
        return "forward:home";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@ModelAttribute("laph") LAPH laph, 
                        BindingResult bindingResult,
                        ModelMap model, 
                        HttpSession session,
                        HttpServletRequest request, 
                        HttpServletResponse response,
                        @RequestParam(required=true) String last_usertype) {

        if (bindingResult.hasErrors()) {
            logger.debug("Error returning home");
            return "home";
        }

        logger.debug("Started ....");  

        // Do authentication  


        if (!isAuthenticated) {
            model.put("status_message", error_message);       
            return "login";
        }

        // success, send newly authenticated user to a search page
        nextView = "search";
        return "redirect:" + nextView;
    }

}// end class LoginController

我的日志显示我什至没有达到处理 /login 的 GET 请求的控制器方法,我仍然收到 /login 不支持 GET 的错误消息。

关于如何解决此问题的任何想法?

谢谢

史蒂夫

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

    我担心用户将 https://blah.blah.blah/acme/login 加入书签,并收到“不支持 GET 请求”的错误消息。

    您的方法签名是正确的;使用您在login_getlogin 上放置的注释,Spring 不会混淆,并且会为 GET 和 POST 请求调用正确的方法。

    你的方法home是错误的;它返回字符串"login",但我猜您没有名为 login 的视图,并且您希望它调用其中一种登录方法。在这种情况下,您应该返回“forward:login”,但这个解决方案也好不到哪里去。

    我的建议是:

    1. /home 应该使用文件 home.jsp 或您正在使用的任何视图技术呈现主视图。
    2. 使用HandlerInterceptor 来检查用户是否已登录,如果没有,只有将他重定向到登录网址。

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2012-02-15
      相关资源
      最近更新 更多