@Controller,@RequestMapping,@RequestParam的使用方法

@Controller //@Controller表示该类的实例为控制器类型,不需要再继承或者实现特定接口 @RequestMapping("/user") //@RequestMapping用在类上面,表示所有的方法都将映射为类级别的的请求/user下面 public class UserController { private static List<User> userList; public UserController() { super(); this.userList = new ArrayList<User>(); } private static final Log logger = LogFactory.getLog(UserController.class); //register方法就被映射到如下路径: //localhost:8080/projectName/user/register,这里的/user对应的是类注解@RequestMapping("/user"),这里的/register对应的是方法注解 @RequestMapping(value="/register",method=RequestMethod.GET) @RequestMapping(value="/register",method=RequestMethod.GET) public String register() { logger.info("register GET方法被调用..."); return "registerForm"; } @RequestMapping(value="/register",method=RequestMethod.POST) public String register( //@RequestParam("loginname")表示将请求中的loginname的值赋值给loginname变量(相当于String loginname = request.getParameter("loginname")) @RequestParam("loginname") String loginname, @RequestParam("password")String password, @RequestParam("username") String username) { logger.info("register POST方法被调用..."); User user = new User(loginname, password, username); this.userList.add(user); return "loginForm"; } @RequestMapping(value="/login") public String login( @RequestParam("loginname") String loginname, @RequestParam("password") String password, Model model) { logger.info("login方法被调用了..."); logger.info("loginname:"+loginname+"password:"+password); for(User user : userList) { if(user.getLoginname().equals(loginname) && user.getPassword().equals(password)) { model.addAttribute("user", user); return "welcome"; } } return "loginForm"; } }

@Controller,@RequestMapping,@RequestParam的使用方法

上一篇:javascript学习-创建json对象数据,遍历


下一篇:Server.MapPath()用法