gpt4 book ai didi

java - 如何将逻辑与 Controller 分离并使用注释将其放入某些处理程序?

转载 作者:行者123 更新时间:2023-12-02 12:46:29 25 4
gpt4 key购买 nike

在我们的项目中,我们已经有了一些必要的 Controller ,我的任务是让它变得更容易。它应该像这样工作:我只需在 Controller 下放置一个注释,然后处理程序完成其所有工作。我已经拥有的:

/**
* This annotation marks collector methods
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Collect {

}

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Collectors {

/**
* An array of subclasses that can load and provide values for
* generating a ModelMap.
*/
Class<?>[] value();
}

我找不到任何示例。 Controller 示例:

@Controller
public class TestController {
private final SchoolService schoolService;

private final TeacherService teacherService;


public TestController(SchoolService schoolService, TeacherService teacherService) {
this.schoolService = schoolService;
this.teacherService = teacherService;
}

/**
* Saves the static list of users in model and renders it
* via freemarker template.
*
* @param model
* @return The index view (FTL)
*/
@Collectors(SchoolCollector.class)
@RequestMapping(value = "freemarker/freemarkertest", method = RequestMethod.GET)
public String index(@ModelAttribute("model") ModelMap model) {
List<SchoolDTO> schoolList = new ArrayList<SchoolDTO>();
schoolList = schoolService.findAll();
model.addAttribute("schoolList", schoolList);
return "freemarkertest";
}

/**
* Add a new School
*
* @param schoolDTO
* @return Redirect back to same /freemarkertest page to display school list, if successful
*/
@Collectors(SchoolCollector.class)
@RequestMapping(value = "freemarker/freemarkertest/add", method = RequestMethod.POST)
public String add(@ModelAttribute("schoolDTO") SchoolDTO schoolDTO) {
if(schoolDTO.getName() != null && !schoolDTO.getName().isEmpty() &&
schoolDTO.getEnabled() != null) {
schoolService.save(schoolDTO);
return "redirect:";
} else {
return "redirect:error"; //TODO: create error page
}

}

/**
* Get list of teachers.
*
* @param model
* @param schoolId
* @return The index view (FTL)
*/
@Collectors(SchoolCollector.class)
@RequestMapping(value = "freemarker/teachers/{schoolId}", method = RequestMethod.GET)
public String index(@ModelAttribute("model") ModelMap model, @PathVariable Long schoolId) {
List<TeacherDTO> teachers = teacherService.getAllBySchoolId(schoolId);
model.addAttribute("teachersList", teachers);
model.addAttribute("schoolId", schoolId);
return "teachers";
}

}

最佳答案

您需要的是 HandlerInterceptorAdapters。这些必须在您的 SpringMvc-servlet.xml 中定义,否则它们将拦截您的所有请求。

public class CollectorHandler extends HandlerInterceptorAdapter {

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if (handler instanceof HandlerMethod && modelAndView != null) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (handlerMethod.getMethodAnnotation(Collectors.class) == null) {
return;
}

/**
* Your logic here
*/
}
}
}

进一步阅读:

PS:我还想补充一点,您的代码将无法工作,因为其中有一些“错误”...返回字符串而不是对象作为示例

关于java - 如何将逻辑与 Controller 分离并使用注释将其放入某些处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44747636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com