一、SpringMVC注解入门

1. 创建web项目
2. 在springmvc的配置文件中指定注解驱动,配置扫描器

<!-- mvc的注解驱动 -->
<mvc:annotation-driven />

<!--只要定义了扫描器,注解驱动就不需要,扫描器已经有了注解驱动的功能 -->
<context:component-scan base-package="org.study1.mvc.controller" />

<!-- 前缀+ viewName +后缀 -->
<bean
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<!-- WebContent(WebRoot)到某一指定的文件夹的路径 ,如下表示/WEB-INF/view/*.jsp -->
	<property name="prefix" value="/WEB-INF/view/"></property>
	<!-- 视图名称的后缀 -->
	<property name="suffix" value=".jsp"></property>
</bean>

<context:component-scan/> 扫描指定的包中的类上的注解,常用的注解有:

@Controller 声明Action组件
@Service    声明Service组件    @Service("myMovieLister") 
@Repository 声明Dao组件
@Component   泛指组件, 当不好归类时. 
@RequestMapping("/menu")  请求映射
@Resource  用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName") 
@Autowired 用于注入,(srping提供的) 默认按类型装配 
@Transactional( rollbackFor={Exception.class}) 事务管理
@ResponseBody
@Scope("prototype")   设定bean的作用

3. @controller:标识当前类是控制层的一个具体的实现
4. @requestMapping:放在方法上面用来指定某个方法的路径,当它放在类上的时候相当于命名空间需要组合方法上的requestmapping来访问。

@Controller // 用来标注当前类是springmvc的控制层的类
@RequestMapping("/test") // RequestMapping表示 该控制器的唯一标识或者命名空间
public class TestController {

	/**
	 * 方法的返回值是ModelAndView中的
	 */
	@RequestMapping("/hello.do") // 用来访问控制层的方法的注解
	public String hello() {
		System.out.println("springmvc annotation... ");
		return "jsp1/index";
	}

	//*****
}

在本例中,项目部署名为mvc,tomcat url为 http://localhost,所以实际为:http://localhos/mvc

在本例中,因为有命名空间 /test,所以请求hello方法地址为:http://localhost/mvc/test/hello.do

输出:springmvc annotation...

二、注解形式的参数接收

1. HttpServletRequest可以直接定义在参数的列表,通过该请求可以传递参数

url:http://localhost/mvc/test/toPerson.do?name=zhangsan

/**
	 * HttpServletRequest可以直接定义在参数的列表,
	 * 
	 */
	@RequestMapping("/toPerson.do")
	public String toPerson(HttpServletRequest request) {
		String result = request.getParameter("name");
		System.out.println(result);
		return "jsp1/index";
	}

 可以从HttpServletRequest 取出“name”属性,然后进行操作!如上,可以取出 “name=zhangsan”

输出:zhangsan
2. 在参数列表上直接定义要接收的参数名称,只要参数名称能匹配的上就能接收所传过来的数据, 可以自动转换成参数列表里面的类型,注意的是值与类型之间是可以转换的

2.1传递多种不同类型的参数:

url:http://localhost/mvc/test/toPerson1.do?name=zhangsan&age=14&address=china&birthday=2000-2-11

/**
	 * 传递的参数的名字必须要与实体类的属性set方法后面的字符串匹配的上才能接收到参数,首字符的大小写不区分
	 * 请求中传的参数只要是能和参数列表里面的变量名或者实体里面的set后面的字符串匹配的上就能接收到 a
	 * 
	 */
	@RequestMapping("/toPerson1.do")
	public String toPerson1(String name, Integer age, String address,
              Date birthday) {
            System.out.println(name + " " + age + " " + address + " " + birthday);
            return "jsp1/index";
	}
	
	/**
	 * 注册时间类型的属性编辑器,将String转化为Date
	 */
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				new SimpleDateFormat("yyyy-MM-dd"), true));
	}

输出:zhangsan 14 china Fri Feb 11 00:00:00 CST 2000

2.2传递数组:

url:http://localhost/mvc/test/toPerson2.do?name=tom&name=jack  

/**
	 * 对数组的接收,定义为同名即可 
	 */
	@RequestMapping("/toPerson2.do")
	public String toPerson2(String[] name) {
		for (String result : name) {
			System.out.println(result);
		}
		return "jsp1/index";
	}

 输出:tom jack

2.3传递自定义对象(可多个):

url:http://localhost/mvc/test/toPerson3.do?name=zhangsan&age=14&address=china&birthday=2000-2-11

 User 定义的属性有:name,age,并且有各自属性的对应的set方法以及toString方法

 Person定义的属性有:name,age.address,birthday,并且有各自属性的对应的set方法以及toString方法

/**
	 * 
	 * 传递的参数的名字必须要与实体类的属性set方法后面的字符串匹配的上才能接收到参数,首字符的大小写不区分
	 * 请求中传的参数只要是能和参数列表里面的变量名或者实体里面的set后面的字符串匹配的上就能接收到 
	 * 
	 */
	@RequestMapping("/toPerson3.do")
	public String toPerson3(Person person, User user) {
		System.out.println(person);
		System.out.println(user);
		return "jsp1/index";
	}

  输出:

Person [name=zhangsan, age=14, address=china, birthday=Fri Feb 11 00:00:00 CST 2000]
User [name=zhangsan, age=14] 

自动封装了对象,并且被分别注入进来!

三、注解形式的结果返回

 1. 数据写到页面,方法的返回值采用ModelAndView, new ModelAndView("index", map);,相当于把结果数据放到response里面

url:http://localhost/mvc/test/toPerson41.do

url:http://localhost/mvc/test/toPerson42.do

url:http://localhost/mvc/test/toPerson43.do

url:http://localhost/mvc/test/toPerson44.do