Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8114715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:14:01+00:00 2026-06-06T03:14:01+00:00

I have the following beans: public class CodedEntity { private String name; private String

  • 0

I have the following beans:

public class CodedEntity {
    private String name;
    private String code;
    // getters and setters
}

public class MyProduct {
    @javax.validation.constraints.Size(min = 1)
    private List<CodedEntity> codedEntities;
    // getters and setters
}

As you see the codedEntities list must have at least one element.

In my controller I validate the size of the list like this :

@RequestMapping(value = "/**", method = RequestMethod.POST)
public String submit(@Valid final MyProduct myProduct, final BindingResult result,
        final Model model) {
    // ...
}

And finally my jsp :

<c:forEach items="${codedEntitiesList}" var="codedEntity" varStatus="loopStatus"        
    <form:checkbox path="myProduct.codedEntities[${loopStatus.index}]" value="${codedEntity.code}" cssClass="checkbox" />
</c:forEach>

The problem here is the following : when I submit the jsp without checking any checkbox, spring mvc returns a non-empty list containing “null” elements. And because of this, the validation fails.

How can I tell spring-mvc to return an empty list instead of a list full of “null” elements ? Or at least is there any way I can validate my constraint ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:14:04+00:00Added an answer on June 6, 2026 at 3:14 am

    I finally found a solution. I don’t know if it’s the best one, but it’s certainly a good one.

    First of all, the jsp has to be modified like this :

    <c:forEach items="${codedEntitiesList}" var="codedEntity" varStatus="loopStatus"        
        <form:checkbox path="myProduct.codedEntities" value="${codedEntity}" />
    </c:forEach>
    

    Than you have to create a custom editor :

    public class CodedEntityEditor extends PropertyEditorSupport {
        @Override
        public void setAsText(final String text) {
            final CodedEntity codedEntity = new CodedEntity();
            codedEntity.setCode(text);
            this.setValue(codedEntity);
        }
    
        @Override
        public String getAsText() {
            return ((CodedEntity) this.getValue()).getCode();
        }
    }
    

    Of course, do not forget to bind the CodedEntityEditor to the Controller :

    @InitBinder
    public void initBinder(final WebDataBinder binder) {
        binder.registerCustomEditor(CodedEntity.class, new CodedEntityEditor());
    }
    

    Finally, you have to implement the hashCode() and equals() methods in the CodedEntity object. You have to do this because the getAsText method in the CodedEntityEditor retrieves the object by its reference.
    If you implement those 2 methods, it will retrieve it on a specific field (the “code” field in my case). To make it easy I suggest to use an IDE like Eclipse to generate those automatically.
    Just for the example, here are the 2 methods generated by Eclipse :

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.code == null) ? 0 : this.code.hashCode());
        return result;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (this.getClass() != obj.getClass())
            return false;
        final CodedEntity other = (CodedEntity) obj;
        if (this.code == null) {
            if (other.code != null)
                return false;
        } else if (!this.code.equals(other.code))
            return false;
        return true;
    }
    

    Of course you can skip those 2 methods, but in this case the checkboxes won’t be checked when you’ll reload the page !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have the following java bean: public class MyBean { private List<String> names
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have the following 2 entities: @Entity(name = Employee) @Table(name = EMPLOYEE) public class
If I have the following class: package com.example; import org.springframework.beans.factory.annotation.Required; public class Customer {
Say I have the following simple java bean: class MyBean { private Date startDate;
Imagine that i have the following code: <a:repeat value=#{bean.getList()} var=x > <li class=la> <span>
I have the following code with a simple class and a method for writing
I have the following configuration: @Aspect public class MyAspect { @Around(@annotation(SomeAnnotation)) public Object myMethod(ProceedingJoinPoint
(Java Spring WebApp) I have: public class PersonValidator implements Validator { private PersonDAO d;
I have the following class: @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = true) public class TclRequest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.