English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Pulsante di selezione singola del modulo MVC di Spring

Il pulsante di selezione singola del modulo MVC di Spring ci permette di selezionare una singola opzione. Questa etichetta visualizzerà un etichetta di input HTML di tipo radio.

Sintassi

<form:radiobutton path="abc" value="xyz"/>

Oltre alle etichette di pulsanti di selezione singola, la libreria di etichette dei moduli MVC di Spring include radiobuttons Etichetta. Questa etichetta visualizzerà più etichette di input HTML di tipo radio.

<form:radiobuttons path="abc" items="${xyz}"/>

Esempio di pulsante di selezione singola del modulo MVC di Spring

1、Aggiungi la dipendenza al file pom.xml

          <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.1.RELEASE</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>servlet-api</artifactId>  
    <version>3.0-alpha-1</version>  
</dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.12</version>
</dependency>

2、创建bean类

Reservation.java

package com.w3codebox;
public class Reservation {
    private String firstName;
    private String lastName;
    private String Gender;
    
    public Reservation()
    {       
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getGender() {
        return Gender;
    }
    public void setGender(String gender) {
        Gender = gender;
    }   
}

3、创建控制器类

ReservationController.java

package com.w3codebox;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/reservation")
@Controller
public class ReservationController {
    @RequestMapping("/bookingForm")
public String bookingForm(Model model)
{
      //creare un oggetto prenotazione 
    Reservation res = new Reservation();
      //fornire l'oggetto prenotazione al modello 
    model.addAttribute("reservation", res);
    return "reservation-page";
}
@RequestMapping("/submitForm")
public String submitForm(@ModelAttribute("reservation") Reservation res)
{
    return "confirmation-form";
}
}

4、nella file web.xml fornire l'entry del controller

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
   <servlet>  
    <servlet-name>spring</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>    
</servlet>  
<servlet-mapping>  
    <servlet-name>spring</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  
</web-app>

5、在xml文件中定义Bean

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- Fornire supporto per la scansione dei componenti -->
    <context:component-scan base-package="com.w3codebox" />
    <!-- Fornire supporto per la conversione, la formattazione e la convalida -->
    <mvc:annotation-driven/>
    <!-- Definire il risolutore di vista Spring MVC -->
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>     
     </bean>
</beans>

6、Creare la pagina della richiesta

index.jsp

<!DOCTYPE html>
<html>
<head>
    <title>Modulo di registrazione ferroviaria</title>
</head>
<body>
<a href="reservation/bookingForm">Clicca qui per la prenotazione.</a>
</body>
</html>

7、Creare componenti di vista

reservation-page.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
    <title>Modulo di prenotazione</title>
</head>
<h3>Modulo di prenotazione ferroviaria</h3>
<body>
    <form:form action="submitForm" modelAttribute="reservation">
        Nome: <form:input path="firstName" />      
        <br><br>
        Cognome: <form:input path="lastName" />
        <br><br>
        Genere: 
        Uomo <form:radiobutton path="Gender" value="Male"/>
        Femmina <form:radiobutton path="Gender" value="Female"/>
        <br><br>
        <input type="submit" value="Invia" />
    </form:form>
</body>
</html>

confirmation-page.jsp

<!DOCTYPE html>
<html>
<body>
<p>La tua prenotazione è stata confermata con successo. Per favore, rivedi i dettagli.</p>
Nome: ${reservation.firstName} <br>
Cognome: ${reservation.lastName} <br>
Genere: ${reservation.gender}
</body>
</html>

Output: