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。

Struts2 的性能低的原因是因为 OGNL(一种表达式语言,通过它简单一致 的表达式语法,可以存取对象的任意属性,调用对象的方法,结合 struts2 框架使用)和值栈(简单理解为存放 struts2 action 的堆栈)造成的。所以,如果系统并发量高,可以使用 freemaker 进行显示,而不是采用 OGNL 和值栈。这样,在性能上会有相当大得提高。 需要视频配套资料或其他资料+我们小姐姐V lezijie007(加好友暗号 -98 ,不备注不加)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值