cancel
Showing results for 
Search instead for 
Did you mean: 

Display category name instead of category code

former_member658721
Participant
0 Kudos

Hello,

Could any of you help me how to display the category name instead of category number / code in the URL? I want to display the meaning full name of category in the URL.

http://localhost:9001/Category/Servers/c/10031

If you see 10031 is the category code and instead of that need to display name like this..

http://localhost:9001/Category/Servers/c/flat-bed

Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi Sasi

You need to customize the resolveInternal method of the DefaultCategoryModelUrlResolver.java. This method is responsible for the generation of the URL. Please modify the code snippet as shown below:

 @Override
     protected String resolveInternal(final CategoryModel source)
     {
         // Work out values
 
         // Replace pattern values
         String url = getPattern();
         if (url.contains("{baseSite-uid}"))
         {
             url = url.replace("{baseSite-uid}", urlEncode(getBaseSiteUid().toString()));
         }
         if (url.contains("{category-path}"))
         {
             final String categoryPath = buildPathString(getCategoryPath(source));
             url = url.replace("{category-path}", categoryPath);
         }

             //Modification starts
             if (url.contains("{category-code}"))
         {
             final String categoryName = urlEncode(source.getName()).replaceAll("\\+", "-");
             url = url.replace("{category-code}", **categoryName**);
         }
             //Modification ends



         if (url.contains("{catalog-id}"))
         {
             url = url.replace("{catalog-id}", urlEncode(source.getCatalogVersion().getCatalog().getId()));
         }
         if (url.contains("{catalogVersion}"))
         {
             url = url.replace("{catalogVersion}", urlEncode(source.getCatalogVersion().getVersion()));
         }
 
         return url;
 
     }



Please let me know you still have any issues.

former_member658721
Participant
0 Kudos

Hi Vikrant,

I changed the logic to use the name. Now getting black screen. The url shows the category name. The site is working fine with the codes but if I use name getting blank screen. Do I need to change anything on top of overriding the above method with name?

If change the code to name then the query is called with name using code column which is not going to return any data.

Thanks in advance.

0 Kudos

Hi Sasi

Post you have changed the URL, you need to make changes in the way the controller logic is implemented. Go to the category page controller and make changes so that the category is queried on the basis of name instead of code. Please do not forget to set the locale of the name when querying the db.

Answers (3)

Answers (3)

former_member658721
Participant
0 Kudos

Thank you for your response Vikrant.

former_member658721
Participant
0 Kudos

Thanks for your quick response.

former_member618655
Active Participant
0 Kudos

Hi SasiReddy

Category and supercategory name gets appended to the URL when the URL get rewritten in the following method in AbstractCategoryPageController

 protected String performSearchAndGetResultsPage

However, if you choose to change the logic here and wish to display category name after /c/ then you will have to change your categoryPageController and change below method

 @RequestMapping(value =
     { CATEGORY_CODE_PATH_VARIABLE_PATTERN, "/**" + CATEGORY_CODE_PATH_VARIABLE_PATTERN }, method = RequestMethod.GET)
     public String category(@PathVariable("categoryCode") final String categoryCode, // NOSONAR

Change the value from categoryCode to categoryName and query the DB with categoryName instead.

Note: I personally do not recommend this approch. It is best to check with your SEO team first before doing this kind of change