简体   繁体   English

从服务器中的jsp页面读取参数

[英]Reading parameters from jsp page in the server

In my Spring project, my service classes are capturing the parameters I send from my forms via method POST this way, currently: 在我的Spring项目中,我的服务类正在捕获以这种方式通过POST方法从表单发送的参数,目前:

@Transactional
@PreAuthorize("hasPermission(#user, 'cadastra_fornecedor')")
public boolean cadastra(HttpServletRequest request, HttpServletResponse response) {
    String identificador = request.getParameter("identificador");
    String nome = request.getParameter("nome");
    String website = request.getParameter("website");
    String[] endereco2 = request.getParameterValues("endereco[]");
    String[] contato2 = request.getParameterValues("contato[]");

    Fornecedor f = new Fornecedor();
    f.setRazao_social(nome);
    f.setWebsite(website);
    if(identificador.length() == 14)
        f.setCnpj(identificador);
    else if(identificador.length() == 11)
        f.setCpf(identificador);

    List<Endereco> lista_endereco = new ArrayList<Endereco>();
    if(endereco2 != null) {
        int max = endereco2.length;
        for(int i=0; i<max; i++) {
            int id = Integer.valueOf(endereco2[i]).intValue();
            lista_endereco.add(endereco.findById(id));
        }
        f.setEndereco(lista_endereco);
    }


    List<Contato> lista_contato = new ArrayList<Contato>();
    if(contato2 != null) {
        int max = contato2.length;
        for(int i=0; i<max; i++) {
            int id = Integer.valueOf(contato2[i]).intValue();
            lista_contato.add(contato.findById(id));
        }
        f.setContato(lista_contato);
    }

    return fornecedor.persist(f);
}

I am trying find another way to read this parameters, without the need of read one by one with the method getParameter (or getParaterValues). 我正在尝试寻找另一种读取此参数的方法,而无需使用getParameter(或getParaterValues)方法逐一读取。

Each service class is related to one of my Entity classes, the name of the fields from form have the same name of the atributes in the table, and all of them which are arrays, are lists of primary keys to another entity. 每个服务类都与我的Entity类之一相关,来自form的字段名称与表中的属性名称相同,并且它们都是数组,都是另一个实体的主键列表。

Anyone knows if exist any solution where I could use the information above to read this parameters all at once? 任何人都知道是否存在可以使用以上信息一次读取所有参数的解决方案?

(I trying create a generic class for my controller and service classes to group this commons methods. I wonder if exist a way to do that without direct reference to the names of each parameter.) (我试图为我的控制器和服务类创建一个通用类来对这个commons方法进行分组。我想知道是否存在一种无需直接引用每个参数名称的方法。)

I prefer a solution where no other framework besides Spring be used. 我更喜欢这样的解决方案:除了Spring之外,不使用其他框架。

My suggestion is to use your Entity to pass the parameters. 我的建议是使用您的实体来传递参数。

You mentioned you already have Entity classes for the forms, then you should be able to use 您提到您已经有表单的Entity类,那么您应该可以使用

@ModelAttribute(value=" ")Entity entity

to get all the form parameters, and then you pass this entity to your service method as a parameter? 获取所有表单参数,然后将此实体作为参数传递给服务方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM