xinhudong

上一篇我们结束了配置式开发,配置式开发目前在企业中用的并不是很多,大部分企业都在使用注解式开发,所以今天我们就来学习注解式开发。所谓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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.wechatbao.controller"></context:component-scan>
    
</beans>

 

二、第一个注解式Demo

1:Controller

package cn.wechatbao.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;

@Controller
public class MyController {

    @RequestMapping("/my.do")
    public ModelAndView first(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "第一个注解式开发程序");
        mv.setViewName("/WEB-INF/jsp/welcome.jsp");
        return new ModelAndView("");
    }

}

 

2:JSP页面(welcome.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>SpringMVC1</title>
  </head>
  
  <body>
    ${message }
  </body>
</html>

 

3:完整的项目结构

 

 

三、命名空间的配置

一般情况下,我们开发时,一个Controller类就是一个模块,而里面的所有处理器方法就是该模块的不同业务功能。这个时候,我们Controller与Controller之间就要用路径来区分开来。以表示不同的业务模块。这个时候,只需要在类上再加上@RequestMapping("/test")注解就OK了。

完整的类如下:

package cn.wechatbao.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;

@Controller
@RequestMapping("/test")
public class MyController {

    @RequestMapping("/first.do")
    public ModelAndView first(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "第一个注解式开发程序方法一");
        mv.setViewName("/WEB-INF/jsp/welcome.jsp");
        return mv;
    }
    
    @RequestMapping("/second.do")
    public ModelAndView second(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "第一个注解式开发程序方法二");
        mv.setViewName("/WEB-INF/jsp/welcome.jsp");
        return mv;
    }

}

 

四、请求中通配符的使用

在实际开发的过程中,我们可能会遇到请求中的方法开头或结尾是固定的,其它字符是可变的,比如:

http://localhost:8080/SpringMVC/usermanager/user-add.do

http://localhost:8080/SpringMVC/usermanager/user-edit.do

假设上面URL中usermanager是模块名(也就是我们说的命名空间),user-add.do和user-edit.do是具体的请求。但是添加和修改我们完全可以用一个处理器方法来解决。这个时候用通配符就简单多了。其实配置起来也特别简单,只需要在处理器方法上面的注解里加*就可以了。如下

@RequestMapping("/user-*.do")
public ModelAndView userAddOrUpdate(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    ModelAndView mv = new ModelAndView();
    mv.addObject("message", "用户的添加或修改功能");
    mv.setViewName("/WEB-INF/jsp/welcome.jsp");
    return mv;
}

 

相关文章: