spring mvc 常用注解及使用

@Controller
public class TestRequestMappingController {

    /**
     * 退出session
     *
     */
    @ResponseBody
    @RequestMapping("/quitsession")
    public String quitsession(HttpSession session){
        String resultfirst= String.valueOf(session.getAttribute("user"));
        session.removeAttribute("user");
        String resultlast= String.valueOf(session.getAttribute("user"));
        String result="resultfirst:"+resultfirst+"\n"+"resultlast:"+resultlast;
        return result;
    }

    /**
     * Session
     */
    @RequestMapping("/testsession")
    public String testSession(HttpSession session,User user){

        session.setAttribute("user",user);
        return "show";
    }


    /**
     * 表单映射(包括级联属性)
     * spring mvc 会按请求参数名和pojo属性名进行自动匹配,支持级联属性
     * 使用级联属性时 表单参数形式:address.city
     *
     * http://localhost:8080/testpojo?username=ma&age=21&password=123&email=12@aa&address.city=滨州&address.provice=山东
     */
    @ResponseBody
    @RequestMapping("/testpojo")
    public User testPojo(User user){
        return user;
    }

    /**
     * @RequestParam
     * required参数是否必须
     */
    @ResponseBody
    @RequestMapping("/testrequestparam")
    public String testrequestparam(@RequestParam(value = "username",required = false) String username){
        System.out.println(username);
        return username;
    }

    /**
     * 请求路径通配符
     * 一个?表示一个字符、*表示多个字符
     */
    @ResponseBody
    @RequestMapping(value = "/test/*")
    public String testRequestMapper(){
        return "success";
    }

    /**
     * @PathVariable 可以映射url中的占位符到目标方法
     * 路径中的{id}和@PathVariable注解的参数名需一致
     */
    @ResponseBody
    @RequestMapping(value = "/testpathvariable/{id}")
    public Integer testPathV(@PathVariable("id") Integer id){
        return id;

    }

}

猜你喜欢

转载自blog.csdn.net/qq_37570296/article/details/81051610
今日推荐