2016-04-07 9 views
0

次のJSFページがあります。私はそれにパラメータとしてidを渡し、コンバータはそのIDを使用してバッキングBeanにオブジェクトを作成します。これは基本的にデータベースの既存のレコードを編集するページです。保存を押すと、roleInformationオブジェクトがコンバーターを使用して再作成されるため、namedescriptionからinputtextまでは設定されていません。どうすれば修正できますか?どのようにConverterが新しいroleInformationを挿入するのを防ぐにはcommandButtonをクリックしますか?f:コンバーターを経由して送信時にviewParam Beanプロパティを再作成

<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" 
      xmlns:p="http://primefaces.org/ui" 
      template="/WEB-INF/template.xhtml"> 
<ui:define name="metadata"> 
    <f:metadata> 
     <f:viewParam name="id" value="#{roleEdit.roleInformation}" 
        converter="#{roleConverter}" 
        required="true" requiredMessage="Bad request, please use a link from within the system." 
       /> 
     <f:viewAction action="#{roleEdit.init}"/> 
    </f:metadata> 
</ui:define> 
<ui:define name="content"> 

    <h3>Here you can edit information about the single role #{param} </h3> 

    <h:form> 
     <p:dataTable id="roleTable" var="roleInformation" value="#{roleEdit.roleInformation}"> 
      <p:column headerText="Id"> 
       <h:outputText value="#{roleInformation.id}"/> 
      </p:column> 
      <p:column headerText="Name"> 
       <p:inputText value="#{roleInformation.name}"/> 
      </p:column> 
      <p:column headerText="Description"> 
       <p:inputText value="#{roleInformation.description}"/> 
      </p:column> 
     </p:dataTable> 
     <br/> 
     <p:pickList value="#{roleEdit.rightInformations}" var="rightInformation" itemLabel="#{rightInformation.id}" 
        itemValue="#{rightInformation}" converter="#{rightConverter}"> 
      <f:facet name="sourceCaption">Available</f:facet> 
      <f:facet name="targetCaption">Role rights</f:facet> 
      <p:column style="width:100%"> 
       #{rightInformation.name} 
      </p:column> 
     </p:pickList> 
     <p:commandButton value="Save" action="#{roleEdit.save}"> 
     </p:commandButton> 
    </h:form> 
</ui:define> 

UPDATE

@ManagedBean(name = "roleEdit") 
@ViewScoped 
public class Edit implements Serializable { 

private DualListModel<RightInformation> rightInformations; 
private RoleInformation roleInformation; 

@Inject 
@RoleServiceJPA 
private RoleService roleService; 

@Inject 
@RightServiceJPA 
private RightService rightService; 

public void init() { 
    List<RightInformation> target = new ArrayList<>(); 
    List<RightInformation> source = rightService.find(null); 
    source.removeAll(roleInformation.getRightInformations()); 
    target.addAll(roleInformation.getRightInformations()); 
    rightInformations = new DualListModel<>(source, target); 
    System.out.println("init roleinfor tostring " + roleInformation.toString()); 
} 

public String save() { 
    System.out.println("role save to string " + roleInformation.toString()); 
    System.out.println(roleInformation.getName()); 
    System.out.println(roleInformation.getDescription()); 
    roleInformation.getRightInformations().clear(); 
    roleInformation.getRightInformations().addAll(rightInformations.getTarget()); 
    roleService.updateRole(roleInformation); 
    return "role?faces-redirect=true&id=" + roleInformation.getId(); 
} 

RoleInformationインタフェース

public interface RoleInformation { 
long getId(); 

String getName(); 

String getDescription(); 

List<RightInformation> getRightInformations(); 

void setName(String name); 

void setDescription(String description); 

} 

RoleConverter

@ManagedBean 
@FacesConverter(forClass = RoleInformation.class) 
public class RoleConverter implements Converter, Serializable { 

@Inject 
@RoleServiceJPA 
private RoleService roleService; 

@Override 
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) { 
    if (value == null || value.isEmpty()) { 
     return null; 
    } 

    Long id = Long.valueOf(value); 
    return roleService.findSingle(id); 
} 

@Override 
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) { 
    return Long.toString(((RoleInformation) o).getId()); 
} 
} 
+0

これは、セッターが入力から呼び出された後に再作成されたことを意味しますか?これは予想される動作ではありません。 – BalusC

+0

@BalusCが助けてくれればより多くのコードを追加しましたが、出力で見ることができるものから、 'name'と' description'が 'inputText'に入力したものと等しくないので、setterが呼び出された後に再作成されると思います。 。 – Asiat

答えて

0

commandButtonを押すと、バッキングBeanが再作成されたという問題がありました。 はjsf注釈だったので、@Namedに切り替えると、私は欲しいものを達成することができました(バッキングビーンは再現されなかったので、StringinputTextから救うことができました)。

関連する問題