zoukankan      html  css  js  c++  java
  • SpringMVC框架的注解如何使用?

    SpringMVC 注解的方式

    • @Controller
    • @RequestMapping
    • @SessionAttributes

    案例实操

    @Controller 控制器定义

    在 spring 3.0 中,通过@controller 标注即可将 class 定义为一个 controller 类。为使 springMVC 能找到定义为 controller 的 bean,需要在 servlet-context 配置文件中增加

    如下定义:

    <context:component-scan base-package="com.xxx.controller"/>
    

    注:实际上,使用@Component,也可以起到@Controller 同样的作用。

    @RequestMapping

    在类前面定义,则将 url 和类绑定。

    在方法前面定义,则将 url 和类的方法绑定

    //url: http://localhost:8080/springmvc01/hello2/test01.do 
    
    @RequestMapping("test01") 
    
    public ModelAndView test01(){ 
    
    	ModelAndView mv=new ModelAndView(); 
    
    	mv.setViewName("hello"); 
    
    	mv.addObject("hello", "hello test01"); 
    
    	return mv; 
    
    } 
    
    //url: http://localhost:8080/springmvc01/hello2.do?test01 
    
    @RequestMapping(params="test02") 
    
    public ModelAndView test02(){ 
    
    	ModelAndView mv=new ModelAndView(); 
    
    	mv.setViewName("hello"); 
    
    	mv.addObject("hello", "hello test02"); 
    
    	return mv; 
    
    } 
    
    //url: http://localhost:8080/springmvc01/hello2/test03.do 
    
    @RequestMapping("test03") 
    
        public String test03(Model model){ 
    
        model.addAttribute("hello", "hello test03"); 
    
        return "hello"; 
    
    } 
    
    //url: http://localhost:8080/springmvc01/hello2/test04.do 
    
    @RequestMapping("test04") 
    
    public String test04(ModelMap modelMap){ 
    
        modelMap.addAttribute("hello", "hello test04"); 
    
        return "hello"; 
    
    } 
    
    //url: http://localhost:8080/springmvc01/hello2/test05.do 
    
    @SuppressWarnings("unchecked") 
    
    @RequestMapping("test05") 
    
    public String test05(Map model){ 
    
        model.put("hello", "hello test05 "); 
    
        return "hello"; 
    
    } 
    

    @SessionAttributes

    用于声明 session 级别存储的属性,放置在处理器类上(了解)

    @Controller 
    
    @SessionAttributes({"userName"})// userName 放入 session 
    
    public class UserController { 
    
        @RequestMapping("/queryUser") 
    
        public ModelAndView queryUser(String userName){ 
    
            ModelAndView mv=new ModelAndView(); 
    
            mv.addObject("userName", userName); 
    
            mv.setViewName("user"); 
    
            return mv; 
    
    	} 
    
    } 
    

    页面取值

    <body> 
    
    	${sessionScope.a}|||${sessionScope.b} 
    
    </body> 
    

    扩展~常见 MVC 框架比较

    运行性能上:

    Jsp+servlet>struts1>spring mvc>struts2+freemarker>struts2,ognl, 值栈。

    开发效率上,基本正好相反。值得强调的是,spring mvc 开发效率和 struts2 不相上下,但从目前来看,spring mvc 的流行度已远远超过 struts2。

    let>struts1>spring mvc>struts2+freemarker>struts2,ognl, 值栈。

    开发效率上,基本正好相反。值得强调的是,spring mvc 开发效率和 struts2 不相上下,但从目前来看,spring mvc 的流行度已远远超过 struts2。

    Struts2 的性能低的原因是因为 OGNL(一种表达式语言,通过它简单一致 的表达式语法,可以存取对象的任意属性,调用对象的方法,结合 struts2 框架使用)和值栈(简单理解为存放 struts2 action 的堆栈)造成的。所以,如果系统并发量高,可以使用 freemaker 进行显示,而不是采用 OGNL 和值栈。这样,在性能上会有相当大得提高。

  • 相关阅读:
    js canvas登陆验证
    媒体查询
    js读取excel中日期格式转换问题
    jquery获取元素对应高度
    js引用类型的赋值
    asp.net core mvc视频A:笔记2-4.ActionResult(动作结果,即返回值)
    asp.net core mvc视频A:笔记2-3.高级数据绑定
    asp.net core mvc视频A:笔记2-2.接收数据
    asp.net core mvc视频A:笔记2-1.控制器定义
    asp.net core mvc视频A:笔记1.基本概念介绍
  • 原文地址:https://www.cnblogs.com/lezijie/p/13596580.html
Copyright © 2011-2022 走看看