ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringMVC框架的注解如何使用?

2020-09-01 17:51:01  阅读:216  来源: 互联网

标签:RequestMapping 框架 SpringMVC spring struts2 mv ModelAndView 注解 hello


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 和值栈。这样,在性能上会有相当大得提高。

标签:RequestMapping,框架,SpringMVC,spring,struts2,mv,ModelAndView,注解,hello
来源: https://blog.51cto.com/14816480/2526764

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有