Spring MVC 3,使用 Ajax 时转发不起作用

发布于 2024-11-15 05:48:13 字数 1298 浏览 4 评论 0原文

我有一个非常有趣的问题。我正在为我的网络应用程序制作登录页面,并通过 AJAX 发送登录请求。如果成功我想将用户转发到另一个页面。

看来他的遭遇就是这样。我发送 Ajax 请求,控制器将我转发到需要的视图(我在调试模式下看到日志),但我停留在同一页面上,因为我假设该页面正在等待 AJAX 响应,因此转发不会发生。

我认为这也是错误的处理方法,但由于我是新手,所以不知道更好。我如何登录并将用户转发到下一页。

谢谢。

这是我的代码:

JS代码:

    Page.authenticate = function() {
        $.ajax({
              url: "/login/authenticate/" + $('#username').val() + "/" + $('#password').val(),
              type: "GET",
              success: function(poi){
                 // alert("nesto");
              }
           });

        return true;
    }

控制器类:

@Controller
public class LoginPageController {

private Logger logger = Logger.getLogger(this.getClass());

@Autowired
private UserManagement userManagement; 

@RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) {

if (userManagement.authenticateUser(userName, password)) {
        String forward = "forward:/login/success";
        return forward;
    } else {
        model.addAttribute("errorMessage", "Invalid Username/Password, please try again!");
        return "/";
    }

}

}

I have very interesting problem. I am making log in page for my web app and I am sending login request via AJAX. If success I want to forward user to another page.

It seems that his is what happens. I send Ajax request, controller forwards me to need view (I see log in debug mode) but I stay on the same page, since I assume the page is waiting for AJAX response and for that reason forwarding does not happen.

I think this is also wrong way to approach this but since I am new to this don't know better. How can I log in and and forward user to next page.

Thank you.

Here is my code:

JS Code:

    Page.authenticate = function() {
        $.ajax({
              url: "/login/authenticate/" + $('#username').val() + "/" + $('#password').val(),
              type: "GET",
              success: function(poi){
                 // alert("nesto");
              }
           });

        return true;
    }

Controller Class:

@Controller
public class LoginPageController {

private Logger logger = Logger.getLogger(this.getClass());

@Autowired
private UserManagement userManagement; 

@RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) {

if (userManagement.authenticateUser(userName, password)) {
        String forward = "forward:/login/success";
        return forward;
    } else {
        model.addAttribute("errorMessage", "Invalid Username/Password, please try again!");
        return "/";
    }

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高速公鹿 2024-11-22 05:48:13

如果您使用 AJAX,则需要在 @ResponseBody 注解内进行响应。

    @RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
    public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) {

    if (userManagement.authenticateUser(userName, password)) {
            String forward = "forward:/login/success";
            return forward;
        } else {
            String forward = "forward:/login/error?message=Invalid Username/Password, please try again!";
            return forward;
        }

    }

     @RequestMapping(value="/login/success", method=RequestMethod.GET)
      @Responsebody
      public String handleMySuccessRedirect() {
         return "Logged In successfully"
              } 

 @RequestMapping(value="/login/error", method=RequestMethod.GET)
  @Responsebody
  public String handleMyExceptionOnRedirect(@RequestParamter("message") String message) {
     return message;
          } 

更新:

 @RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
@ResponseBody
    public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) {

    if (userManagement.authenticateUser(userName, password)) {
            String response = "Logged Successfully";
            return response;
        } else {
            String response = "Invalid Username/Password, please try again!";
            return response;
        }

    }

You need to response within @ResponseBody Annotation if you are using AJAX.

    @RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
    public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) {

    if (userManagement.authenticateUser(userName, password)) {
            String forward = "forward:/login/success";
            return forward;
        } else {
            String forward = "forward:/login/error?message=Invalid Username/Password, please try again!";
            return forward;
        }

    }

     @RequestMapping(value="/login/success", method=RequestMethod.GET)
      @Responsebody
      public String handleMySuccessRedirect() {
         return "Logged In successfully"
              } 

 @RequestMapping(value="/login/error", method=RequestMethod.GET)
  @Responsebody
  public String handleMyExceptionOnRedirect(@RequestParamter("message") String message) {
     return message;
          } 

Update:

 @RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
@ResponseBody
    public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) {

    if (userManagement.authenticateUser(userName, password)) {
            String response = "Logged Successfully";
            return response;
        } else {
            String response = "Invalid Username/Password, please try again!";
            return response;
        }

    }
假情假意假温柔 2024-11-22 05:48:13

您可以在此处执行以下操作:

  1. 不要从控制器返回视图,而是返回 json,根据 json 中的响应,适当设置位置 - window.location = 'home. action' - 这是一个 使用的示例ext-js

  2. 让登录页面执行完整的发布,而不是 AJAX 发布。

There are a couple of things you can do here:

  1. Don't return a view from your controller, instead return json, based on the response in json, set the location appropriately - window.location = 'home.action' - here is an example using ext-js

  2. Let the login page perform a full fledged post, not an AJAX post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文