Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4830

Override TextProvider doesnot work in struts 2.5.12

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Not A Problem
    • Affects Version/s: 2.5.12
    • Fix Version/s: 2.5.13
    • Component/s: Core
    • Labels:
      None

      Description

      In struts 2.3 , to override the TextProvider we used below

      
      	<constant name="struts.xworkTextProvider" value="DefaultTextProvider" />
      	<bean type="com.opensymphony.xwork2.TextProvider" name="DefaultTextProvider" class=".utils.CustomTextProvider" scope="default" />
      
      

      And make CustomTextProvider

      public class CustomTextProvider extends DefaultTextProvider{
      
      public String getText(String key, String defaultValue, List<?> args) {
              String text = super.getText(key, defaultValue, args);
              //Do something with the text
              //and return it
          }
      
       //other getText methods can be override too
      }
      

      This seems not to be worked at Struts 2.15.2.

      To make some test, I remove my bean from* strust.xml* and put some break points in *DefaultTextProvider *the *getText *of this class is not called, however I find that the *TextProviderSupport **getText *method is called.

      Should we override other classes ?!

        Activity

        Hide
        lukaszlenart Lukasz Lenart added a comment -

        You should use TextProviderFactory instead, the DefaultTextProvider is only used internally as a fallback solution a which soon should also be dropped.

        Here you have an example how to implement your custom factory
        https://github.com/apache/struts-examples/blob/master/text-provider/

        Here is some more details about that
        https://struts.apache.org/docs/localization.html#Localization-CustomTextProviderandTextProviderFactory

        Show
        lukaszlenart Lukasz Lenart added a comment - You should use TextProviderFactory instead, the DefaultTextProvider is only used internally as a fallback solution a which soon should also be dropped. Here you have an example how to implement your custom factory https://github.com/apache/struts-examples/blob/master/text-provider/ Here is some more details about that https://struts.apache.org/docs/localization.html#Localization-CustomTextProviderandTextProviderFactory
        Hide
        afattahi Alireza Fattahi added a comment - - edited

        It worked, the sample helped a lot.

        I will put some code here for others who may read

        <constant name="struts.xworkTextProvider" value="DefaultTextProvider" />
        <bean type="com.opensymphony.xwork2.TextProvider" name="DefaultTextProvider" class="utils.CustomTextProvider" scope="default" />
        
        
        

        And

        
        public class CustomStrutsTextProviderFactory extends StrutsTextProviderFactory {
        	
        	@Override
        	protected TextProvider getTextProvider(Class clazz) {
                       //If you want to optionaly add some resource bundles dynamically you can do it here
        		//localizedTextProvider.addDefaultResourceBundle("messages/label");
        		//localizedTextProvider.addDefaultResourceBundle("messages/customerA/label");
        		return new CustomTextProvider(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
        	}
        
        	@Override
        	protected TextProvider getTextProvider(ResourceBundle bundle) {
                         //If you want to optionaly add some resource bundles dynamically you can do it here
        		//localizedTextProvider.addDefaultResourceBundle("messages/label");
        		//localizedTextProvider.addDefaultResourceBundle("messages/customerA/label");
        
        		return new CustomTextProvider(bundle, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
        	}
        }
        
        

        And the CustomTextProvider which you can play with get text.

        
        public class CustomTextProvider extends TextProviderSupport {
        
        	public CustomTextProvider(Class<?> clazz, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) {
        	        super(clazz, provider, localizedTextProvider);
        	    }
        
        	public CustomTextProvider(ResourceBundle bundle, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) {
        	        super(bundle, provider, localizedTextProvider);
        	}
        
                public String getText(String key, String defaultValue, List<?> args) {
                    String text = super.getText(key, defaultValue, args);
                    //Do some thing with text
                  //for example return text.trim();
              }
        
           //Override other getText if you need
        
        
        

        Please correct me. Thanks for the help

        Show
        afattahi Alireza Fattahi added a comment - - edited It worked, the sample helped a lot. I will put some code here for others who may read <constant name= "struts.xworkTextProvider" value= "DefaultTextProvider" /> <bean type= "com.opensymphony.xwork2.TextProvider" name= "DefaultTextProvider" class= "utils.CustomTextProvider" scope= "default" /> And public class CustomStrutsTextProviderFactory extends StrutsTextProviderFactory { @Override protected TextProvider getTextProvider( Class clazz) { //If you want to optionaly add some resource bundles dynamically you can do it here //localizedTextProvider.addDefaultResourceBundle( "messages/label" ); //localizedTextProvider.addDefaultResourceBundle( "messages/customerA/label" ); return new CustomTextProvider(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider); } @Override protected TextProvider getTextProvider(ResourceBundle bundle) { //If you want to optionaly add some resource bundles dynamically you can do it here //localizedTextProvider.addDefaultResourceBundle( "messages/label" ); //localizedTextProvider.addDefaultResourceBundle( "messages/customerA/label" ); return new CustomTextProvider(bundle, localeProviderFactory.createLocaleProvider(), localizedTextProvider); } } And the CustomTextProvider which you can play with get text. public class CustomTextProvider extends TextProviderSupport { public CustomTextProvider( Class <?> clazz, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) { super (clazz, provider, localizedTextProvider); } public CustomTextProvider(ResourceBundle bundle, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) { super (bundle, provider, localizedTextProvider); } public String getText( String key, String defaultValue, List<?> args) { String text = super .getText(key, defaultValue, args); //Do some thing with text // for example return text.trim(); } //Override other getText if you need Please correct me. Thanks for the help

          People

          • Assignee:
            Unassigned
            Reporter:
            afattahi Alireza Fattahi
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development