zoukankan      html  css  js  c++  java
  • springMVC基于注解的控制器

    springMVC基于注解的控制器

    springMVC基于注解的控制器的优点有两个:

    1、控制器可以处理多个动作,这就允许将相关操作写在一个类中。
    2、控制器的请求映射不需要存储在配置文件中。使用requestMapping注释类型,可以对一个方法进行请求处理。
     
    接下来介绍两个最重要的注释类型:

    一、controller注释类型

    这种注释类型用于指示Spring类的实例是一个实例;
    import org.springframework.stereotype;
    public class CustemerController{
        //methods
    }
    Spring 使用扫描机制来找到应用程序中所有基于注解的控制器类;

    1、配置spring-context

    <beans
    ...
    xmlns:context="http://www.springframework.org/schema/context"
    ...
    >

    2、配置<component-scan/>

    假设所有的控制器类都在com.example.controller及其子包中
    <context:component-scan base-package="com.example.controller"/>
     

    二、RequestMapping注解类型

    1、为每个动作开发相应的处理方法。

    使用org.springframework.web.bind.annotation.RequestMapping注解类型映射URI和方法;

    RequestMapping映射了一个请求和一种方法。可以使用@RequestMapping注解一种方法或类。
    看下面例子:
    @Controller
    public class CustemerController{
        @RequestMapping(value = "/customer_input")
        public String inputCustemer(){
            //do something 
            return "CustemerForm";
    }
    }
    value 属性就是将URI映射到方法。
    value属性是默认的属性,如果只有一个属性,则可省略属性名称;
    @RequestMapping("/customer_input")
    超过一个属性,必须写value属性名称;
     
    除了value属性,还有其他属性,如method属性用来指示该方法处理哪些HTTP方法。
    @RequestMapping(value = "/customer_input",method={RequestMethod.POST,RequestMethod.PUT})
    或者只有一个HTTP方法值,则使用
    @RequestMapping(value = "/customer_input",method=RequestMethod.POST)
     
    RequestMapping注释类型也可用来注释一个控制类
    @Controller
    @RequestMapping(value = "/customer_input")
     public class inputCustemer(){
                               
    @RequestMapping(value = "/delete",method={RequestMethod.POST,RequestMethod.PUT})
    pulic String deleteCustemer(){
            //do something 
            return "CustemerForm";
    }
    }

    2、请求处理方法

    每个请求处理方法可以有多个不同类型的参数,记忆一个多钟类型的返回结果。
    传递参数的方式:
    @RequestMapping(value = "/delete",method={RequestMethod.POST,RequestMethod.PUT})
    pulic String deleteCustemer(HttpSession session){
            //do something 
            return "CustemerForm";//指向了视图(页面)的名称
    }
    参数类型:
     
    请求处理方法的返回类型:
    ModelAndView
    Model
    Map
    View()
    void
    代表逻辑视图名的String(就是上面例子中请求处理方法返回的String类型)
    callable
    等等
     

    3、应用@Autowired和@Service 进行依赖注入

     
    将依赖注入到SpringMVC控制器的最简单方法就是通过注解@Autowired到字段或方法;此外,为了能被作为依赖注入,类必须要注明为@Service ,在配置文件中,还需要添加一个<component-scan/>元素来扫描依赖基本包。
     
    @Autowired
    private ProductService productService
     
    productService是一个提供处理产品方法的接口,将productService的一个实例注入到某类中。
     
     
     
  • 相关阅读:
    IDEA 编译时 未结束的字符串文字
    JAVA文件下载,页面显示另存为效果
    no matching key exchange method found. Their offer: diffie-hellman-group1-sha1
    【转】修改LINUX时间
    【转】tomcat7性能调优
    【转】Nginx中upstream有以下几种方式:
    【转】tomcat性能调优
    【转】Memcached安装
    【转】 linux下的g++编译器安装
    【转】nginx+tomcat+memcached (msm)实现 session同步复制
  • 原文地址:https://www.cnblogs.com/sdgf/p/4946901.html
Copyright © 2011-2022 走看看