Spring MVC form:select selected value?

后端 未结 6 1400
遇见更好的自我
遇见更好的自我 2021-02-19 09:56

Is there any way to select current value in dropdown list by Spring MVC by ?

6条回答
  •  面向向阳花
    2021-02-19 10:29

    I had similar problem and after several days of battling with it, I was able to fix it by implementing hash and equal methods in my model class. The problem is that spring was not able to determine where an item in the drop down is equals to a value in the model. But after implementing the hash and equals in the model object, everything worked fine.

    @Entity
    @Table(name = "BANKS")
    public class Bank implements java.io.Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = -8928809572705999915L;
    
    private Long id;
    
    private String bankCode;
    
    private String bankName;
    
    ...........
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((bankCode == null) ? 0 : bankCode.hashCode());
        result = prime * result
                + ((bankName == null) ? 0 : bankName.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Bank other = (Bank) obj;
        if (bankCode == null) {
            if (other.bankCode != null)
                return false;
        } else if (!bankCode.equals(other.bankCode))
            return false;
        if (bankName == null) {
            if (other.bankName != null)
                return false;
        } else if (!bankName.equals(other.bankName))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
    

    And in the view i have something like this

        
            --Select--
            
        
    

提交回复
热议问题