【发布时间】:2017-04-06 13:54:36
【问题描述】:
我的带有 roo 生成 CRUD 的项目工作正常,但现在我需要更改一些实体的保存方式(例如,我有一个“用户”属性,我想在用户登录时动态设置)
目前我只是将 save() 方法从方面移动到 .java 并根据需要对其进行修改。到目前为止它运行良好,但 roo 控制台似乎不喜欢它,因为一旦我更改方法返回类型或其他内容,它就会在方面重新创建方法。
我不需要对此示例的具体答案,而是想知道这是否是修改/覆盖 roo 提供的实体的开箱即用创建/显示功能的最佳方法.
编辑:添加示例
我的一个实体是 "Servicio",它有一些 "ServiciosCollectionThymeleafController_Roo_Thymeleaf.aj" 和一个 "ServiciosCollectionThymeleafController.create" 方法。我继续将所有 .aj 推入 "ServiciosCollectionThymeleafController.java"
然后我在 create 方法中做了一些小改动并保存了它。它有效,但是当我打开 roo 控制台时,控制台再次生成了推送的 aj,只是使用我之前编辑的方法。
方面的原始创建方法:
/**
* TODO Auto-generated method documentation
*
* @param servicio
* @param result
* @param model
* @return ModelAndView
*/
@PostMapping(name = "create")
public ModelAndView ServiciosCollectionThymeleafController.create(@Valid @ModelAttribute Servicio servicio, BindingResult result, Model model) {
if (result.hasErrors()) {
populateForm(model);
return new ModelAndView("/servicios/create");
}
Servicio newServicio = getServicioService().save(servicio);
UriComponents showURI = getItemLink().to(ServiciosItemThymeleafLinkFactory.SHOW).with("servicio", newServicio.getId()).toUri();
return new ModelAndView("redirect:" + showURI.toUriString());
}
将相同的方法推入 .java,以及我的修改:
/**
* TODO Auto-generated method documentation
*
* @param servicio
* @param result
* @param model
* @return ModelAndView
*/
@PostMapping(name = "create")
public ModelAndView create(@Valid @ModelAttribute Servicio servicio, BindingResult result, Model model, Principal principal, Pageable pageable) {
if (result.hasErrors()) {
populateForm(model);
return new ModelAndView("/servicios/create");
}
Prestador current = (Prestador) personaService.findByUsername(principal.getName(), pageable).getContent().get(0);
if (current == null) {
populateForm(model);
return new ModelAndView("/servicios/create");
}
servicio.setPrestador(current);
Servicio newServicio = getServicioService().save(servicio);
return new ModelAndView("redirect:/ver-servicio/" + newServicio.getId());
}
谢谢。
【问题讨论】:
标签: java spring spring-roo