Spring MVC学习二

今天学习SpringMVC的注解配置,发现注解的确比XML配置方便简洁多了,现将SpringMvc的具体注解配置说明一下。

1、添加SpringMVC注解功能

添加SpringMVC有两种方式:

a、手工注入开启注解的具体类

<!-- 注解扫描包 -->
<context:component-scan base-package="com.springmvc.controller.annotation"></context:component-scan>
<!-- 开启注解 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>

b、使用springmvc标签

<mvc:annotation-driven/>
<!-- 注解扫描包 -->
<context:component-scan base-package="com.springmvc.controller.annotation"></context:component-scan>

2、使用注解后的Controller类

package com.springmvc.controller.annotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {
	@RequestMapping("/add.do")
	public ModelAndView addUser(){
		return new ModelAndView("user","method","add");
	}
	@RequestMapping("/delete.do")
	public ModelAndView deleteUser(){
		return new ModelAndView("user","method","delete");
	}
	@RequestMapping("/update.do")
	public ModelAndView updateUser(){
		return new ModelAndView("user","method","update");
	}
	@RequestMapping("/toUser.do")
	//只进行跳转
	public String toUser(HttpServletRequest request, HttpServletResponse response){
		System.out.println("跳转...");
		request.setAttribute("method", "跳转");
		return "user";
	}
}


更多文章见:http://www.16boke.com

你可能感兴趣的:(spring,mvc,springMVC)