如何修改Hybris Backoffice中对象的标识符显示
Hey there! Let’s tackle that issue where your Tile items show up as PKs in the Offer editor dropdown instead of a meaningful unique property. The reason overriding toString() didn’t work is because Backoffice doesn’t rely on Java’s toString() for UI labels—it uses item label providers or configuration-defined display properties instead. Here’s how to fix it:
1. Quick Fix: Use display-properties in items.xml
This is the simplest approach if you just want to use a single unique attribute (like tileCode or name) as the display label.
Edit your items.xml where the Tile item type is defined, adding a <display-properties> section pointing to your desired attribute:
<itemtype code="Tile" generate="true" autocreate="true"> <!-- Your existing attribute definitions here --> <attributes> <attribute qualifier="tileCode" type="java.lang.String"> <modifiers unique="true" optional="false"/> <persistence type="property"/> </attribute> <!-- Other Tile attributes... --> </attributes> <!-- Add this section --> <display-properties> <property name="tileCode"/> <!-- Replace with your unique property name --> </display-properties> </itemtype>
After making this change:
- Run
ant clean allto build your extensions - Update your Hybris system (via HAC or
ant updatesystem) - Restart Backoffice—your dropdown should now show the
tileCodeinstead of the PK.
2. Custom Label for Complex Displays (e.g., Multiple Attributes)
If you need to combine multiple attributes (like tileCode + " - " + title) for the label, create a custom ItemLabelProvider:
Step 1: Create the Label Provider Class
package com.yourcompany.backoffice.labelproviders; // Use your actual package import com.hybris.cockpitng.labels.impl.AbstractItemLabelProvider; import com.yourcompany.core.model.TileModel; // Your Tile model class public class CustomTileLabelProvider extends AbstractItemLabelProvider { @Override public String getLabel(final Object item) { if (item instanceof TileModel) { final TileModel tile = (TileModel) item; // Return your custom formatted label here return tile.getTileCode() + " - " + tile.getTitle(); } // Fallback to default for non-Tile items return super.getLabel(item); } }
Step 2: Register the Provider in backoffice-config.xml
Add this context configuration to your extension’s backoffice-config.xml:
<context type="Tile" component="labelProvider"> <bean class="com.yourcompany.backoffice.labelproviders.CustomTileLabelProvider" parent="abstractLabelProvider"/> </context>
Rebuild, update the system, and restart Backoffice—your dropdown will now show the combined label.
3. Check Editor-Specific Configuration
If you’ve configured the Offer editor explicitly, make sure it’s not overriding the display property. Look for this in your backoffice-config.xml:
<context type="Offer" component="editor"> <editor:editor xmlns:editor="http://www.hybris.com/cockpitng/component/editor"> <editor:attribute qualifier="tiles"> <editor:editor type="com.hybris.cockpitng.editor.defaultlisteditor"> <editor:parameter> <editor:name>displayProperty</editor:name> <editor:value>tileCode</editor:value> <!-- Match your desired property --> </editor:parameter> </editor:editor> </editor:attribute> </editor:editor> </context>
If this exists, ensure the displayProperty value matches the attribute you want to show.
Why toString() Didn’t Work
Hybris Backoffice’s UI components (like dropdowns and lists) use dedicated label providers to fetch display text, not the Java object’s toString() method. Unless you explicitly reference toString() in a custom label provider, overriding it won’t affect how Backoffice renders items.
内容的提问来源于stack exchange,提问作者majki

