【问题标题】:Spring MVC message.properties issueSpring MVC message.properties 问题
【发布时间】:2018-02-23 14:44:55
【问题描述】:

我无法读取我的 messages.properties 文件(在/src/main/resources 下)

环境:Spring MVC 4.3.14

MyAppConfig.java:

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    return messageSource;
}

在我的 jsp (newuserregistration.jsp) 中,我有:

<form:form method="POST" modelAttribute="user" class="form-horizontal">


<div class="row">
<label class="col-md-3 control-label" for="userfirstname">First
    Name</label>
<div class="col-md-7">
    <form:input type="text" path="userfirstname" id="userfirstname"
class="form-control input-sm" />
    <div class="has-error">
        <form:errors path="userfirstname" class="help-inline" />
    </div>
</div>

在我的 css 中我有:

.has-error{
color:red;

}

实体类(AppUsers.java)有:

@Basic(optional = false)
@NotNull
@NotEmpty
//@Size(min = 1, max = 45)
@Column(name = "USERFIRSTNAME", nullable = false, length = 45)
private String userfirstname;

我的 messages.properties 有:

NotEmpty.user.userfirstname=First name can not be blank. 

控制器有:

 @Autowired
MessageSource messageSource;

    @RequestMapping(value = {"/registernewuser"}, method = RequestMethod.GET)
public String newUser(ModelMap model) {
    AppUsers user = new AppUsers();
    model.addAttribute("user", user);



 @RequestMapping(value = {"/registernewuser"}, method = RequestMethod.POST)
public String saveUser(@Valid AppUsers user, BindingResult result,
        ModelMap model) {
    model.addAttribute("user", user);
if (result.hasErrors()) {
        model.addAttribute("user", user);
        model.addAttribute("loggedinuser", getPrincipal());
        return "newuserregistration";
    }

不知何故错误消息没有显示在我的 jsp 上。我在这里错过了什么或做错了什么?

我为结果做了 sysout,它给了我 -

org.springframework.validation.BeanPropertyBindingResult: 1 errors  Field error in object 'appUsers' on field 'userfirstname': rejected value []; codes [NotEmpty.appUsers.userfirstname,NotEmpty.userfirstname,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [appUsers.userfirstname,userfirstname]; arguments []; default message [userfirstname]]; default message [may not be empty]

【问题讨论】:

标签: spring-mvc


【解决方案1】:

你能从资源文件中删除 NotEmpty.user 前缀吗?

资源文件message.properties

userfirstname=First name can not be blank.

【讨论】:

  • 我试过“userfirstname=名字不能为空”(不带引号)。没运气。结果还是一样。
  • 您是否尝试在@NotEmpty 验证中添加显式消息,即@NotEmpty(message = "NotEmpty.user.userfirstname")
  • 感谢您尝试提供帮助,但没有按照您在上次 cmets 中的建议进行操作。
【解决方案2】:

以下内容对我有用:

移除 model.addAttribute("user", user);并在我的 POST 方法中添加了 -- @ModelAttribute("user") -- 属性。这是工作的 sn-p:

@RequestMapping(value = {"/registernewuser"}, method = RequestMethod.POST)
public String saveUser(@ModelAttribute("user") @Valid AppUsers user, BindingResult result,
        ModelMap model) {
    if (result.hasErrors()) {
        model.addAttribute("loggedinuser", getPrincipal());
        return "newuserregistration";
    }

但是我仍然想知道最初的情况出了什么问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-11
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2011-01-11
    • 2011-06-10
    • 2013-08-21
    相关资源
    最近更新 更多