Spring MVC Dropdown-Boxbeispiel

Beispiel für eine Spring MVC Dropdown-Box

In Spring MVC werden Formular-Tags -<form:select />,<form:option /> oder<form:options /> - zum Rendern des HTML-Dropdown-Felds verwendet. Siehe folgende Beispiele:

//SimpleFormController
protected Map referenceData(HttpServletRequest request) throws Exception {
    Map referenceData = new HashMap();
    Map country = new LinkedHashMap();
    country.put("US", "United Stated");
    country.put("CHINA", "China");
    country.put("SG", "Singapore");
    country.put("MY", "Malaysia");
    referenceData.put("countryList", country);
}

1.

Generieren Sie eine Dropbox-Box mit $ {countryList}.

HTML Quelltext

2.

Das muss dem select-Tag beigefügt sein.


    

HTML Quelltext

3.

Die<form:option />müssen ebenfalls dem Auswahl-Tag beigefügt sein und eine einzelne Auswahloption rendern (siehe folgende Kombination).


   
   

HTML Quelltext

4. Listenfeld

Um ein Listenfeld zu rendern, fügen Sie einfach das Attribut "multiple=true" in das Auswahl-Tag ein.

HTML code mit einem versteckten Wert für die Länderauswahl.


Select a dropdown box value
Für Dropdown-Felder, Listenfelder oder Optionen zum Auswählen, solange "path" oder "property" gleich "select option key value" sind. s “werden die Optionen automatisch ausgewählt.

Beispiel für eine vollständige Dropdown-Box

Schauen wir uns ein vollständiges Beispiel für eine Spring MVC-Dropdown-Box an:

1. Modell

Eine Kundenmodellklasse zum Speichern des Dropdown-Feldwerts.

Datei: Customer.java

package com.example.customer.model;

public class Customer{

    String country;
    String javaSkills;

    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getJavaSkills() {
        return javaSkills;
    }
    public void setJavaSkills(String javaSkills) {
        this.javaSkills = javaSkills;
    }
}

2. Regler

ASimpleFormController, um den Wert des Formular-Dropdown-Felds zu verarbeiten. Legen Sie die "Spring" der Java-Fertigkeiten als Standardwert für das Dropdown-Feld fest.

Datei: DropDownBoxController.java

package com.example.customer.controller;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.example.customer.model.Customer;

public class DropDownBoxController extends SimpleFormController{

    public DropDownBoxController(){
        setCommandClass(Customer.class);
        setCommandName("customerForm");
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request)
        throws Exception {

        Customer cust = new Customer();

        //make "Spring" as the default java skills selection
        cust.setJavaSkills("Spring");

        return cust;

    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response, Object command, BindException errors)
        throws Exception {

        Customer customer = (Customer)command;
        return new ModelAndView("CustomerSuccess","customer",customer);

    }

    protected Map referenceData(HttpServletRequest request) throws Exception {

        Map referenceData = new HashMap();

        Map country = new LinkedHashMap();
        country.put("US", "United Stated");
        country.put("CHINA", "China");
        country.put("SG", "Singapore");
        country.put("MY", "Malaysia");
        referenceData.put("countryList", country);

        Map javaSkill = new LinkedHashMap();
        javaSkill.put("Hibernate", "Hibernate");
        javaSkill.put("Spring", "Spring");
        javaSkill.put("Apache Wicket", "Apache Wicket");
        javaSkill.put("Struts", "Struts");
        referenceData.put("javaSkillsList", javaSkill);

        return referenceData;
    }
}

3. Validator

Ein einfacher Formularvalidator, um sicherzustellen, dass das Dropdown-Feld "country" und "javaSkills" ausgewählt ist.

Datei: DropDownBoxValidator.java

package com.example.customer.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.example.customer.model.Customer;

public class DropDownBoxValidator implements Validator{

    @Override
    public boolean supports(Class clazz) {
       //just validate the Customer instances
       return Customer.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {

       Customer cust = (Customer)target;

       ValidationUtils.rejectIfEmptyOrWhitespace(errors, "javaSkills", "required.javaSkills");

       if("NONE".equals(cust.getCountry())){
        errors.rejectValue("country", "required.country");
       }
    }
}

Datei: message.properties

required.country = Please select a country!
required.javaSkills = Please select a java Skill!

4. View

Eine JSP-Seite, auf der die Verwendung der Spring-Formular-Tags<form:select />,<form:option /> und<form:options /> angezeigt wird.

Datei: CustomerForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>






    

Spring's form select, option, options example

Country :
Java Skills :

Verwenden Sie JSTL, um den übergebenen Wert anzuzeigen.

Datei: CustomerSuccess.jsp

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



    

Spring's form select, option, options example

Country : ${customer.country}
Java Skills : ${customer.javaSkills}

5. Spring Bean Konfiguration

Verknüpfe alles ~



  

    
        
        

        
        
            
        
    

    
    
        
    

    
        
            /WEB-INF/pages/
        
        
            .jsp
        
    

6. Demo

SpringMVC-DropDownBox-Example-1

Wenn der Benutzer beim Absenden des Formulars keinen Dropdown-Feldwert ausgewählt hat, zeigen Sie die Fehlermeldung an und markieren Sie sie.

SpringMVC-DropDownBox-Example-2

Wenn das Formular erfolgreich gesendet wurde, zeigen Sie einfach die gesendeten Dropdown-Box-Werte an.

SpringMVC-DropDownBox-Example-3

Quellcode herunterladen

Laden Sie es herunter -SpringMVCForm-DropDownBox-Example.zip (10 KB)