org.springframework.context.NoSuchMessageException: No message found under code 'required.name.dukeCommand.name' for locale 'en_US'

1,672 views
Skip to first unread message

RajaSekhar

unread,
Jun 30, 2014, 6:43:49 PM6/30/14
to JPassion Spring Group
Hi Sang,

I am getting below exception while doing - homework_spring3_mvc_form homework

1)Attached error log
2) DukeController code

package com.raja.examples.hellodukes.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;


import com.raja.examples.hellodukes.domain.Duke;
import com.raja.examples.hellodukes.validators.DukeValidator;

@Controller
@RequestMapping(value="/HelloDuke")
public class DukeController {
DukeValidator dukeValidator;
@Autowired
public DukeController(DukeValidator dukeValidator){
this.dukeValidator = dukeValidator;
}

@RequestMapping(method = RequestMethod.GET)
public String getCreateForm(ModelMap model){
Duke duke = new Duke();
// Set Name field
duke.setName("Raja");
// Set "Mastering" as default checked value
duke.setHobby("golf");
// Save the command object as "student" so that
// the "StudentForm" view access it.
model.addAttribute("dukeCommand", duke);
// Return form view
return "dukeForm";
}


//Handle form submission. 
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(
@ModelAttribute("dukeCommand") Duke duke,
BindingResult result, SessionStatus status) {
// Perform validation
dukeValidator.validate(duke, result);
if (result.hasErrors()) {
// If validation failed, display the form page again
return "dukeForm";
} else {
status.setComplete();
// If validation succeeded, return "StudentSuccess" 
return "dukeForm2";
}
}

}

3) DukeValidator Code:

package com.raja.examples.hellodukes.validators;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.raja.examples.hellodukes.domain.Duke;

public class DukeValidator implements Validator{

public boolean supports(Class clazz) {
//just validate the Student instances
return Duke.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
"required.name", "Field name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "hobby",
"required.hobby", "Field hobby is required.");
Duke cust = (Duke)target;
if((cust.getName().length() < 5 || cust.getName().length() > 10)){
errors.rejectValue("name", "required.name");
}
}
}

4) messages.properties

required.name = Username is required2!
required.hobby = Address is required!
length.name=name should be within 5 - 10 chars!


Please suggest where I mistaken.

Thanks
Raja

errorlog.png

Sang Shin

unread,
Jul 1, 2014, 4:41:59 PM7/1/14
to prs...@gmail.com, JPassion Spring Group
On 6/30/2014 6:43 PM, 'RajaSekhar' via JPassion.com: Spring Framework Programming wrote:
Hi Sang,

I am getting below exception while doing - homework_spring3_mvc_form homework

The error says

No message found under code 'required.name.dukeCommand.name' for locale 'en_US'
and your messages.properties below does not seem to have a message for
'required.name.dukeCommand.name'

required.name = Username is required2!
required.hobby = Address is required!
length.name=name should be within 5 - 10 chars!

-Sang

--
You received this message because you are subscribed to the Google Groups "JPassion.com: Spring Framework Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jpassion_spri...@googlegroups.com.
Visit this group at http://groups.google.com/group/jpassion_spring.
For more options, visit https://groups.google.com/d/optout.


-- 
-------------------------------------------------------------------
             Sang Shin, sangshi...@gmail.com
  Founder and Chief Instructor of JPassion.com (JavaPassion.com)
         http://www.linkedin.com/in/javapassion (Linkedin)
          http://twitter.com/javapassion (Tweeter)
            Life is worth living... with Passion!
    
   Practically Free 3 to 5 days Live, Hands-on, Online Codecamps on
 Java, HTML5, Ruby/Rails, Grails, JavaScript/jQuery, Spring, Android
             http://jpassion.com/codecamps       
----------------------------------------------------------------------

Sang Shin

unread,
Jul 2, 2014, 7:13:55 AM7/2/14
to RajaSekhar, JPassion Spring Group

It is hard to tell why the properties file is not being accessed just by looking at
your code unfortunately.

-Sang

On 7/1/2014 5:41 PM, RajaSekhar wrote:
When I set logs in Controller, for different errors in validators

1)
inside haserrors------org.springframework.validation.BeanPropertyBindingResult: 1 errors

Field error in object 'dukeC' on field 'name': rejected value []; codes [required.name.dukeC.name,required.name.name,required.name.java.lang.String,required.name]; arguments []; default message [Field name is required.]

2)
inside haserrors------org.springframework.validation.BeanPropertyBindingResult: 1 errors

Field error in object 'dukeC' on field 'name': rejected value [Raja]; codes [required.name.dukeC.name,required.name.name,required.name.java.lang.String,required.name]; arguments []; default message [null]

So, I changed the code as below and its working as alternative,

public class DukeValidator implements Validator{

public boolean supports(Class clazz) {
//just validate the Student instances
return Duke.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
"required.name", "Field name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "hobby",
"required.hobby", "Field hobby is required.");
Duke cust = (Duke)target;
if((cust.getName().length() < 5 || cust.getName().length() > 10)){
errors.rejectValue("name", "required.name","Name should be within 5 to 10 chars");

// added third param, ' default message' and its taking default message rather than from the message.properties.
}
}


here still my questions is : How to get error messages from properties ? Seems like they are not getting triggered.

Thanks
Raja


On Tue, Jul 1, 2014 at 10:06 PM, RajaSekhar <prs...@gmail.com> wrote:
Sir,

I have given 'required.name.dukeCommand.name' in messages.properties but no luck.

DukeValidator code is

package com.raja.examples.hellodukes.validators;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.raja.examples.hellodukes.domain.Duke;

public class DukeValidator implements Validator{

public boolean supports(Class clazz) {
//just validate the Student instances
return Duke.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
"required.name", "Field name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "hobby",
"required.hobby", "Field hobby is required.");
Duke cust = (Duke)target;
if((cust.getName().length() < 5 || cust.getName().length() > 10)){
errors.rejectValue("name", "required.name"); // the code is failing when this line gets executed. when I have given required.name why its not looking up for this particular message key ? . dukeCommand is messageAttribute given from Controller class. Im confused here.


}
}
}

RajaSekhar

unread,
Jul 4, 2014, 9:04:21 AM7/4/14
to Sang Shin, JPassion Spring Group
When I set logs in Controller, for different errors in validators

1)
inside haserrors------org.springframework.validation.BeanPropertyBindingResult: 1 errors

Field error in object 'dukeC' on field 'name': rejected value []; codes [required.name.dukeC.name,required.name.name,required.name.java.lang.String,required.name]; arguments []; default message [Field name is required.]

2)
inside haserrors------org.springframework.validation.BeanPropertyBindingResult: 1 errors

Field error in object 'dukeC' on field 'name': rejected value [Raja]; codes [required.name.dukeC.name,required.name.name,required.name.java.lang.String,required.name]; arguments []; default message [null]

So, I changed the code as below and its working as alternative,

public class DukeValidator implements Validator{

public boolean supports(Class clazz) {
//just validate the Student instances
return Duke.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
"required.name", "Field name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "hobby",
"required.hobby", "Field hobby is required.");
Duke cust = (Duke)target;
if((cust.getName().length() < 5 || cust.getName().length() > 10)){
errors.rejectValue("name", "required.name","Name should be within 5 to 10 chars");

// added third param, ' default message' and its taking default message rather than from the message.properties.
}
}


here still my questions is : How to get error messages from properties ? Seems like they are not getting triggered.

Thanks
Raja
On Tue, Jul 1, 2014 at 10:06 PM, RajaSekhar <prs...@gmail.com> wrote:
Sir,

I have given 'required.name.dukeCommand.name' in messages.properties but no luck.

DukeValidator code is

package com.raja.examples.hellodukes.validators;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.raja.examples.hellodukes.domain.Duke;

public class DukeValidator implements Validator{

public boolean supports(Class clazz) {
//just validate the Student instances
return Duke.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
"required.name", "Field name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "hobby",
"required.hobby", "Field hobby is required.");
Duke cust = (Duke)target;
if((cust.getName().length() < 5 || cust.getName().length() > 10)){
errors.rejectValue("name", "required.name"); // the code is failing when this line gets executed. when I have given required.name why its not looking up for this particular message key ? . dukeCommand is messageAttribute given from Controller class. Im confused here.


}
}
}
On Tue, Jul 1, 2014 at 9:41 PM, Sang Shin <sangshi...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages