Request parameter is null during postback

后端 未结 2 1119
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 11:46

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 fro

相关标签:
2条回答
  • 2020-12-21 12:21

    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;
    }
    
    0 讨论(0)
  • 2020-12-21 12:29

    The cause of this error is that parameterMap.get("id_usuario") is null. You should investigate how you pass this parameter from the UI to the backing bean.

    0 讨论(0)
提交回复
热议问题