java - gvNIX 典型安全性 : no feedback in signup form UI of binding errors

标签 java spring-roo gvnix

使用典型安全性时,注册表单中的错误不会显示在表单中。没有任何关于问题所在的反馈,只有一个空白的屏幕。

我在这个问题的底部提供了空表单的解决方案。

但问题仍然是如何获取表单中显示的绑定(bind)结果验证错误。就像“创建新用户”表单中一样。

我正在使用添加了 gvNIX 的 Spring 2.0.0.M1!

我已经创建了一个issue at github

我的问题:是否有一个简单的解决方法可以使其正常工作。

测试脚本:

// Create a new project
project setup --topLevelPackage com.springsource.pizzashop --projectName pizzashop

jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY

entity jpa --class ~.domain.Pizza
field string --fieldName name --notNull --sizeMin 10
field number --fieldName price --notNull --type java.math.BigDecimal

// Adding web layers
web mvc setup
web mvc all --package ~.web

// Adding Spring security
typicalsecurity setup

// Adding JQuery, Datatables and Bootstrap
web mvc jquery setup
web mvc jquery all
web mvc datatables setup
web mvc bootstrap setup

web mvc bootstrap update

更改密码字段所需的长度:

在文件...\src\main\java\com\springsource\pizzashop\domain\User.java

@NotNull
@Size(min = 8) // was (min = 1)
private String password;

在文件 ..\src\main\java\com\springsource\pizzashop\web\UserRegistrationForm.java 中应用与上面相同的更改。

@NotNull
@Size(min = 8) // was (min = 1)
private String password;

在文件 ...\src\main\java\com\springsource\pizzashop\web\SignUpController.java 中添加 1 行调试信息

@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String create(@Valid UserRegistrationForm userRegistration, BindingResult result, Model model, HttpServletRequest request) {
    validator.validate(userRegistration, result);

    System.out.println("Has binding errors: " + result.toString());  // added

    if (result.hasErrors()) {

        return createForm(model);
    } else {

    // Rest of the code

当注册表单中的密码字段仅填写一个字母而不是 8 个字母时,验证码错误,调试行将显示以下消息:

Has binding errors: org.springframework.validation.BeanPropertyBindingResult: 3 errors

Field error in object 'userRegistrationForm' on field 'repeatPassword': rejected value [a]; codes [Size.userRegistrationForm.repeatPassword,Size.repeatPassword,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userRegistrationForm.repeatPassword,repeatPassword]; arguments []; default message [repeatPassword],2147483647,8]; default message [size must be between 8 and 2147483647]

Field error in object 'userRegistrationForm' on field 'password': rejected value [a]; codes [Size.userRegistrationForm.password,Size.password,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userRegistrationForm.password,password]; arguments []; default message [password],2147483647,8]; default message [size must be between 8 and 2147483647]

Error in object 'userRegistrationForm': codes [recaptcha.mismatch.userRegistrationForm,recaptcha.mismatch]; arguments []; default message [null]

这是正确的,但是用户在屏幕上看到的形式没有显示任何反馈! 因此,对于用户来说,不清楚出了什么问题以及为什么表单没有保存但又回来了。 用户看到的只是一个空的注册表单,没有任何信息。

enter image description here

我期望的是一条类似于“创建新用户”中显示的消息,该密码字段也填充了一个字母。比红色显示的消息:大小必须介于 8 和 2147483647 之间

enter image description here

空屏的解决方案: 空白屏幕的解决方案是返回到表单,其中包含用户按下“保存”时提供的信息。这与 Spring Roo/gvNIX 的其他创建和更新方法中的行为相同。

@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String create(@Valid UserRegistrationForm userRegistration, BindingResult result, Model model, HttpServletRequest request) {
    validator.validate(userRegistration, result);
    if (result.hasErrors()) {
            model.addAttribute("User", userRegistration);  // Added: the original info
        return "signup/index";
        //return createForm(model);  // original, commented out.
    } else {

这解决了空屏的问题,用户可以看到他/她提供的信息。 但是,我仍然看不到绑定(bind)结果的红色反馈。

如何使其可见?

最佳答案

这是在 gvNIX 2.0.0.M1 上发现的错误,将在 gvNIX 项目的 future 版本中修复。

要显示这些错误消息,您必须对项目进行以下更改:

在文件 ~/webapp/WEB-INF/tags/jquery/form/create.tagx (第 ~30 行)上,您必须编辑 form 元素的 cssClass 属性像这样:

<form:form cssClass="form-horizontal validate" ... >

将红色样式添加到错误范围,在文件 ~/webapp/styles/standard.css 中添加以下代码:

.errors {
    color: #b94a48;
    display: block;
}

要将此选项应用于密码输入,您必须编辑文件 ~/WEB-INF/tags/jquery/form/fields/input.tagx,将所有这些选项添加到密码输入中。只需将它们全部写在第 ~112 行,如下所示:

          <form:password class="form-control input-sm" id="_${sec_field}_id" path="${sec_field}" disabled="${disabled}" size="${size}"
          data-required="${required}" data-invalid="${field_invalid}" data-missing="${sec_field_required}"
          data-regex="${validationRegex}" data-minlength="${min}" data-maxlength="${max}"
          data-mindecimal="${decimalMin}" data-maxdecimal="${decimalMax}" value="${value}"/>

请尝试一下并告诉我它是否对您有用。

We will take in mind this problems for future versions of gvNIX

希望这有帮助。问候。

关于java - gvNIX 典型安全性 : no feedback in signup form UI of binding errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34565674/

相关文章:

java - (Jackson) 如何使用另一个对象的属性将 JSON 反序列化为具有多态性的 POJO?

java - 将 watch 置于断点时,Java 调试器显示盒装整数而不是原始整数

database - 忽略数据库 Spring Roo 中的字段

java - websphere 7(和 Spring Roo)与 javax.el.E​​LException 不兼容

java - Spring Roo 项目找不到 org.springframework.roo.annotations :jar:1. 2.5.BUILD-SNAPSHOT

javascript - 实现步骤进度条

swing - Java SE 二进制崩溃

java - 需要帮助理解 logcat 消息 ClassCastException

java - Spring Roo 最佳实践显示本地时间日期

java - 无法使用 gvNIX/Roo 创建仅 Web 服务的项目