1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="cn.happy.*"></context:component-scan> </beans>

01.spring MVC最基本的注解之零散参数自动装配

@Controller
@RequestMapping("/hr")
public class MyController {
@RequestMapping("/hello.do")
public String show(String name,Model model){
System.out.println("=="+name+"==");
model.addAttribute("msg",name+"展示页面");
return "happy";
}
}

其中,方法中的参数与界面表单的name属性并且和实体类中的字段name保持一直("三者合一"),Model类型代替了ModelAndView的用法去装载数据然后直接return到jsp界面,

        如果直接像图中返回happy那样就需要在配置文件中添加一个视图解析器

     <!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

如果界面中的属性不一致,则需用到注解@RequestParam来进行指明

@RequestMapping(value="/list.do",method=RequestMethod.POST)
public String list(Model model,@RequestParam(value="uname",required=false) String name){
System.out.println("=="+name);
return "happy";
}

       02.spring MVC注解之参数以对象、作用域、map、泛型作为传输数据

//装配对象类型
@RequestMapping(value="/list.do",method=RequestMethod.POST)
public String list(Model model,UserInfo info){ System.out.println("===="+info.getUname()+"\t地址"+info.getRadd().getAdd()+"\t图书1"+info.getBooklist().get(0).getBookname());
model.addAttribute("uname", info.getUname());
return "index";
}

 03.获取请求的地址栏中的属性参数值

@Controller
public class HandleReturn {
/*
* 获取地址栏中的属性值
*/
@RequestMapping("/{rname}/{age}/first.do")
public String handlereturn(Model model,@PathVariable("rname") String name,@PathVariable int age){
System.out.println(name+"==="+age);
model.addAttribute("name", name);
model.addAttribute("age", age);
return "handle";
}
}

其中,如果地址栏中的属性名称与方法参数名不一致,就通过如代码所示的注解@PathVariable来指明地址栏中的属性名称rname与name关系

  

      04.spring MVC注解之返回值void、Object、string

@Controller
public class HandleAjax { @RequestMapping("/ajax.do")
public void handleAjax(HttpServletResponse response) throws Exception{
//虚拟出一些数据
Map<String, UserInfo> map=new HashMap<String,UserInfo>();
UserInfo u1=new UserInfo();
u1.setAge(12);
u1.setName("恭喜就业"); UserInfo u2=new UserInfo();
u2.setAge(122);
u2.setName("顺利就业"); map.put("001",u1);
map.put("001",u2); //工具 map----json字符串 fastjson
String jsonString = JSON.toJSONString(map);
response.setCharacterEncoding("utf-8");
//响应流
response.getWriter().write(jsonString);
response.getWriter().close();
}
}
/*
* Object返回值类型代替其他类型
*/
@Controller
public class HandleAjaxObject { @RequestMapping("/num.do")
@ResponseBody
public Object number() {
return 1;
} @RequestMapping(value = "/nums.do", produces = "text/html;charset=utf-8")
@ResponseBody
public Object numberS() {
return "汉字";
} // 处理器方法-----UserInfo
@RequestMapping(value = "/third.do")
@ResponseBody
public Object doThird() {
UserInfo info = new UserInfo();
info.setAge(12);
info.setName("Happy");
return info;
}

使用Object作为返回值需要用到注解@ResponseBody来将数据回传到jsp界面

----js

<script type="text/javascript">
$(function(){
$("#btn").click(function(){ $.ajax({
url:"nums.do",
success:function(data){ //data指的是从server打印到浏览器的数据
alert(data)
}
});
});
});
</script> -----body <input type="button" id="btn" value="Ajax"/>

使用void返回值返回json数据回传到jsp界面

<script type="text/javascript">
$(function(){
$("#btn").click(function(){
$.ajax({
url:"ajax.do",
success:function(data){ //data指的是从server打印到浏览器的数据
//jsonString jsonObject
//{"001":{"age":122,"name":"顺利就业"}}
var result= eval("("+data+")");
$.each(result,function(i,dom){
alert(dom.age)
});
}
});
});
});
</script> <input type="button" id="btn" value="Ajax"/>

最后就是String类型的了,不过跟void类型的返回值很相似

     使用String作为返回值需要用到注解@ResponseBody来支持json数据回传到jsp界面

/*
* String返回值类型代替其他类型
*/
@Controller
public class HandleAjax {
@RequestMapping("/ajax.do")
public void handleAjax(HttpServletResponse response) throws Exception{
//伪造数据
Map<String, UserInfo> map=new HashMap<String,UserInfo>();
UserInfo u1=new UserInfo();
u1.setAge(12);
u1.setName("恭喜就业"); UserInfo u2=new UserInfo();
u2.setAge(122);
u2.setName("顺利就业"); map.put("001",u1);
map.put("001",u2); //工具 map----json字符串 fastjson
String jsonString = JSON.toJSONString(map);
response.setCharacterEncoding("utf-8");
return jsonString ;
}

05.重定向到另一个方法去

/*
* 转发与重定向
* 重定向到另一个方法
*/
@Controller
public class Dispatchreturn { /*
* 重定向到另一个方法dsipatch.do
*/
@RequestMapping(value="add.do")
public String AddAllInfo(){
return "redirect:dolist.do";
} @RequestMapping(value="dolist.do")
public String doList(){
return "redirect:/list.jsp";
}
}

    注意:"/"可写可不写

Spring MVC注解的一些案列的更多相关文章

  1. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  2. spring mvc 注解入门示例

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  3. spring mvc 注解示例

    springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  4. 关于Spring mvc注解中的定时任务的配置

    关于spring mvc注解定时任务配置 简单的记载:避免自己忘记,不是很确定我理解的是否正确.有错误地方望请大家指出. 1,定时方法执行配置: (1)在applicationContext.xml中 ...

  5. spring mvc 注解@Controller @RequestMapping @Resource的详细例子

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  6. spring mvc 注解 学习笔记(一)

    以前接触过spring,但是没有接触spring mvc 以及注解的应用,特习之,记之: 注解了解 @Component 是通用标注, @Controller 标注web控制器, @Service 标 ...

  7. Spring MVC 注解[转]

    [学习笔记]基于注解的spring3.0.x MVC学习笔记(九) 摘要: 本章节,仅为@SessionAttributes的功能扩展介绍介绍,结合@requestparam注解进行简易无数据库分页. ...

  8. junit4测试 Spring MVC注解方式

    本人使用的为junit4进行测试 spring-servlet.xml中使用的为注解扫描的方式 <?xml version="1.0" encoding="UTF- ...

  9. [转]spring mvc注解方式实现向导式跳转页面

    由于项目需要用到向导式的跳转页面效果,本项目又是用spring mvc实现的,刚开始想到用spring 的webflow,不过webflow太过笨重,对于我们不是很复杂的跳转来说好像有种“杀鸡焉用牛刀 ...

随机推荐

  1. spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )

    在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.or ...

  2. MySQL NoInstall 配置

    1.下载 mysql-mysql-5.1.55-win32.zip 2. 解压缩到任何一个目录,最好目录名称不要有空格: 例如:C:\mysql 3. 删除Embedded,include,lib,m ...

  3. November 2nd Week 45th Wednesday 2016

    If your ship doesn't come in, swim out to it. 如果你的船不驶进来,那你就朝他游过去吧! Swim out to it, don't fear that y ...

  4. 如何编写稳定流畅的iOS移动端应用

    原文链接:http://www.jianshu.com/p/f4adce56166f 不忘初心 在过去几年间,移动应用以雷霆之势席卷全球.我们在工作和休闲时间中使用互联网的方式,已经随着移动应用的前进 ...

  5. Python中内置数据类型list,tuple,dict,set的区别和用法

    Python中内置数据类型list,tuple,dict,set的区别和用法 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, ...

  6. 【转】理解 PHP 依赖注入 | Laravel IoC容器

    Laravel框架的依赖注入确实很强大,并且通过容器实现依赖注入可以有选择性的加载需要的服务,减少初始化框架的开销,下面是我在网上看到的一个帖子,写的很好拿来与大家分享,文章从开始按照传统的类设计数据 ...

  7. html常用标签介绍

    常用标签介绍 文本 最常用的标签可能是<font>了,它用于改变字体,字号,文字颜色. 点击查看效果 <font size="6">6</font&g ...

  8. taskkill批量删除进程命令

    本人自用: TASKKILL /F /IM notepad --强制删除进程名中带notepad的所有进程 TASKKILL [/S system [/U username [/P [password ...

  9. Golang 语法学习笔记

    Golang 语法学习笔记 包.变量和函数. 包 每个 Go 程序都是由包组成的. 程序运行的入口是包 main. 包名与导入路径的最后一个目录一致."math/rand" 包由 ...

  10. 数据分析师的福音——VS 2017带来一体化的数据分析开发环境

    (此文章同时发表在本人微信公众号“dotNET开发经验谈”,欢迎右边二维码来关注.) 题记:在上个月的Connect() 2016大会上,微软宣布了VS 2017 RC的发布,其中为数据分析师带来了一 ...