JSF项目:如何在PrettyFaces设置URL参数前调用Bean方法并重置属性?
Great question! I’ve worked through similar scenarios with PrettyFaces (now integrated as part of the Rewrite framework) in JSF projects, so let’s walk through how to address both your requirements clearly.
1. Calling a Bean Method Before PrettyFaces Sets URL Parameters
PrettyFaces/Rewrite provides straightforward ways to execute code before URL query parameters are bound to your managed bean properties. You can use either annotation-based PrettyFaces syntax or Rewrite’s flexible rule API.
Option 1: Using @UrlAction (PrettyFaces Annotation)
Add a method to your managed bean and annotate it with @UrlAction(phase = UrlAction.Phase.BEFORE_PARAMS). This tells PrettyFaces to run the method before it injects any URL parameters into your bean properties.
Here’s a concrete example:
import com.ocpsoft.pretty.faces.annotation.UrlAction; import com.ocpsoft.pretty.faces.annotation.UrlMapping; import com.ocpsoft.pretty.faces.annotation.UrlMappings; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; @ManagedBean @ViewScoped @UrlMappings({ @UrlMapping( id = "userProfile", pattern = "/user/profile", viewId = "/userProfile.xhtml" ) }) public class UserProfileBean { private String userId; private String userName; // Executes BEFORE URL parameters are bound to bean properties @UrlAction(phase = UrlAction.Phase.BEFORE_PARAMS) public void preParameterSetup() { // Your custom logic here: e.g., validate session state, load base data System.out.println("Running pre-parameter binding logic..."); } // Getters & Setters public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
Option 2: Rewrite Rule-Based Approach (More Flexible)
If you need cross-bean or global pre-binding logic, use Rewrite’s core API by creating a ConfigurationProvider to define a rule that triggers your bean method before request parameters are decoded.
import org.ocpsoft.rewrite.annotation.RewriteConfiguration; import org.ocpsoft.rewrite.config.Configuration; import org.ocpsoft.rewrite.config.ConfigurationBuilder; import org.ocpsoft.rewrite.el.EL; import org.ocpsoft.rewrite.faces.config.Faces; import javax.servlet.ServletContext; @RewriteConfiguration public class AppRewriteConfig implements org.ocpsoft.rewrite.config.ConfigurationProvider { @Override public Configuration getConfiguration(ServletContext context) { return ConfigurationBuilder.begin() .addRule() // Match the target page(s) where you need pre-parameter logic .when(Faces.isPage("/userProfile.xhtml")) // Call your bean method BEFORE request params are processed .perform(EL.invoke("#{userProfileBean.preParameterSetup()}").before(Faces.decodeRequestParams())); } }
2. Resetting Bean Properties to Null Before Parameter Binding
This is a natural extension of the first requirement—simply add property reset logic directly inside your pre-binding method. Since this method runs before PrettyFaces injects URL parameters, any null assignments will be preserved unless the URL provides a value (which will overwrite the null).
Update the pre-binding method in your bean like this:
@UrlAction(phase = UrlAction.Phase.BEFORE_PARAMS) public void preParameterSetup() { // Reset properties to null this.userId = null; this.userName = null; // Add any other pre-binding logic you need System.out.println("Bean properties reset; ready for parameter binding."); }
Key Notes
- Bean Scope: For
ViewScopedorSessionScopedbeans, this reset ensures properties don’t carry over from previous requests/views. ForRequestScopedbeans, resetting is often redundant (since the bean is recreated each request) but still safe to include. - Optional Parameters: If your URL has optional parameters, resetting to null ensures unprovided parameters don’t retain old values from prior bean instances.
Hope this covers both your needs smoothly! Feel free to ask if you hit any specific edge cases or need further tweaks.
内容的提问来源于stack exchange,提问作者user9729396

