NinjaBoy
NinjaBoy

Reputation: 3755

Spring MVC - How to display blank textbox when integer value is zero

Im using spring, hibernate, java, and jsp. My problem is that when the integer value is zero it displays 0 in my textbox. I want to display only empty string but Idk how to do it. Please help.

In my jsp:

<form:form method="POST" commandName="division">
...
...
    <form:input path="number"/>
</form:form>

In my domain:

/**
 * Get the number of the division.
 * @return The number.
 */
@Column(name = "NUMBER")
public int getNumber() {
    return number;
}

/**
 * Set the number of the division.
 * @param number The division number.
 */
public void setNumber(int number) {
    this.number = number;
}

Upvotes: 6

Views: 3316

Answers (1)

Raul Rene
Raul Rene

Reputation: 10270

You will have to use spring:bind for that.

Also, you will have to use JSTL. Import it with:

<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core"%>

In order to get the value for number:

<spring:bind path="number">

The result of the spring:bind is returned in a variable called status, in its value field. Check if it is 0 and print nothing, else print the number:

<core:if test="${status.value != 0}">
    ${status.value}    
</core:if>

For more information, take a look at the spring:bind documentation.

Upvotes: 5

Related Questions