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

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

问题描述

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

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

public void getParam(){FacesContext facesContext = FacesContext.getCurrentInstance();ExternalContext externalContext = facesContext.getExternalContext();//观察者参数删除请求映射<字符串,字符串>parameterMap = (Map) 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("你好");特权优先=新特权();FacesContext facesContext = FacesContext.getCurrentInstance();ExternalContext externalContext = facesContext.getExternalContext();//观察者参数删除请求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());}

这是我的观点:

<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="容器"><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="奇数,偶数"><h:列><f:facet name="header">#</f:facet>#{usuario.u.id}</h:列><h:列><f:facet name="header">标识符</f:facet><h:inputText id="identificador" value="#{usuario.u.identificador}"/></h:列><h:列><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:列><h:列><f:facet name="header">Active</f:facet><h:selectBooleanCheckbox id="check" value="#{usuario.u.active}"></h:selectBooleanCheckbox></h:列></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>授权委托人:</h3><h:dataTable id="privilegios" value="#{usuario.u.privilegios}" var="p" styleClass="table table-striped table-bordered" headerClass="sorting_asc"rowClasses="奇数,偶数"><h:列><f:facet name="header">#</f:facet>#{p.id}</h:列><h:列><f:facet name="header">角色</f:facet>#{p.rol.nombre}</h:列><h:列><f:facet name="header">Grupos</f:facet>#{p.agrupacion.nombre}</h:列><h:列><f:facet name="header">Active</f:facet><h:selectBooleanCheckbox id="checkbox" value="#{p.active}"></h:selectBooleanCheckbox></h:列></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>

</ui:composition>

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

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

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

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

这是我完整的用户 bean":

@ManagedBean(name = "usuario")@ViewScoped公共类 UsuarioBean {私有 usuarioController 控制器;私人 Usuario u=new Usuario();私人特权选择=新特权();私人布尔活动;私人长id_user;@PostConstruct公共无效初始化(){controlador=new usuarioController();}公共长 getId_user() {返回 id_user;}公共无效setId_user(长id_user){this.id_user = id_user;}公共特权 getSelected() {返回已选;}public void setSelected(已选择特权){选中 = 选中;}公共布尔 getActive() {返回活跃;}public void setActive(Boolean active) {this.active = 活动;}公共使用 getU() {获取参数();返回你;}public void setU(Usuario u) {这.u = u;}私人名单<Usuario>usuario;公共列表getUsuario() {usuario=UsuarioDAO.getAll();返回usuario;}公共 Usuario getById(long id_usuario){返回你;}public void setUsuario(Listusuario) {this.usuario = usuario;}公共无效 saveUsuario(Usuario u){controlador.saveUsuario(u);}公共无效getParam(){FacesContext facesContext = FacesContext.getCurrentInstance();ExternalContext externalContext = facesContext.getExternalContext();//观察者参数删除请求映射<字符串,字符串>parameterMap = (Map) externalContext.getRequestParameterMap();Long param = Long.valueOf(parameterMap.get("id_usuario"));System.out.println(param);this.setU(controlador.getUser(param));}公共无效 setPrivilegio(){System.out.println("你好");特权优先=新特权();FacesContext facesContext = FacesContext.getCurrentInstance();ExternalContext externalContext = facesContext.getExternalContext();//观察者参数删除请求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());}}

这是第一个视图(用户列表,从中我可以转到用户详细信息)

<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="容器"><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="奇数,偶数"><h:列><f:facet name="header">#</f:facet>#{o.id}</h:列><h:列><f:facet name="header">标识符</f:facet>#{o.identificador}</h:列><h:列><f:facet name="header">Nombre</f:facet>#{o.nombres} #{o.apellidoMaterno} #{o.apellidoPaterno}</h:列><h:列><f:facet name="header">Active</f:facet><h:selectBooleanCheckbox id="check" value="#{o.active}"></h:selectBooleanCheckbox></h:列><h:列><f:facet name="header">细节</f:facet><h:outputLink value="contenido/detalleUsuario.xhtml">细节<f:param name="id_usuario" value="#{o.id}"/></h:outputLink></h:列></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>

</ui:composition>

解决方案

Bad getter!

 public Usuario getU() {获取参数();返回你;}

上面的 getter 是一个非常糟糕的主意.您自己说过该变量使其成为 usuario 支持 bean(我对此表示怀疑).由于不一致(就像您遇到的那样)以及在请求期间多次调用 getter.有在 JSF 页面之间传递和初始化参数的更优雅、更简洁的方法.

private Usuario u=new Usuario(); 也是一个坏主意.为什么有必要这样做

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

所有应该发生在你的 @PostConstructor

 @PostConstruct公共无效初始化(){controlador=new usuarioController();FacesContext facesContext = FacesContext.getCurrentInstance();ExternalContext externalContext = facesContext.getExternalContext();//观察者参数删除请求映射<字符串,字符串>parameterMap = (Map) externalContext.getRequestParameterMap();Long param = Long.valueOf(parameterMap.get("id_usuario"));System.out.println(param);this.setU(controlador.getUser(param));}

getter 应该是简单的

public Usuario getU() {返回你;}

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".

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());


}

This is my view:

<?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>

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)

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?

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>

解决方案

Bad getter!

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

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(); is also a bad idea. Why is this necessary when you have

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

All that should happen inside your @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));

    }

The getter should just be plain

public Usuario getU() {
    return u;
}

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

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆