You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSF 2.2中页面跳转并传递参数的问题

Fixing CommandButton Page Navigation with Object ID Parameter

Alright, let's work through getting your <commandButton> (I’m assuming PrimeFaces here, since it’s the most common implementation for this component) to properly navigate and pass the selected object’s ID—even though we know POST isn’t the ideal approach for page navigation. Let’s break this down step by step.

1. Correctly Pass the ID from the Source Page

First, you need to ensure the selected object’s ID is properly passed when the button is clicked. The key here is to disable AJAX (since AJAX requests won’t trigger a full page redirect) and handle navigation via a bean method that injects the ID into the request or session.

Source Page XHTML

<!-- Your data table with selection logic -->
<p:dataTable value="#{sourceBean.objectList}" var="obj" 
             selection="#{sourceBean.selectedObject}" rowKey="#{obj.id}">
    <!-- Columns for displaying object properties -->
    <p:column headerText="Actions">
        <!-- Command button with AJAX disabled to trigger full page navigation -->
        <p:commandButton value="View Full Details" 
                         action="#{sourceBean.navigateToDetails}" 
                         ajax="false" />
    </p:column>
</p:dataTable>

Source Bean Code

@ManagedBean
@ViewScoped
public class SourceBean implements Serializable {
    private List<MyObject> objectList;
    private MyObject selectedObject;

    // Getters and setters for both properties

    public String navigateToDetails() {
        // Pass the selected object's ID as a request parameter
        FacesContext.getCurrentInstance()
                    .getExternalContext()
                    .getRequestMap()
                    .put("objectId", selectedObject.getId());

        // Use faces-redirect=true to avoid POST duplicate submission issues
        return "detailsPage.xhtml?faces-redirect=true&includeViewParams=true";
    }
}

2. Receive the ID and Load the Object on the Target Page

On the target page’s bean, you’ll fetch the ID and load the full object from your database. You can use @ManagedProperty to inject the request parameter directly, or pull it from the request map.

Target Bean Code

@ManagedBean
@ViewScoped
public class DetailsBean implements Serializable {
    private MyObject loadedObject;

    // Inject the ID from the request parameter
    @ManagedProperty(value = "#{param.objectId}")
    private Long objectId;

    // Getters and setters for objectId and loadedObject

    @PostConstruct
    public void init() {
        if (objectId != null) {
            // Call your DAO/service to fetch the full object from the database
            loadedObject = myObjectService.getObjectById(objectId);
        } else {
            // Handle missing ID case (e.g., redirect to error page)
            try {
                FacesContext.getCurrentInstance()
                            .getExternalContext()
                            .redirect("error.xhtml");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Target Page XHTML

You can now display the loaded object’s properties directly:

<h1>Object Details</h1>
<p>ID: #{detailsBean.loadedObject.id}</p>
<p>Name: #{detailsBean.loadedObject.name}</p>
<!-- Add other object properties as needed -->

3. Troubleshooting Common Issues

If you’re still seeing problems on page load, check these common pitfalls:

  • Forgot ajax="false": CommandButtons default to AJAX, which won’t trigger full page navigation. Always disable AJAX when using it for redirects.
  • Incorrect selection binding: Ensure your data table’s selection attribute is correctly linked to the bean’s selectedObject property, and rowKey uses a unique identifier (like the object’s ID).
  • Missing faces-redirect=true: Without this, you’ll stay on the POST request’s page, which can cause duplicate submissions and unexpected behavior.
  • Null ID in target bean: Verify the objectId is being passed correctly. Debug by printing FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("objectId") in the target bean’s init() method.

内容的提问来源于stack exchange,提问作者Eduardo Dorneles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:16:59