通过f:viewParam对新/编辑bean使用同一页面的正确方法是什么?

时间:2019-02-05 14:07:43

标签: forms jsf jsf-2 jpa-2.0

我已经使用JSF几个月了。 我的bean Person有很多字段(大约50个)和一些sub-beans(4个地址,2个联系人..):相关的表单很大,并通过一些ajax操作进行管理。我会将该表格用于“新”人员,也用于“编辑”人员。

现在Creating master-detail pages for entities, how to link them and which bean scope to choose之后,我有一个用于person.xhtml的表格,一个搜索页面 search_p.xhtml ,在其中我选择了Person并转到 edit_person.xhtml

search_p.xhtml有一个“人”列表,每个人都有此链接

<h:link value="Open" outcome="person">
 <f:param name="id_p" value="#{person.id}"/>
</h:link>

person.xhtml包含

<f:metadata>
 <f:viewParam name="id_p" value="#{editPerson.person}"
 converter="#{personConverter}"
 converterMessage="Person unknown, please use a link from within the system."
 required="true" requiredMessage="Bad request, please use a link from within the system." />
</f:metadata>

PersonConverter具有

@ManagedBean
@RequestScoped
public class PersonConverter implements Converter {
 public Object getAsObject(FacesContext context, UIComponent component, String value) {
  if (value == null || value.isEmpty()) return null; 
  try {
   if(value.equals("new")) return new Person(); //used for link to new
   else {
    Integer id = Integer.valueOf(value);
    return PersonDAO.getById(id.intValue());
   }
  } catch (NumberFormatException e) { throw new ConverterException("Not valid Person ID: " + value, e); }
 }

 @Override    
 public String getAsString(FacesContext context, UIComponent component, Object value) {        
  if (value == null) return "";
  if (value instanceof Ordine) {
   Integer id = ((Ordine) value).getId();
   return (id != null) ? String.valueOf(id.intValue()) : null;
  } else {
   throw new ConverterException("Not valid Person ID: " + value);}
 }
 }

side_menu.xhtml是模板的一部分,在这里它称为“新人”

<li><h:link value="New" outcome="person">
  <f:param name="id_p" value="new" />
</h:link></li>

EditPerson bean是

@ManagedBean
@ViewScoped
public class EditPerson implements Serializable {
    private static final long serialVersionUID = 1768587713177545840L;
    private Person person; 

    public Person getPerson() {return person;}
    public void setPerson(Person person) {this.person = person;}
    public String save() {
        PersonDAO.addOrUpdate(person);
        return "/search_p?faces-redirect=true";
    }

因此,当我进入person.xhtml时,

Contact 2
<h:selectBooleanCheckbox value="#{editPerson.person.enable_c2}" id="flag_c2">
    <f:ajax event="click" execute="flag_c2" render="div_c2"></f:ajax>
</h:selectBooleanCheckbox>

出现一个弹出窗口: serverError:类javax.faces.component.UpdateModelException /include/person/tab_anag.xhtml @ 372,26 value =“#{editPerson.person.enable_f2}”:目标无法访问,“人”返回null 。由于页面代码中包含 f:metadata ,我认为PersonConverter被解雇了:我可以避免在解雇ajax时调用PersonConverter吗?我只需要 f:metadata 来从search_p页面传递ID。

0 个答案:

没有答案