属性约注解@ModelAtribute和@ preAutorize在通用控制器/服务类 [英] Atribute for annotations @ModelAtribute and @PreAutorize in Generic Controller / Service classes

查看:767
本文介绍了属性约注解@ModelAtribute和@ preAutorize在通用控制器/服务类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个泛型类为我的项目。到目前为止,我得到这个code:

I am trying setting a generic class for my project. So far I get this code:

<一个href=\"https://github.com/klebermo/blog.cms/blob/master/src/main/java/com/config/generic/controller/GenericController.java\" rel=\"nofollow\">https://github.com/klebermo/blog.cms/blob/master/src/main/java/com/config/generic/controller/GenericController.java

在这一刻,我坚持这个方法:

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 ,就我的理解,你有几种形式调用相同映射方法cadastra以验证信息是否正确。
我想你可以使用表单参数来告诉它是实体类,并用包装来包装所有调用这个方法的实体。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆