В этом уроке мы покажем вам, как разработать Spring MVC на основе аннотаций MultiActionController , используя @RequestMapping
.
В мультиактивационном контроллере на основе XML необходимо настроить распознаватель имен методов ( InternalPathMethodNameResolver , Свойстваметодномер-решатель или ParameterMethodNameResolver ) для сопоставления URL-адреса с определенным именем метода. Но с поддержкой аннотаций жизнь стала проще, теперь вы можете использовать @RequestMapping аннотацию в качестве преобразователя имен методов, который использовался для сопоставления URL-адреса с определенным методом.
Чтобы настроить его, определите @RequestMapping с URL-адресом сопоставления над именем метода.
package com.mkyong.common.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 CustomerController{ @RequestMapping("/customer/add.htm") public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("CustomerAddView"); } @RequestMapping("/customer/delete.htm") public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("CustomerDeleteView"); } @RequestMapping("/customer/update.htm") public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("CustomerUpdateView"); } @RequestMapping("/customer/list.htm") public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("CustomerListView"); } }
Теперь URL-адрес будет сопоставлен с именем метода в следующих шаблонах:
- /customer/add.htm –>добавить() метод
- /клиент/delete.htm –>метод удаления()
- /customer/update.htm –>обновить() метод
- /customer/list.htm –>список() метод
Скачать Исходный Код
Оригинал: “https://mkyong.com/spring-mvc/spring-mvc-multiactioncontroller-annotation-example/”