SpringMVC 注解配置
2013-07-21 20:03
447 查看
在Spring项目开发中呢,最好是搞明白原理,其次装上Spring为eclipse开发的插件,这样会大大提高开发效率,而且减少了大量信息的记忆负担。SpringIDE插件,可自行到eclipse插件库中进行下载,还有其他Spring相关的plugin可以自行研究下。当装好这个插件之后呢,可以根据向导创建一个简单的SpringMVC项目,大量的基本信息都可以自动生成,当然了是建立在明白原理的基础上,熟练了之后再去使用插件。
截个图感觉下,创建Spring项目的向导。
创建好之后的项目目录,
首先看下,web.xml。这里copy自动生成的xml讲解下。
根据xml中的注释也能很容易各个配置的作用。
Context加载器。
SpringMVC前端控制器。相当核心的一个东西,利用了facade设计模式,有兴趣的可以研究下。
SpringMVC中request的生命周期
当每个请求到达前端控制器,前端控制开始根据相应的HandlerMapping配置,发送给Controller,Controller调用logic object或service或domain object处理,然后返回一个视图的逻辑名称和相应的model(数据),给前端控制器,然后前端控制器根据相应的视图解析(ViewResolver)配置,转给相应的视图(譬如jsp页面)。
在spring-context.xml中添加上对注解的支持。
第一个主要是对类级别上的注解处理。
第二个主要是对方法级别上的注解处理。
在spring-context.xml中配置好视图解析器。
配置Spring的组件扫描功能。
当基本的配置工作做好之类,下面举几个例子来说明注解的使用,当然都是很简单一目了然的例子。
一个特定的URL映射到一个控制器类
多映射
截个图感觉下,创建Spring项目的向导。
创建好之后的项目目录,
首先看下,web.xml。这里copy自动生成的xml讲解下。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
根据xml中的注释也能很容易各个配置的作用。
<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param>项目的根容器上下文,用于Servlet和Filter的共享。
<!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Context加载器。
<!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
SpringMVC前端控制器。相当核心的一个东西,利用了facade设计模式,有兴趣的可以研究下。
SpringMVC中request的生命周期
当每个请求到达前端控制器,前端控制开始根据相应的HandlerMapping配置,发送给Controller,Controller调用logic object或service或domain object处理,然后返回一个视图的逻辑名称和相应的model(数据),给前端控制器,然后前端控制器根据相应的视图解析(ViewResolver)配置,转给相应的视图(譬如jsp页面)。
在spring-context.xml中添加上对注解的支持。
<bean name="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean name="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
第一个主要是对类级别上的注解处理。
第二个主要是对方法级别上的注解处理。
在spring-context.xml中配置好视图解析器。
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean>
配置Spring的组件扫描功能。
<context:component-scan base-package="org.changsheng.springmvc" />
当基本的配置工作做好之类,下面举几个例子来说明注解的使用,当然都是很简单一目了然的例子。
一个特定的URL映射到一个控制器类
/** * Handles requests for the application home page. */ @Controller @RequestMapping("/home.jsp") public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); /** * Simply selects the home view to render by returning its name. */ @RequestMapping(method = RequestMethod.GET) public String home(Locale locale, Model model) { logger.info("Welcome home! The client locale is {}.", locale); Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(date); model.addAttribute("serverTime", formattedDate ); return "home"; } }
多映射
@Controller public class ProductController { private ProductService productService; @Autowired public ProductController(ProductService productService){ this.productService = productService; } @RequestMapping("/product/add.jsp") public String addProduct(Product product){ productService.addProduct(product); return "redirect:list.htm"; } @RequestMapping("/product/del.jsp") public String delProduct(@RequestParam("productName") String productName){ productService.delProduct(productName); return "redirect:list.htm"; } @RequestMapping("/product/list.jsp") public ModelAndView listProduct(Product product){ List<Product> products = productService.list(); return new ModelAndView("productList","products",products); } }
相关文章推荐
- 修改零配置的SpringMVC中使用@ResponseBody注解返回json的时间格式
- springmvc 基于注解的事务配置,事务失效
- 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例
- SpringMVC 注解配置
- 配置SpringMVC注解遇到的一些小问题
- SpringMVC注解配置Swagger2步骤及相关知识点
- SpringMVC通过注解@Value获取properties配置
- Springmvc 注解 伪静态(urlrewrite) 配置
- SpringMVC框架学习(1)--执行原理 及 xml注解配置说明
- SpringMVC注解开发基础1---配置
- 七、初学SpringMVC+Mybatis之SpringMVC基于注解的配置
- springMvc 之 【javaBean读取属性配置文件中的值】---非注解方式
- spring mvc 第二天【注解实现springmvc Handler处理ajax简单请求 的配置】
- 天易47----Springmvc注解方式实现aop配置简单示例
- springMvc中获取通过注解获取properties配置文件
- SpringMVC_配置和注解--跟海涛学SpringMVC(和自己在项目中的实际使用的对比)
- springMVC 注解事务配置
- springmvc通过@Value注解读取Properties配置文件的值,junit测试可以取到值,但是在业务中无法读取
- SpringMVC_01 SpringMVC五大组件、SpringMVC编程步骤(不使用注解进行配置)、SpringMVC编程步骤(利用注解进行配置)、参数获取、响应数据
- SpringMVC 全注解配置