0

我使用带有休眠验证器的spring mvc。我的问题是,当整数值为零时,它在我的文本框中显示 0。我只想显示空字符串。怎么做?

在我的 JSP 中:

<form:form action ="processForm" modelAttribute="customer" >
...
...
Free Passes: <form:input path="freePasses"/>
<form:errors path="freePasses" cssClass="error" />
<input type="submit" value="submit" />
</form:form>

我的客户类别:

public class Customer {
...
private int freePasses;
}

我的控制器

public class CustomerController {
@RequestMapping("/showForm")
public String showForm(Model model) {
    model.addAttribute("customer",new Customer());
    return "customer-form";
}

@RequestMapping("/processForm")
public String processForm(@Valid @ModelAttribute("customer") Customer cust,
        BindingResult result) {
    if(result.hasErrors()) {
        return "customer-form";
    } else {
        return "customer-confirmation";
    }
}
}

当表单被加载时,我希望在免费通行证字段中看到空而不是零。

结果我得到

预期结果

4

2 回答 2

0

使用<c:if>JSP JSTL(java 标准标签库)if 条件标签并检查值 0 的条件并将值设置为“”空字符串,如下所示。

<c:if test = "${customer.freePasses == 0}">
   //put text area with empty string
</c:if>

参考https://www.tutorialspoint.com/jsp/jstl_core_if_tag.htm

于 2019-04-28T14:15:16.237 回答
0

您正在使用int默认为 0 的原始类型。

私人 int freePasses;

我建议改用 Wrapper Integer,这可以解决您的问题。

私人整数免费通行证;

注意:一旦从 int 更改为 Integer,请不要忘记重新生成 getter/setter

于 2019-04-29T08:23:57.697 回答