通用控制器/服务类中注释@ModelAtribute 和@PreAutorize 的属性 [英] Atribute for annotations @ModelAtribute and @PreAutorize in Generic Controller / Service classes
问题描述
我正在尝试为我的项目设置一个通用类.到目前为止,我得到了这个代码:
I am trying setting a generic class for my project. So far I get this code:
此刻,我坚持使用这种方法:
In this moment, I am stuck with this method:
@RequestMapping(value="cadastra", method=RequestMethod.POST)
@ResponseBody
public String cadastra(@ModelAttribute("") E object, BindingResult result) {
if(service.cadastra(object))
return "yes";
else
return "not";
}
注解ModelAtribute的参数应该是我项目中实体类的名称,我已经存储在类成员entity_class中,但是不能使用,因为注解只接受常量值.
the parameter for the annotation ModelAtribute should be the name of an entity class from my project, which I have stored in the class member entity_class, but can't use because the annotation only accept constant values.
此外,我对注释 PreAutorize 也有同样的问题.我会像这样使用这个注释:
Also, I have this same problem with the annotation PreAutorize. I will use this annotation like that:
@PreAuthorize("hasPermission(#user, '<<permission_name>>')")
@RequestMapping(value="cadastra")
public ModelAndView cadastra() {
ModelAndView mav = new ModelAndView();
mav.setViewName("privado/"+this.entity_name+"/cadastra");
return mav;
}
在我的通用服务方法中也是如此.权限名称遵循以下规则:
and in my methods from my generic service too. The permission name follows this rule:
<<action_name>>_<<entity_name>>
对于每个实体类,我将拥有三个权限:cadastra (new_item)、altera (change_item) 和 remove (remove_item).
and for each entity class I will have three permissions: cadastra (new_item), altera (change_item) and remove (remove_item).
谁能指出如何解决这个问题的方向?
Anyone can point a direction of how to solve this?
推荐答案
对于第一个问题 @ModelAttribute
,据我所知,您有几个表单调用相同的 Mapping 方法 cadastra 来验证是否信息是正确的.我觉得可以用一个form参数来告诉它是哪个实体类,用wrapper来包装所有调用这个方法的实体.
For the first question @ModelAttribute
, what I've understood, you have several form calling the same Mapping method cadastra to verify if the informations are correct.
I think you can use a form parameter to tell which entity class it is, and use wrapper to wrap all of the entities that call this method.
创建一个实体:
@Entity
public class Wrapper{
private Entity1 e1;
.....
在你的jsp中:
<form:form modelAttribute="wrapper" .../>
<form:hidden value="classname" .../>
<form:input path=e1.someProperty .../>
在您的控制器中:
@RequestParam("classname") String classname;//used to get which entity it's
service.cadastra(wrapper.getEntityX())
对于第二个问题,我对@PreAuthorize 注解不太了解.
for the second question I don't know much about @PreAuthorize annotation.
这篇关于通用控制器/服务类中注释@ModelAtribute 和@PreAutorize 的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!