Liferay 7 自定义资产分类行为:分类选择面板改写困境求助
Hey there, let's break down the problems you're facing with overriding the Asset Category Selector's JSPs using an OSGI Fragment in Liferay 7, and walk through how to fix them.
First, Let's Recap Your Core Issues
You've built a Fragment module targeting com.liferay.asset.categories.selector.web, but run into these roadblocks:
- Only changes to
view.jsptake effect - Modifying
init.jsporinit-ext.jspdoesn't alter the interface at all - Deleting
view.jsptriggers the "Asset category selector is temporarily unavailable" error
Key Rules for OSGI Fragment JSP Overrides in Liferay
Before diving into fixes, let's align on how Liferay handles Fragment-based resource overrides—getting these basics right solves most issues:
- Your Fragment's
bnd.bndmust correctly target the host bundle viaFragment-Host, including a matching version range - The file structure in your Fragment must mirror the target bundle exactly (same directory paths for all JSPs)
- Liferay's JSP engine prioritizes Fragment resources, but initialization JSPs like
init.jsphave strict loading order dependencies—if your references are off, your changes won't be picked up.
Step-by-Step Fixes for Your Scenario
1. Why Aren't init.jsp/init-ext.jsp Changes Working?
The root cause here is almost always incorrect file paths or misconfigured references in your view.jsp. Here's how to fix it:
- Verify file paths: Ensure your Fragment has
init.jspandinit-ext.jspatMETA-INF/resources/init.jspandMETA-INF/resources/init-ext.jsp—this matches exactly where they live in the originalcom.liferay.asset.categories.selector.webbundle. - Check
view.jspincludes: The originalview.jspstarts with<%@ include file="/init.jsp" %>and<%@ include file="/init-ext.jsp" %>. Your Fragment'sview.jspmust keep these exact include paths—if you change them (even accidentally), it'll load the original bundle's init files instead of yours. - Validate
bnd.bndconfiguration: Double-check your Fragment'sbnd.bndto make sure it's correctly targeting the host bundle. For example:
Use Liferay's Gogo shell (Bundle-SymbolicName: com.liferay.asset.categories.selector.web.custom.fragment Fragment-Host: com.liferay.asset.categories.selector.web;bundle-version="[7.4.0,8.0.0)" Bundle-Version: 1.0.0lb com.liferay.asset.categories.selector.web) to get the exact version range of the host bundle—using the wrong range can prevent your Fragment from attaching properly.
2. Why Does Deleting view.jsp Cause Errors?
When you remove view.jsp from your Fragment, Liferay falls back to the original bundle's view.jsp. But that original JSP expects its own init.jsp/init-ext.jsp (or any other dependencies) to be present as they were. If you've modified those init files in your Fragment, the original view.jsp will hit missing variables or resources, triggering the error.
The fix: Never delete view.jsp from your Fragment. Even if you want to reuse most of the original logic, keep a copy in your Fragment and modify only the parts you need. This ensures the include paths point to your customized init files, and there's no mismatch between the JSP and its dependencies.
3. Pro Tips for Reliable Overrides
- Clear caches religiously: Liferay caches JSPs aggressively. After deploying your Fragment, clear both the OSGI bundle cache (in
[Liferay Home]/osgi/state) and your browser cache to ensure you're seeing the latest changes. - Verify Fragment attachment: Use the Gogo shell command
lb com.liferay.asset.categories.selector.webto confirm your Fragment is listed as attached to the host bundle (look for the "Fragments" column showing your Fragment's symbolic name). - Test incrementally: Start with small changes to
view.jspto confirm your Fragment is working, then move to modifyinginit.jsp/init-ext.jsponce you know the basic setup is correct.
Next Steps for Your Custom Category Integration
Once you've got the Fragment working, remember to update the JSP logic to pull data from your custom category table instead of the default AssetCategory table. You'll need to reference your custom service (e.g., CustomAssetCategoryLocalService) in the init files and adjust the UI components to use your data.
Hope this gets you past the Fragment roadblock! If you hit specific errors or have questions about the service integration part, feel free to share more details.
内容的提问来源于stack exchange,提问作者Mave751

