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

【@Controller,@RequestMapping,@RequestParam的使用方法】天下之事常成于困约,而败于奢靡。这篇文章主要讲述@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="https://www.songbingjia.com/register",method=RequestMethod.GET) @RequestMapping(value="https://www.songbingjia.com/register",method=RequestMethod.GET) public String register() { logger.info("register GET方法被调用..."); return "registerForm"; } @RequestMapping(value="https://www.songbingjia.com/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="https://www.songbingjia.com/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"; } }


    推荐阅读