请求SpringMVC的Controller,出现404的错误

u013904882 2017-02-10 03:59:56


Controller文件代码:
@Controller
@RequestMapping("/login")
public class LoginController {
@Resource
private UserServiceDaoImplement userServiceDaoImpl;
@RequestMapping(value="/userLogin", method = RequestMethod.POST)
@ResponseBody
public Map userLogin(HttpServletRequest request,
@RequestParam(value="username") String username,
@RequestParam(value = "password") String password,Map maps) throws Exception{

前端login.HTML的js代码:
$.ajax({
type: "POST",
dataType: "json",
url: "login/userLogin",
data: {"password":password,"username":username},
success: function (data, textStatus) {
alert(data+"==="+textStatus);
window.location.href = 'index.html';
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("登录失败:用户名或密码错误");
}
});


请问大神,可能那里出问题了?
...全文
1045 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
什么都不能 2017-02-10
  • 打赏
  • 举报
回复
随便玩的,仅供参考
package org.tom.pfms.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.tom.pfms.common.dto.UserDTO;
import org.tom.pfms.common.utils.ConstantSettings;
import org.tom.pfms.common.utils.MessageUtils;
import org.tom.pfms.service.AccessControlService;

@Controller
public class LoginController extends BaseController {
    private AccessControlService accessControlService = null;
	
	@Autowired
	public void setAccessControlService(AccessControlService accessControlService) {
		this.accessControlService = accessControlService;
	}

	@RequestMapping(value="/signon", method=RequestMethod.GET)
    public String showSignOnForm(HttpServletRequest request) {
		try{
			HttpSession session = request.getSession(false);
			if(session != null) {
				Object sessionObj = session.getAttribute(ConstantSettings.LOGIN_USER);
				if(null != sessionObj) {
					return "redirect:portal";
				}
			}
			return "signon";
		}catch(Exception e){
			return "signon";
		}
		
	}

	@RequestMapping(value="/signon", method=RequestMethod.POST)
    public String doSignOnForm(HttpServletRequest request) {
		UserDTO user = new UserDTO();
		String message = "";
		int iResult = 0;
		try {
			String userName = request.getParameter("userName").trim();
			String passWord = request.getParameter("passWord").trim();
			String remeberMe = request.getParameter("remeberMe");
			user.setUserName(userName);
			user.setPassWord(passWord);
			Map<String, Object> loginResultMap = accessControlService.validateUser(user);
			String loginResult = (String)loginResultMap.get(ConstantSettings.LOGIN_RESULT);
			if(ConstantSettings.LOGIN_SUCCESS.equals(loginResult)) {
				UserDTO loginUser = (UserDTO)loginResultMap.get(ConstantSettings.LOGIN_USER);
				request.getSession(true).setAttribute(ConstantSettings.LOGIN_USER, loginUser);
			}
			iResult = Integer.parseInt(loginResult);
			switch(iResult) {
			case -1:
				message = MessageUtils.getMessage(ConstantSettings.LABEL_LOGIN_INVALID_USER);
			break;
			case 1:
				message = MessageUtils.getMessage(ConstantSettings.LABEL_LOGIN_SUCCESS);
			break;
			case 0:
				message = MessageUtils.getMessage(ConstantSettings.LABEL_LOGIN_ERROR_PASSWORD);
			}
			
			if("1".equals(remeberMe)) {
				request.setAttribute(ConstantSettings.KEY_RESULT, user);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("showSignOnForm: login action error.", e);
			message = MessageUtils.getMessage(ConstantSettings.LABEL_LOGIN_INVALID_USER);;
		}
		String result = iResult == 1 ? "redirect:portal" : "signon";
		request.setAttribute(ConstantSettings.KEY_MESSAGE, message);
    	return result;
    }
	
	@RequestMapping("/signout")
	public String signOut(HttpServletRequest request){
		HttpSession session = request.getSession(false);
		if(null != session) {
			Object obj = session.getAttribute(ConstantSettings.LOGIN_USER);
			if(obj != null) {
				session.setAttribute(ConstantSettings.LOGIN_USER, null);
				session.invalidate();
			}
		}
		return "redirect:/signon";
	}
}
黑色龙猫 2017-02-10
  • 打赏
  • 举报
回复
配置问题嘛,404错误,你的项目根本没有编译成功啊
jiajing1990_ 2017-02-10
  • 打赏
  • 举报
回复
你是不是没配置component-scan
ASCII_marvel 2017-02-10
  • 打赏
  • 举报
回复
看请求路径貌似是对的,在前端页面写个demo测试一下ajax到底有没有传值给后台,页面和后台分别debug一下
q_srbzy 2017-02-10
  • 打赏
  • 举报
回复
仔细看了下 是UserLogin.jsp 这个页面 找不到,你看下你java放跳转的页面
q_srbzy 2017-02-10
  • 打赏
  • 举报
回复
url 可以这样写吗,我一般是'<%=reuqest.getContextPath()%>/xxxx/xxxx.do'

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧