基于注解的控制器

  • SpringMVC是一个基于DispatcherServlet的MVC框架,每个请求最先访问的是DispatcherServlet,DispatcherServlet负责将每一个Request转发到相应的Handler,Handler处理后再返回相应的模型(Model)和视图(View)。在使用注解的Spring MVC中,处理器Handler是基于@Controller和@RequestMapping两个注解的,这两个注解可以提供非常灵活的匹配和处理方式。
@Controller和@RequestMapping注解
  • @Controller注解类型

  声明一个控制器类,Spring使用扫描机制来找到应用程序中所有基于注解的控制器类,控制器类的内部包含每个动作相应的处理方法,如下是一个@Controller的例子。

package com.example.controller;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;
...
@Controller
public class ProductController {
    //request-handling methods here
}

  为了保证Spring能扫描到控制器类,需要完成两个配置,首先要在Spring MVC的配置文件中声明Spring-context,如下所示:

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

  其次需要应用<component-scan/>元素,在该元素中指定控制器类的基本包。例如所有的控制器类都在com.example.controller及其子包下,则该元素如下:

<context:component-scan base-package="com.example.controller"/>

  确保所有控制器都在基本包下,并且不要指定太广泛的基本包,否则会导致Spring MVC扫描无关的包。

  • @RequestMapping注解类型

  该注解类型在控制器类的内部定义每一个动作相应的处理方法,一个采用@RequestMapping注释的方法将成为一个请求处理方法,并由调度程序在接收到对应的URL请求时调用,下面是一个RequestMapping注解方法的控制器类。

package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
...

@Controller
public class ProductController {
    
    @RequestMapping(value="/productInput")
    public String inputProduct(){
        //do something here
        return "ProductForm";
    }
}

  使用RequestMapping注解的value属性将URL映射到方法,在如上的例子中,我们将productInput映射到inputProduct()方法,通过http://localhost:8081/SpringMVC/productInput访问inputProduct方法。RequestMapping方法除了有value属性外,还有method属性,该属性用来指示该方法仅处理哪些http方法,例如,仅当HTTP POST或PUT方法时,调用下面的processOrder方法。

@RequestMapping(value="/order_process", method={RequestMethod.POST, RequestMethod.PUT})
public String processOrder(){
    //do something here
    return "OrderForm";
}

  如果method属性只有一个HTTP方法值,则无需{},直接为method=RequestMethod.POST,如果未指定method属性,则请求处理方法可以处理任意HTTP方法。此外RequestMapping注释类型也可以用来注释一个控制器类,如下所示:

import org.springframework.stereotype.Controller;
...

@Controller
@RequestMapping(value="/customer")
public class CustomerController{
@RequestMapping(value="/delete", method={RequestMethod.POST, RequestMethod.PUT})
public String deleteCustomer(){
//do something here
return ...;
} }

   在这种情况下,所有的方法都将映射为相对于类级别的请求,如例子中的deleteCustomer方法,由于控制器类映射使用"/customer",而deleteCustomer方法映射为"/delete",则需要通过http://localhost:8081/SpringMVC/customer/delete。

应用基于注解的控制器

  本节将通过一个例子说明基于注解的控制器,展示了一个包含两个请求处理方法的控制器类,项目目录结构如下:

Spring MVC---基于注解的控制器 Spring MVC---基于注解的控制器

  我们首先对类进行介绍,Product是产品类,包括产品的名称、描述和价格属性,该类实现了Serializable接口,而ProductForm类与HTML表单相映射,是Product在服务端的代表,表单对象传递ServletRequest给其他组件,还有当数据校验失败时,表单对象将用于保存和展示用户在原始表单上的输入。DBOperator类负责与mysql数据库打交道,ProductService定义了服务层的接口,ProductServiceImpl实现了ProductService中定义的接口。

Product类代码如下,包含属性和属性的set和get方法:

public class Product implements Serializable{
    private static final long serialVersionUID = 1L;
    private String name;
    private String description;
    private double price;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
}
View Code

相关文章:

  • 2022-01-18
  • 2021-08-23
  • 2021-06-13
  • 2021-07-29
  • 2021-09-23
  • 2022-12-23
  • 2022-01-27
猜你喜欢
  • 2022-01-27
  • 2021-05-28
  • 2022-12-23
  • 2022-02-21
  • 2022-01-16
  • 2021-11-19
  • 2021-08-13
相关资源
相似解决方案