1

我有一个包含一行的数据表,我需要编辑这一行的字段,所以我有一些带有值的 inputText,但是当我编辑它们并单击命令按钮(调用方法“actualizarUsuario”时,值被传递为无效的。

这是我的 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>
4

1 回答 1

5

您的具体问题是由于您使用了错误的参数名称而引起的。在生成的 HTML 输出和 HTTP 流量监视器中查找正确的参数名称。

但是,您的实际问题更大:您的视图/模型方法完全错误。你根本不应该使用 a <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>

就这样。另请参阅此 JSF 2.0 教程。至于您通过 ID 获取用户的尝试,您应该使用<f:viewParam>,另请参阅<f:metadata>、<f:viewParam> 和 <f:viewAction> 可以用于什么?JSF 2.0 中的通信

于 2013-01-30T14:05:51.063 回答