Spring4MVC取参还能更加方便

  1. 其实在Spring4MVC中还可以更加方便的取参
  2. 以前我们通过public String loginMethod(@RequestParam("username") String u, @RequestParam("password") String p, Model model) {}取得url中username的参数值

核心代码如下:

 `
    
    package controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    @Controller
    public class UserinfoController {
        @RequestMapping(value = "login")
        public String loginMethod(String username, String password, Model model) {
            System.out.println("username"+username+":"+"password"+password);
            if (username.equals("a") && password.equals("aa")) {
                model.addAttribute("usernameKey", username);
                return "ok.jsp";
            } else {
                return "no.jsp";
            }
        }
    }
`

index.jsp

`<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 
 
 
 
 
 post:
 
username:
password:


get:
username:
password:

`

url中同名的参数将要自动传给控制层方法中同名的参数,并且不在需要**@RequestParam("username") **注解了。

你可能感兴趣的:(Spring4MVC取参还能更加方便)