Announcement Announcement Module
Collapse
No announcement yet.
How to access resourceBundle inside the Validator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to access resourceBundle inside the Validator

    i created a Validator for my model..
    then my problem is on how to access my resourceBundle in order to use as my error messages..

    Code:
    @Component
    public class RosenkaSanshutsuViewValidator extends CommonValidator {
    
    	public void validateEnterRosenka (RosenkaSanshutsuView view, ValidationContext context) {
    		MessageContext messages = context.getMessageContext();
    		Message[] msgList = messages.getAllMessages();
                   //msgList1 is null
    		Message[] msgList1 = messages.getMessagesBySource(null);
    	}
    	
    }
    xml settings
    Code:
        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        	<property name="basename" value="WEB-INF/classes/messages"/>
        	<property name="defaultEncoding" value="UTF-8"></property>
        </bean>
    messages.properties
    Code:
    EMCC00001=sample error message1
    
    EVY100001=sample error message2
    my target folder:
    target/webproj/WEB-INF/classes/messages.properties
    Last edited by eros; Mar 16th, 2010, 07:21 AM. Reason: tag as SOLVED

  • #2
    Why? That is already handled by the framework, simply set the error code and the message will be resolved for you...

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      Why? That is already handled by the framework, simply set the error code and the message will be resolved for you...
      i am using a non-standard message code that's why I am looking for other way that allowing me to pass the code then return back the message.

      i injected the messageSource inside my validator in order to have access with my resourceBundle...

      messages.properties
      Code:
      EMCC00001=Item1 is required
      EVY100001=Item2 is required
      validator
      Code:
      @Component
      public class RosenkaSanshutsuViewValidator {
      
          @Autowired
          private MessageSource messageSource;
      
          private MessageSourceAccessor accessor;
          
      	/**
      	 * @param messageSource the messageSource to set
      	 */
      	public void setMessageSource(MessageSource messageSource) {
      		this.accessor = new MessageSourceAccessor(messageSource);
      	}
      
      	public void validateEnterRosenka (RosenkaSanshutsuView view, ValidationContext context) {
      		MessageContext messages = context.getMessageContext();
      
      		messages.addMessage(new MessageBuilder().error().source("selectedTaishoHaniSentei.analyzeId").defaultText(DEFAULT_MESSAGE).build());
      		
      	}
      	
      }
      domain
      Code:
      public class RosenkaSanshutsuView extends AbstractComplex {
          
      	private SelectedTaishoHaniSentei selectedTaishoHaniSentei = new SelectedTaishoHaniSentei();  
          
      	/**
      	 * @param selectedTaishoHaniSentei the selectedTaishoHaniSentei to set
      	 */
      	public void setSelectedTaishoHaniSentei(SelectedTaishoHaniSentei selectedTaishoHaniSentei) {
      		this.selectedTaishoHaniSentei = selectedTaishoHaniSentei;
      	}
      
      	/**
      	 * @return the selectedTaishoHaniSentei
      	 */
      	public SelectedTaishoHaniSentei getSelectedTaishoHaniSentei() {
      		return selectedTaishoHaniSentei;
      	}
      
      }
      
      public class SelectedTaishoHaniSentei extends AbstractComplex {
      
      	private String analyzeId = "";
      	
      	private String joruiNo = "";
      	
      	/**
      	 * @return the analyzeId
      	 */
      	public String getAnalyzeId() {
      		return analyzeId;
      	}
      
      	/**
      	 * @param analyzeId the analyzeId to set
      	 */
      	public void setAnalyzeId(String analyzeId) {
      		this.analyzeId = analyzeId;
      	}
      
      	/**
      	 * @return the joruiNo
      	 */
      	public String getJoruiNo() {
      		return joruiNo;
      	}
      
      	/**
      	 * @param joruiNo the joruiNo to set
      	 */
      	public void setJoruiNo(String joruiNo) {
      		this.joruiNo = joruiNo;
      	}
      
      }
      Last edited by eros; Mar 16th, 2010, 06:02 AM.

      Comment


      • #4
        You haven't specified the code, you only specified the source. which then generates its own codes. Simple add (code("yourcode") to the message builder and your code should be used.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          You haven't specified the code, you only specified the source. which then generates its own codes. Simple add (code("yourcode") to the message builder and your code should be used.
          thanks a lot... it helps me a lot..

          here's the resolution... adding code()
          Code:
          messages.addMessage(new MessageBuilder().error().source("selectedTaishoHaniSentei.analyzeId").code("EMCC00001").defaultText(DEFAULT_MESSAGE).build());
          I will tag this thread as SOLVED.

          *
          actually my almost 2 days problem that I can't solve it due to lack of Spring Foundation...

          from Flow -> there is an evaluationException and don't know the main reason...I've check one by one on how to set multiple eventId within a form..
          please check it out also... thanks in advanced..

          "evaluationException on eventId_view-rltRosenka"

          Comment

          Working...
          X