在我目前的春季项目中,我的控制器和服务类具有相似的结构。我想为它们创建一个基类,它将用作所有其他控制器和服务类的基础。任何人都可以告诉我这是否可能?
这个类的基本结构是(术语<>会有所不同):
控制器
@Controller
@RequestMapping(value="<<name>>")
public class <<name>>Controler {
@Autowired
private <<name>>Service <<name>>;
@RequestMapping(value="cadastra.htm", method=RequestMethod.POST)
@ResponseBody
@PreAuthorize("hasPermission(#user, 'cadastra_<<name>>')")
public String cadastra(HttpServletRequest request, HttpServletResponse response) {
if(<<name>>.cadastra(request, response))
return "yes";
else
return "not";
}
@RequestMapping(value="altera.htm", method=RequestMethod.POST)
@ResponseBody
@PreAuthorize("hasPermission(#user, 'altera_<<name>>')")
public String altera(HttpServletRequest request, HttpServletResponse response) {
if(<<name>>.altera(request, response))
return "yes";
else
return "not";
}
@RequestMapping(value="remove.htm", method=RequestMethod.POST)
@ResponseBody
@PreAuthorize("hasPermission(#user, 'remove_<<name>>')")
public String remove(HttpServletRequest request, HttpServletResponse response) {
if(<<name>>.remove(request, response))
return "yes";
else
return "not";
}
}
服务
@Service
public class <<name>>Service {
@Autowired
private <<name>>Dao <<name>>;
public boolean cadastra(HttpServletRequest request, HttpServletResponse response) {
return <<name>>.persist(null);
}
public boolean altera(HttpServletRequest request, HttpServletResponse response) {
return <<name>>.merge(null);
}
public boolean remove(HttpServletRequest request, HttpServletResponse response) {
return <<name>>.remove(null);
}
}