mvc html单选,Spring MVC表单单选按钮用法示例

Spring MVC表单单选按钮使我们一次只能选择一个选项。该标签呈现单选类型的HTML输入标签。

句法

除了单选按钮标签之外, Spring MVC表单标签库还包含单选按钮标签。该标签呈现带有radio类型的多个HTML输入标签。

Spring MVC表单单选按钮的示例

1.将依赖项添加到pom.xml

org.springframework

spring-webmvc

5.1.1.RELEASE

javax.servlet

servlet-api

3.0-alpha-1

javax.servlet

jstl

1.2

org.apache.tomcat

tomcat-jasper

9.0.12

2.创建bean类

Reservation.java

package com.srcmini;

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.srcmini;

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)

{

//create a reservation object

Reservation res=new Reservation();

//provide reservation object to the model

model.addAttribute("reservation", res);

return "reservation-page";

}

@RequestMapping("/submitForm")

public String submitForm(@ModelAttribute("reservation") Reservation res)

{

return "confirmation-form";

}

}

4.在web.xml文件中提供控制器的条目

web.xml

SpringMVC

spring

org.springframework.web.servlet.DispatcherServlet

1

spring

/

5.在xml文件中定义bean

spring-servlet.xml

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">

6.创建请求的页面

index.jsp

Railway Registration Form

Click here for reservation.

7.创建视图组件

Reservation-page.jsp

Reservation Form

Railway Reservation Form

First name:

Last name:

Gender:

Male

Female

确认页面.jsp

Your reservation is confirmed successfully. Please, re-check the details.

First Name : ${reservation.firstName}

Last Name : ${reservation.lastName}

Gender: ${reservation.gender}

输出

spring-mvc-form-radio-button-output1.png

spring-mvc-form-radio-button-output2.png

spring-mvc-form-radio-button-output3.png

spring-mvc-form-radio-button-output4.png

下载此示例(使用Eclipse开发)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值