Results 1 to 5 of 5

Thread: How to access resourceBundle inside the Validator

  1. #1
    Join Date
    Oct 2008
    Posts
    286

    Default [SOLVED]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 at 07:21 AM. Reason: tag as SOLVED
    Eros

    Environment:
    JSP 2.0
    Dojo 1.4.1
    Ext JS 3.1 (testing)
    Spring MVC 2.5.6.SEC01 (planning to Spring 3 using STS)
    STS
    SWF 2.0.9.RELEASE
    Tiles 2.0.5
    iBatis: ibatis-sqlmap-2.3.4.726

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,630

    Default

    Why? That is already handled by the framework, simply set the error code and the message will be resolved for you...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2008
    Posts
    286

    Default

    Quote 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 at 06:02 AM.
    Eros

    Environment:
    JSP 2.0
    Dojo 1.4.1
    Ext JS 3.1 (testing)
    Spring MVC 2.5.6.SEC01 (planning to Spring 3 using STS)
    STS
    SWF 2.0.9.RELEASE
    Tiles 2.0.5
    iBatis: ibatis-sqlmap-2.3.4.726

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,630

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Oct 2008
    Posts
    286

    Default

    Quote 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"
    Eros

    Environment:
    JSP 2.0
    Dojo 1.4.1
    Ext JS 3.1 (testing)
    Spring MVC 2.5.6.SEC01 (planning to Spring 3 using STS)
    STS
    SWF 2.0.9.RELEASE
    Tiles 2.0.5
    iBatis: ibatis-sqlmap-2.3.4.726

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •