标签:springmvc
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 0.指定扫描 @Controller,@Component,@Service,@Repository之类Bean的路径--> <context:component-scan base-package="com.billstudy.springmvc"/> <!-- 1.注解映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- 2.注解适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> <!-- 小技巧:以上1、2可以直接使用 <mvc:annotation-driven />,这个配置已经包含上面两个bean配置,所以也能实现支持注解的效果 --> <!-- 3.视图解析器 最终路径为:prefix + Controller内返回逻辑视图名 + suffix 如:方法返回/hello,那么实际路径为:/WEB-INF/jsp/hello.jsp --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp" /> <property name="suffix" value=".jsp"/> </bean> </beans>3.编写Controller类
package com.billstudy.springmvc.controller;
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;
/**
* SpringMvc/Spring 企业开发常用注解使用演示
* @author Bill
* @since V1.0 2015/01/23
*/
/** 标明为Controller类,可以被Spring 扫描到,一般此类注解都作用于类上 ,与此相似的还有:
* @Service : 一般用于MVC设计中M(model)层,也就是业务层
* @Repository : 一般用于DAO层,也就是数据访问层
* @Component : 仅仅表示一个组件 (Bean),比较泛化,当我们写了一个类不好定位其在MVC那层的时候,可以使用这个
* @Controller:一般用于MVC设计中C(Controller)层,也就是控制层
* **/
@Controller
public class AnnotationDemoController{
@RequestMapping("/annotationTest01")/** 定义访问规则 **/
public ModelAndView annotationTest01(HttpServletRequest request,HttpServletResponse response){
ModelAndView result = new ModelAndView();
result.setViewName("/annotationTest01"); // 这里的ViewName,我们把它看成逻辑视图名,最终结合视图解析器,路径为:/WEB-INF/jsp/annotationTest01.jsp
result.addObject("msg", "注解使用成功了!");
return result;
}
}
和大彪一起来学习-SpringMvc之第三回(注解使用详解)
标签:springmvc
原文地址:http://blog.csdn.net/u010811257/article/details/43065377