访问/users/login.action报错 404

来源:9-7 登录、登出、拦截器 代码讲解

AllenAi

2019-02-26

图片描述

图片描述

package com.imooc.controller;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.imooc.bean.AdminUser;
import com.imooc.utils.IMoocJSONResult;

@Controller
@RequestMapping("/users")
public class UserController {
	
	@GetMapping("/login")
	public String login() {
		return "login";
	}
	
	@PostMapping("/login")
	public IMoocJSONResult login(String username, String password,
			HttpServletRequest request, HttpServletResponse response) {
		if(StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
			return IMoocJSONResult.errorMsg("用户名或密码不能为空!");
		}else if(username.equals("lee") && password.equals("lee")) {
			String userToken = UUID.randomUUID().toString();
			AdminUser user = new AdminUser(userToken, username);
			request.getSession().setAttribute("sessionUser", user);
			return IMoocJSONResult.ok();
		}
		return IMoocJSONResult.errorMsg("登录失败,请重试!");
	}
	
	@GetMapping("/logout")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("sessionUser");
		return "login";
	}
}

这是login.js中的部分代码

submitHandler: function(form) {
            	var loginForm = $('.login-form');
            	var hdnContextPath = $("#hdnContextPath").val();
            	$.ajax({
                    type: "post", // 提交方式 get/post
                    url: hdnContextPath + '/users/login.action', // 需要提交的 url
                    data: loginForm.serialize(),
                    dataType: "json",
                    success: function(res) {
                    	debugger
                        // 登录成功或者失败的提示信息
                        if (res.status == 200 && res.msg == "OK") {
                        	window.location.href = hdnContextPath + "/center.action";
                        } else {
//                        	SweetAlert.error(data.msg);
                        	alert(res.msg);
                        }
                    }
                });
            	
            }
写回答

2回答

风间影月

2019-02-26

你是通过maven的tomcat插件运行的项目呢?还是通过eclipse的插件tomcat运行的呢?

0
1
AllenAi
eclipse配置debug configuration
2019-02-26
共1条回复

AllenAi

提问者

2019-02-26

为啥前端显示返回的是/WEB-INF/center/users/login.jsp啊 

我明明返回的是IMoocJSONResult.ok()啊

0
2
AllenAi
回复
风间影月
哦哦 对对对。。明白了
2019-02-26
共2条回复

Spring Boot短视频小程序开发 全栈式实战项目

一门课程带你搞定短视频小程序全栈开发,毕业设计之利器!

2618 学习 · 1956 问题

查看课程