从支持bean中的inputtext获取值(jsf 2)

时间:2013-01-30 13:48:17

标签: jsf-2 managed-bean

我有一行数据表,我需要编辑这一行的字段,所以我有一些带有值的inputText,但当我编辑它们并点击命令按钮(调用方法" actualizarUsuario& #34;值传递为null。

这是我的bean代码:

@ManagedBean(name = "user")
@ViewScoped
public class userDetalles implements Serializable {



    private Usuario u;
    private usuarioController controlador;
    Rol rol;

    private long selection;
    private long selectionrol;
    Agrupacion agrupacion;
    private Privilegio privilegio;
    private RolController controladorRol;
    private ControladorAgrupaciones controladorAgrup;
    private String nombres;
    private String apellidoP;
    private String apellidoM;
    private Boolean check;

    @PostConstruct
    public void init() {

       rol= new Rol() ;
       u=new Usuario();
       agrupacion=new Agrupacion();
       privilegio=new Privilegio();
       controlador= new usuarioController();
       controladorRol=new RolController();
       controladorAgrup=new ControladorAgrupaciones();
       Usuario u=new Usuario();
       FacesContext facesContext = FacesContext.getCurrentInstance();
       ExternalContext externalContext = facesContext.getExternalContext();

        //Obtener parametros del request
       Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
       long iduser = Long.valueOf(parameterMap.get("id_usuario"));

       this.u=controlador.getUser(iduser);
    }

    public Usuario getU() {
        return u;
    }

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

    public long getSelection() {
        System.out.println("selection value----------->"+selection);
        return selection;
    }

    public void setSelection(long selection) {
        this.selection = selection;
    }

    public long getSelectionrol() {
        return selectionrol;
    }

    public void setSelectionrol(long selectionrol) {
        this.selectionrol = selectionrol;
    }

    public String getNombres() {
        return nombres;
    }

    public void setNombres(String nombres) {
        this.nombres = nombres;
    }

    public String getApellidoP() {
        return apellidoP;
    }

    public void setApellidoP(String apellidoP) {
        this.apellidoP = apellidoP;
    }

    public String getApellidoM() {
        return apellidoM;
    }

    public void setApellidoM(String apellidoM) {
        this.apellidoM = apellidoM;
    }

    public Boolean getCheck() {
        return check;
    }

    public void setCheck(Boolean check) {
        this.check = check;
    }


    public void actualizarUsuario(){

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        Map<String, String> parameterMap = (Map<String, String>)   externalContext.getRequestParameterMap();
        nombres=parameterMap.get("nombres");
        apellidoP=parameterMap.get("apellidoP");
        apellidoM=parameterMap.get("apellidoM");
        check=Boolean.parseBoolean(parameterMap.get("check"));
        //test
        System.out.println(nombres+" "+apellidoP+" "+apellidoM+" "+check);
        u.setNombres(nombres);
        u.setApellidoPaterno(apellidoP);
        u.setApellidoMaterno(apellidoM);
        u.setActive(check);
        controlador.saveUsuario(u);
    }



}

这是我的观点:

<?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="Users">
            <h:form id="Form">
                <h2>Detalles Usuario</h2>

                <h:dataTable id="users" value="#{user.u}"  styleClass="table table-striped table-bordered" headerClass="sorting_asc"
                             rowClasses="odd,even">
                    <h:column>
                        <f:facet name="header">#</f:facet>
                        #{user.u.id}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Identificador</f:facet>
                        <h:inputText id="identificador" value="#{user.u.identificador}" />
                    </h:column>
                    <h:column>
                     <f:facet name="header">Nombre</f:facet>

                     <h:inputText id="nombres" value="#{user.u.nombres}"/>
                     <h:inputText id="apellidoP" value="#{user.u.apellidoPaterno}"/>
                     <h:inputText id="apellidoM" value="#{user.u.apellidoMaterno}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">Active</f:facet>
                        <h:selectBooleanCheckbox id="check" value="#{user.u.active}"></h:selectBooleanCheckbox>
                    </h:column>

                </h:dataTable>

                <h:commandButton  value="Actualizar" type="submit" styleClass="btn-primary" actionListener="#{user.actualizarUsuario}">
                </h:commandButton>
 </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>

1 个答案:

答案 0 :(得分:4)

由于您使用了错误的参数名称,导致您的具体问题。查看生成的HTML输出和HTTP流量监视器以获取正确的参数名称。

但是,您的实际问题更大:您的视图/模型方法完全错误。您根本不应该使用<h:dataTable>。它旨在用于List<User>等实体的集合,而不是像User这样的单个实体。您应该使用<h:panelGrid>。您根本不需要在控制器中爆炸/展平模型属性。您已在模型中拥有这些属性。您无需手动遍历请求参数映射。 JSF已经为您完成了所有工作。

我不会为你重写这个烂摊子,但到了这一步,你应该遵循下面的启动示例:

型号:

public class User {

    private Long id;
    private String username;
    private String firstname;
    private String lastname;
    // ...

    // Autogenerate standard getters/setters.
}

控制器:

@ManagedBean
@ViewScoped
public class EditUser {

    private User user; // Initialize it in postconstruct or as viewparam.
    private UserService service; // Initialize it as @EJB or in postconstruct.

    public void save() {
        service.save(user); // That's all. Really.
    }

    public User getUser() {
        return user;
    }

    // No other getters/setters! They are all already in User class.
}

查看:

<h:panelGrid>
    <h:inputText value="#{editUser.user.username}" />
    <h:inputText value="#{editUser.user.firstname}" />
    <h:inputText value="#{editUser.user.lastname}" />
    <h:commandButton value="save" action="#{editUser.save}" />
</h:panelGrid>

这就是全部。另见其他this JSF 2.0 tutorial。至于您尝试按ID获取用户,您应该使用<f:viewParam>,另请参阅What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?communication in JSF 2.0