gpt4 book ai didi

jsf-2 - JSF viewAction 传递参数并设置它,但返回 null

转载 作者:行者123 更新时间:2023-12-01 02:20:17 25 4
gpt4 key购买 nike

JSF 2.2
Primefaces 4.0
JBoss 野蝇

从带有客户列表的页面,并希望每个客户都有一个按钮,用户可以在其中添加项目。
当我单击“新项目”按钮时,我被重定向到新项目页面。

在 url 中是客户 ID

newItem.jsf;jsessionid=Xw7tdljr9f0atzyET2Fy6_WI?customerId=3

我可以调试新项目 bean 中的 set customer id 方法以值 3 调用,很好:)
但是在我调试之后,调用了 get customer id 方法..现在客户 id 为 null :(

我做了一个 syso :

18:10:25,312 INFO [stdout] (default task-9) Setting customer id 3



所以客户 id 开始设置......但以某种方式重置为 null ????

客户.xhtml
<ui:define name="content">
<f:metadata>
<f:viewParam name="customerId" value="#{customerController.customerEnt.id}" />
</f:metadata>
<h:form id="customers" prependId="false" includeViewParams="true">
<p:dataTable id="dataTable" var="customer"
value="#{customerController.customers}" rowKey="#{customer.id}"
styleClass="userDataTableStyle" paginator="true" rows="10"
selection="#{customerController.selectedCustomers}"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
lazy="true" rowsPerPageTemplate="10,15,50">
...

<p:column>
<p:commandButton ajax="false" value="New Item" action="#{customerController.newItem(customer)}"/>
</p:column>

</p:dataTable>

</h:form>

新项目.xhtml
<ui:define name="content">
<f:metadata>
<f:viewParam name="customerId"
value="#{newItemController.customerId}" />
<f:viewAction action="#{newItemController.init()}"/>
</f:metadata>
<h:form id="item" includeViewParams="true">
...

新项目 Controller .java
@SuppressWarnings("serial")
@ViewScoped
@Named
public class NewItemController implements Serializable {

private CustomerEnt customerEnt;
private String customerId;

@PostConstruct
public void init() {
itemEnt = new ItemEnt();

if (customerId == null) {
String message = "Bad request. Please use a link from within the system.";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
return;
}

customerEnt = customerDas.find(Long.parseLong(customerId));

if (customerEnt == null) {
String message = "Bad request. Unknown customer.";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
}
}

public String getCustomerId() {
return customerId;
}

public void setCustomerId(String customerId) {
this.customerId = customerId;
System.out.println("Setting customer id " + customerId);
}
}

客户 Controller .java
@SuppressWarnings("serial")
@SessionScoped
@Named
public class CustomerController implements Serializable {

private Long customerId;

public String newItem(CustomerEnt customerEnt) {
customerId = customerEnt.getId();
return "newItem?faces-redirect=true&customerId=" + customerId;
}

正如 L-Ray 所说,init 被调用了两次,所以我在 NewItemController 中做了这个改变:
        public void init() {
System.out.println("In init");
}

@PostConstruct
public void postConstruct() {
itemEnt = new ItemEnt();
System.out.println("In postConstruct");

}

public void loadData() {
if (customerId == null) {
String message = "Bad request. Please use a link from within the system.";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
return;
}
}


public void save() throws Exception {
try {
serviceSLSB.save(Long.parseLong(customerId), itemEnt);
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved!", "Item saved successful");
facesContext.addMessage(null, m);
postConstruct();
} catch (ConstraintViolationException e) {
itemEnt.setBid(null);
String errorMessage = getRootErrorMessage(e);
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Saving unsuccessful");
facesContext.addMessage(null, m);
} catch (Exception e) {
String errorMessage = getRootErrorMessage(e);
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Saving unsuccessful");
facesContext.addMessage(null, m);
}
}

并在 newItem.xhtml
<f:metadata>
<f:viewParam name="customerId"
value="#{newItemController.customerId}" />
<f:viewAction action="#{newItemController.loadData()}"/>
</f:metadata>

现在它起作用了... :) 但现在我有一个新问题...我将为此创建一个单独的问题 :)
谢谢您的帮助

最佳答案

给定的源看起来不错 - 只有一件事引起了我的注意:目前,您的 NewItemController.init() 被调用了两次

  • 作为@PostConstruct
  • 通过 f:viewAction

  • 如果你无论如何调用这个方法,你就不需要注解,不是吗?

    关于jsf-2 - JSF viewAction 传递参数并设置它,但返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21082962/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com