• controller大全(推荐)


    @Controller
    @RequestMapping("/router")
    @SessionAttributes(value = { "username" })
    public class RouterController {
        @RequestMapping(value = "/index", params = { "username", "age!=18" })
        public String index() {
            return "index";
        }
    
        // 通配符的使用
        @RequestMapping(value = "/testantpath/*/abc")
        public String testantpath() {
            return "index";
        }
    
        // pathVariable
        @RequestMapping(value = "/testpathvariable/{userId}")
        public String testpathvariable(
                @PathVariable(value = "userId") Integer userId) {
            System.out.println(userId);
            return "index";
        }
    
        // RESTFUL
        @RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
        public String testgetmethod(@PathVariable(value = "userId") Integer userId) {
            System.out.println("GET:" + userId);
            return "index";
        }
    
        @RequestMapping(value = "/user/{userId}", method = RequestMethod.POST)
        public String testPostmethod(@PathVariable(value = "userId") Integer userId) {
            System.out.println("POST:" + userId);
            return "index";
        }
    
        @RequestMapping(value = "/user/{userId}", method = RequestMethod.PUT)
        public String testputmethod(@PathVariable(value = "userId") Integer userId) {
            System.out.println("PUT:" + userId);
            return "index";
        }
    
        @RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
        public String testdeletemethod(
                @PathVariable(value = "userId") Integer userId) {
            System.out.println("DELETE:" + userId);
            return "index";
        }
    
        // @RequestParam
        @RequestMapping(value = "/tesrequestparam")
        public String tesrequestparam(
                @RequestParam(value = "username", required = false) String username) {
            System.out.println(username);
            return "index";
        }
    
        // @RequestHeader
        @RequestMapping(value = "/testrequestheader")
        public String testrequestheader(
                @RequestHeader(value = "Accept-Language") String al) {
            System.out.println(al);
            return "index";
        }
    
        // @CookieValue
        @RequestMapping(value = "/testcookievalue")
        public String testcookievalue(
                @CookieValue(value = "JSESSIONID") String cookie) {
            System.out.println(cookie);
            return "index";
        }
    
        /*
         * pojo的 获取
         */
        @RequestMapping(value = "/testuser")
        public String testpojo(TUser user) {
            System.out.println(user);
            // 数据库的操作
            return "index";
        }
    
        /*
         * ModelAndView
         */
        @RequestMapping(value = "/testmodelandview")
        public ModelAndView testmodelandview() {
            ModelAndView modelAndView = new ModelAndView("index");
            modelAndView.addObject("username", "lisi");
            return modelAndView;
        }
    
        /*
         * Map
         */
        @RequestMapping(value = "/testmap")
        public String testmap(Map<String, Object> map) {
            map.put("age", 18);
            return "index";
        }
    
        /*
         * Model
         */
        @RequestMapping(value = "/testmodel")
        public String testModel(Model model) {
            model.addAttribute("email", "1@1.com");
            return "index";
        }
    
        /*
         * @SessionAttributes
         */
        @RequestMapping(value = "/testsession")
        public String testsession(Model model) {
            model.addAttribute("username", "wangwu");
            return "index";
        }
    
        /*
         * @ModelAttribute
         */
        @ModelAttribute
        public void model(
                @RequestParam(value = "userId", required = false) Integer userId,
                Model model) {
            if (userId != null) {
                // 从数据库获取对象
                TUser user = new TUser(userId, "wangwu", "123456", "1@1.com", 18);
                model.addAttribute("user", user);
            }
        }
    
        @RequestMapping(value = "/testmodelattribute")
        public String testmodelattribute(@ModelAttribute(value = "user") TUser user) {
            System.out.println(user);
            // 数据库的操作
            return "index";
        }
    
        /*
         * 获取JSON数据
         */
        @ResponseBody
        @RequestMapping(value = "/testjson")
        public TUser testjson() {
            TUser user = new TUser(5, "wangwu", "123456", "1@1.com", 18);
            return user;
    
        }
    
        /*
         * 文件上传
         */
        @RequestMapping(value = "/testupload")
        public void upload(@RequestParam(name = "file1") MultipartFile file) {
            File newFile = new File("E:\" + file.getOriginalFilename());
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            try {
                file.transferTo(newFile);
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        /*
         * servlet api
         */
        @RequestMapping(value = "/testhttpservletrequest")
        public String testHttpServletRequest(HttpServletRequest request) {
            request.setAttribute("username", "admin");
            return "index";
        }
    
        /*
         * redirect
         */
        @RequestMapping(value = "/testredirect")
        public String testRedirect() {
            return "redirect:/router/testhttpservletrequest";
        }
    
        /*
         * testValidator
         */
        @RequestMapping(value = "/testvalidator")
        public String testValidator(@Valid TUser user, BindingResult result,
                HttpServletRequest request) {
            if (result.hasErrors()) {
                List<FieldError> fieldErrors = result.getFieldErrors();
                for (FieldError fieldError : fieldErrors) {
                    request.setAttribute("_error" + fieldError.getField(),
                            fieldError.getDefaultMessage());
                }
            }
            return "index";
        }
    
    }
  • 相关阅读:
    高性能网络编程(七):到底什么是高并发?一文即懂!
    社交软件红包技术解密(十一):最全解密微信红包随机算法(含代码实现)
    sonar集成阿里p3c代码规范
    jenkins+sonar 持续集成检测代码质量
    JavaScript 利用零宽断言批量替换为注释符 // 后面加空格
    Git 合并时 submodule 冲突问题解决方法
    小程序 iphone X 1rpx 边框透明及不显示问题
    加快pip下载速度
    python中remove函数的坑
    Java程序运行内存机制
  • 原文地址:https://www.cnblogs.com/ScvQ/p/6929302.html
Copyright © 2020-2023  润新知