Spring framework--SpringMVC--Controller

  Controller (控制器)通过一个服务接口提供了对Web应用功能的调用;控制器解释用户的输入并把它转换成一个模型(model),通过视图(View)展现给用户。Spring实现的控制器采用的是一种抽象的方式,这样使得创建更多不同的控制器成为可能。Spring从2.5版本开始为Controller引入了基于注解的编程模型(比如:@Controller,@RequestMapping,@RequestParam,@ModelAttribute等等),这些注解支持在ServletMVC,PortletMVC框架下也是可用的。这种基于注解方式实现的控制器不需要继承特定的基类,也不需要实现特定的接口,甚至不需要依赖于Servlet和Portlet API,但是我们可以容易地配置它来访问Servlet和Portlet的功能。很多web应用都在使用这种基于注解的方式,比如:MvcShowcase, MvcAjax, MvcBasic, PetClinic, PetCare, and others.

1、使用@Controller注解定义一个控制器

  使用了@Controller注解的类,表示这个类充当的是一个控制器,DispatcherServlet会扫描含有这个注解的类,并且检测映射方法。你可以在DispatcherServlet的上下文中像配置普通的bean一样显示地配置这些Controller,也可以使用Spring通用的自动检测bean功能。要使用这种自动检测bean的功能,需要在配置文件中添加自动组件扫描功能,方式是使用spring-context Schema,如:

<?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:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
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">
    <context:component-scan base-package="org.springframework.samples.petclinic.web"/>
<!-- ... -->
</beans>

2、使用@RequestMapping映射请求

  我们一般使用@RequestMapping注解来把URLs(如:/home)映射到整个类或者类中的一个处理方法上。特别地,类级别的注解是把一个请求路径(或者路径模式)映射到一个表单控制器,然后方法级别的注解把这个主映射限制到某一个具体的http请求方法(“GET”,“POST”,etc)或者http请求参数条件。如:

@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
private final AppointmentBook appointmentBook;
@Autowired
public AppointmentsController(AppointmentBook appointmentBook) {
this.appointmentBook = appointmentBook;
}
@RequestMapping(method = RequestMethod.GET)
public Map<String, Appointment> get() {
return appointmentBook.getAppointmentsForToday();
}
@RequestMapping(value="/{day}", method = RequestMethod.GET)
public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE)
Date day, Model model) {
return appointmentBook.getAppointmentsForDay(day);
}
@RequestMapping(value="/new", method = RequestMethod.GET)
public AppointmentForm getNewForm() {
return new AppointmentForm();
}
@RequestMapping(method = RequestMethod.POST)
public String add(@Valid AppointmentForm appointment, BindingResult result) {
if (result.hasErrors()) {
return "appointments/new";
}
appointmentBook.addAppointment(appointment);
return "redirect:/appointments";
}
}

  使用类级别注解,表示在这个控制器里的所有方法注解的映射URL路径都是相对于类上面的路径,如果类上没有url路径,则方法上的url路径都是绝对路径。

  在有些情况下,@RequestMapping注解需要放到接口方法上,比如在使用基于接口的JDK代理时。但是方法参数的注解(如:RequestParam)是必须要放在控制器类的方法里的。

 

Spring framework--SpringMVC--Controller,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。