JSF 2.2中页面跳转并传递参数的问题
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
selectionattribute is correctly linked to the bean’sselectedObjectproperty, androwKeyuses 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
objectIdis being passed correctly. Debug by printingFacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("objectId")in the target bean’sinit()method.
内容的提问来源于stack exchange,提问作者Eduardo Dorneles

