回发期间请求参数为null [英] Request parameter is null during postback

查看:57
本文介绍了回发期间请求参数为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示用户列表的视图,从该视图中,我可以转到任何选定用户的详细信息"的另一个视图.在详细信息视图中,我需要从2选择列表中选择一些值,然后在支持的bean中获取这些值,并将它们添加到用户中,以最终将用户存储(更新)到数据库中.这些是我在用户Bean"中的方法.

I have a view that display a list of users, from this view I can go to another view of "details" of any selected user. In the details view I need to select some values from 2 select list and then in the backed bean take these values, and add them to an user to finally store (update) the user in the database. These are my methods in the "user Bean".

通过这种方法,我从用户列表视图"中获取用户ID,并从数据库中检索该用户以在详细信息视图中显示其信息.

With this method I get the user id from the "list of users view" and retrieve the user from the database to display its info on the details view.

public void getParam(){

    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();

    //Obtener parametros del request
    Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
    Long param = Long.valueOf(parameterMap.get("id_usuario"));
    System.out.println(param);
    this.setU(controlador.getUser(param));


}

使用这种方法,我将选择列表中的值设置为一个对象,然后将该对象添加到用户,最后将其保存在数据库中.

With this method I set the values from the select list to an object and then I add this object to the user, finally I save it on the database.

public void setPrivilegio(){

    System.out.println("hola");
    Privilegio pri=new Privilegio();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();

    //Obtener parametros del request
    Map parameterMap = externalContext.getRequestParameterMap();
    Agrupacion agrupacion= (Agrupacion)parameterMap.get("agrup");
    System.out.println(agrupacion.getNombre());
    Rol rol = (Rol)parameterMap.get("rols");
    System.out.println(rol.getNombre());
    System.out.println(""+rol.getNombre()+" "+agrupacion.getNombre());
    pri.setRol(rol);
    pri.setAgrupacion(agrupacion);
    pri.setActive(true);
    this.getU().addPrivilegio(pri);

    controlador.saveUsuario(this.getU());


}

这是我的观点:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">
    <div class="container">
        <h:panelGroup id="Usuarios">
            <h:form id="FormUsuarios">
                <h2>Detalles Usuario</h2>

                <h:dataTable id="users" value="#{usuario.u}"  styleClass="table table-striped table-bordered" headerClass="sorting_asc"
                             rowClasses="odd,even">
                    <h:column>
                        <f:facet name="header">#</f:facet>
                        #{usuario.u.id}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Identificador</f:facet>
                        <h:inputText id="identificador" value="#{usuario.u.identificador}" />
                    </h:column>
                    <h:column>
                        <f:facet name="header">Nombre</f:facet>
                        <h:inputText id="nombres" value=" #{usuario.u.nombres}"/>   <h:inputText id="apellidoP" value=" #{usuario.u.apellidoPaterno}"/> <h:inputText id="apellidoM" value=" #{usuario.u.apellidoMaterno}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">Active</f:facet>
                        <h:selectBooleanCheckbox id="check" value="#{usuario.u.active}"></h:selectBooleanCheckbox>
                    </h:column>

                </h:dataTable>

                <h3>Asignar Privilegios</h3>

                <h:selectOneMenu id="agrup" value="#{usuario.selected}" converter="omnifaces.SelectItemsConverter">
                    <f:selectItems value="#{agrupacion.agrupacion}" var="entity" itemLabel="#{entity.nombre}" itemValue="#{entity.id}"/>
                </h:selectOneMenu>

                <h:selectOneMenu id="rols" value="#{rol.selected}" converter="omnifaces.SelectItemsConverter">
                    <f:selectItems value="#{rol.roles}" var="rol" itemLabel="#{rol.nombre}" itemValue="#{rol.id}"/>
                </h:selectOneMenu>

                <h:commandButton  value="Asignar"  styleClass="btn-primary" actionListener="#{usuario.setPrivilegio}">
                </h:commandButton>


                <h3>Privilegios Asignados:</h3>

                <h:dataTable id="privilegios" value="#{usuario.u.privilegios}" var="p" styleClass="table table-striped table-bordered" headerClass="sorting_asc"
                             rowClasses="odd,even">
                    <h:column>
                        <f:facet name="header">#</f:facet>
                        #{p.id}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Roles</f:facet>
                        #{p.rol.nombre}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Grupos</f:facet>
                        #{p.agrupacion.nombre}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Active</f:facet>
                        <h:selectBooleanCheckbox id="checkbox" value="#{p.active}"></h:selectBooleanCheckbox>
                    </h:column>

                </h:dataTable>






            </h:form>
            <script type="text/javascript" src="js/paging-bootstrap.js"></script>
            <script type="text/javascript" src="js/contenidoc.datatable.init.js"></script>
        </h:panelGroup>
    </div>
</ui:composition>

当我单击名为"Asignar"的命令按钮调用方法setPrivilegio()时,出现此错误:

When I click on my commandbutton called "Asignar" that calls the method setPrivilegio(), I get this error:

java.lang.NumberFormatException: null
    at java.lang.Long.parseLong(Long.java:404)
    at java.lang.Long.valueOf(Long.java:540)
    at cl.uchile.sti.bean.UsuarioBean.getParam(UsuarioBean.java:114)

视图中的表显示了所有信息,但是当我想调用将选定项添加到用户并将其保存到数据库(setPrivilegio)的方法时,会出现此错误.

The tables in the view shows all the info, but when I want to call the method that add the selected items to the user and save it on the database (setPrivilegio) I get this error.

这是怎么引起的,我该如何解决?

How is this caused and how can I solve it?

这是我的完整用户bean":

This is my full "user bean":

@ManagedBean(name = "usuario")
@ViewScoped
public class UsuarioBean {
    private usuarioController controlador;
    private Usuario u=new Usuario();
    private Privilegio Selected=new Privilegio();
    private Boolean active;
    private long id_user;

    @PostConstruct
    public void init() {


        controlador=new usuarioController();

    }


    public long getId_user() {
        return id_user;
    }

    public void setId_user(long id_user) {
        this.id_user = id_user;
    }

    public Privilegio getSelected() {
        return Selected;
    }

    public void setSelected(Privilegio selected) {
        Selected = selected;
    }

    public Boolean getActive() {

        return active;
    }

    public void setActive(Boolean active) {

        this.active = active;
    }

    public Usuario getU() {
        getParam();
        return u;
    }

    public void setU(Usuario u) {
        this.u = u;
    }

    private List<Usuario> usuario;



    public List<Usuario> getUsuario() {
        usuario=UsuarioDAO.getAll();
        return usuario;
    }

    public Usuario getById(long id_usuario){

       return u;
    }

    public void setUsuario(List<Usuario> usuario) {
        this.usuario = usuario;
    }


    public void saveUsuario(Usuario u){
        controlador.saveUsuario(u);
    }

    public void getParam(){

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();

        //Obtener parametros del request
        Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
        Long param = Long.valueOf(parameterMap.get("id_usuario"));
        System.out.println(param);
        this.setU(controlador.getUser(param));


    }

    public void setPrivilegio(){

        System.out.println("hola");
        Privilegio pri=new Privilegio();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();

        //Obtener parametros del request
        Map parameterMap = externalContext.getRequestParameterMap();
        Agrupacion agrupacion= (Agrupacion)parameterMap.get("agrup");
        System.out.println(agrupacion.getNombre());
        Rol rol = (Rol)parameterMap.get("rols");
        System.out.println(rol.getNombre());
        System.out.println(""+rol.getNombre()+" "+agrupacion.getNombre());
        pri.setRol(rol);
        pri.setAgrupacion(agrupacion);
        pri.setActive(true);
        this.getU().addPrivilegio(pri);

        controlador.saveUsuario(this.getU());


    }









}

这是第一个视图(用户列表,我从中查看用户详细信息)

this is the first view (list of users, from which i go to user details)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">
    <div class="container">
        <h:panelGroup id="Usuarios">
            <h:form id="FormUsuarios">
                <h2>Listado de Usuarios</h2>
                <h:graphicImage url="http://a.dryicons.com/images/icon_sets/simplistica/png/128x128/add.png" width="30" height="30"/>

                <h:dataTable id="users" value="#{usuario.usuario}" var="o" styleClass="table table-striped table-bordered" headerClass="sorting_asc"
                             rowClasses="odd,even">
                    <h:column>
                        <f:facet name="header">#</f:facet>
                        #{o.id}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Identificador</f:facet>
                        #{o.identificador}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Nombre</f:facet>
                        #{o.nombres}  #{o.apellidoMaterno} #{o.apellidoPaterno}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Active</f:facet>
                        <h:selectBooleanCheckbox id="check" value="#{o.active}"></h:selectBooleanCheckbox>
                    </h:column>
                    <h:column>
                        <f:facet name="header">Detalles</f:facet>


                        <h:outputLink  value="contenido/detalleUsuario.xhtml">
                            Detalle
                            <f:param name="id_usuario" value="#{o.id}"  />
                        </h:outputLink>
                    </h:column>
                </h:dataTable>
            </h:form>
            <script type="text/javascript" src="js/paging-bootstrap.js"></script>
            <script type="text/javascript" src="js/contenidoc.datatable.init.js"></script>
        </h:panelGroup>
    </div>
</ui:composition>

推荐答案

吸气剂不正确!

    public Usuario getU() {
    getParam();
    return u;
    }

上面的吸气剂是一个非常糟糕的主意.您自己说过,变量使它成为了usuario支持bean(我对此表示怀疑). 在getter内执行业务逻辑是错误的在请求期间多次调用该getter.有更优雅,更简洁的方法可以在JSF页面之间传递和初始化参数.

The getter above is a very bad idea. You've said it yourself that the variable makes it into the usuario backing bean(this I doubt). It is just plain wrong to perform business logic inside a getter because of inconsistencies (like you're experiencing) and the fact that the getter is called multiple times during a request. There are more elegant and cleaner ways to pass and initialise parameters between JSF pages.

private Usuario u=new Usuario();也不是个好主意.为什么有这个必要

private Usuario u=new Usuario(); is also a bad idea. Why is this necessary when you have

      this.setU(controlador.getUser(param));

所有应该在您的@PostConstructor

    @PostConstruct
    public void init() {


    controlador=new usuarioController();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();

    //Obtener parametros del request
    Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
    Long param = Long.valueOf(parameterMap.get("id_usuario"));
    System.out.println(param);
    this.setU(controlador.getUser(param));

    }

吸气剂应该很简单

public Usuario getU() {
    return u;
}

这篇关于回发期间请求参数为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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