同名方法可以存在同一个controller层,通过

同一个控制层,可以有两个相同的方法名,方法带的参数可以相同也可以不相同。

springmvc首先通过请求路径识别@RequestMapping(value="/login.aspx");

如果请求路径一致,则通过请求方法类型识别@RequestMapping(value="/login.aspx",method=RequestMethod.GET)

示例:

package cn.dapeng.core.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.dapeng.core.bean.user.Buyer;
import cn.dapeng.core.service.user.BuyerService;

/**
 * 登录
 * 去登录页面
 * 提交登录表单
 * 密码加密 MD5+十六进制加密 加盐
 * @author Administrator
 *
 */
@Controller
public class LoginController {

	@Autowired
	private BuyerService buyerService;
	//去登录页面
	@RequestMapping(value="/login.aspx",method=RequestMethod.GET)
	public String login() {
		return"login";
	}
	
	//提交表单的登录
	@RequestMapping(value="/login.aspx",method=RequestMethod.POST)
	public String login(String username,String password,String returnUrl,Model model) {
		// 判断用户名不能为空
		if (StringUtils.isNotBlank(username)) {
			// 判断密码不为空
			if (StringUtils.isNotBlank(password)) {
				// 用户名必须正确nu
				Buyer buyer = buyerService.selectBuyerByusername(username);
				// 密码必须正确
				
				// 保存用户名到session中
				// 回跳到之前的访问页面
			} else {
				model.addAttribute("error", "密码不能为空");
			}
		}else {
			model.addAttribute("error", "用户名不能为空");
		}
		return"login";
	}
}

猜你喜欢

转载自blog.csdn.net/w20228396/article/details/79401475
今日推荐