SpringMVC获取表单数据(radio和checkbox)

1、实体类

package cn.hadron.bean;

import java.io.Serializable;
import java.util.Arrays;

/**
 * create table users(
     id int auto_increment primary key,
     username varchar(45),
     password varchar(45),
     age int default 0
   );
   insert into users(username,password,age) values('hadron','123',18);
 * @author chengyq
 *
 */

// 域对象,实现序列化接口
public class UserBean implements Serializable {

    private Integer id;
    private String username;
    private String password;
    private String birthday;
    private Integer age;
    //测试单选按钮
    private String sex;
    //测试复选按钮
    private String[] favorite;

    public UserBean() {}
    public UserBean(String username, String password,int age) {
        this.username = username;
        this.password = password;
        this.age=age;
    }
    public UserBean(String username, String birthday,String sex) {
        this.username = username;
        this.birthday = birthday;
        this.sex=sex;
    }
    public Integer getId() {
        return id;
    }
    public String getUsername() {
        return username;
    }
    public String getPassword() {
        return password;
    }
    public Integer getAge() {
        return age;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    public String[] getFavorite() {
        return favorite;
    }

    public void setFavorite(String[] favorite) {
        this.favorite = favorite;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "UserBean{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday='" + birthday + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", favorite=" + Arrays.toString(favorite) +
                '}';
    }
}

2、控制器

package cn.hadron.controller;

import cn.hadron.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/f")
public class FormController {

    @RequestMapping(value="/getEditPage",method= RequestMethod.GET)
    public String getEditPage(Model model) {
        System.out.println("表单测试");
        UserBean user = new UserBean("jack","1997-7-1","女");
        // model中添加属性user,值是user对象
        model.addAttribute("user",user);
        //返回Edit.jsp页面
        return "edit";
    }

    @RequestMapping(value="/edit",method=RequestMethod.POST)
    public String edit(@ModelAttribute UserBean user,Model model) {
        System.out.println("获取表单数据:");
        //@ModelAttribute注解指示了参数应该从模型(这里所说的“模型”指 Model)中获取
        model.addAttribute("username", user.getUsername());
        model.addAttribute("birthday", user.getBirthday());
        model.addAttribute("sex", user.getSex());
        model.addAttribute("favorite", user.getFavorite());
        System.out.println("user="+user);
        //返回userPage.jsp页面
        return "userPage";
    }

    /**
     * 可以用@ModelAttribute注解的方法做一些初始化操作。
     * 当同一个controller中有多个方法被@ModelAttribute注解标记,
     * 所有被@ModelAttribute标记的方法均会被执行,按先后顺序执行,然后再进入请求的方法
     * @return
     */
    @ModelAttribute("webList")
    public List<String> getWebList() {
        List<String> webList = new ArrayList<String>();
        webList.add("SpringMVC");
        webList.add("SpringBoot");
        webList.add("SpringCloud");
        return webList;
    }
}

3、页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试form标签</title>
</head>
<body>
<h3>用户信息编辑页面</h3>
<form:form modelAttribute="user" method="post" action="/elastic/f/edit.do" >
    <table>
        <tr>
            <td>姓名:</td>
            <td><form:input path="username"/></td>
        </tr>
        <tr>
            <td>性别:</td>
            <td>
                <form:radiobutton path="sex" value="男" label="男" />
                <form:radiobutton path="sex" value="女" label="女" />
            </td>
        </tr>
        <tr>
            <td>生日:</td>
            <td><form:input path="birthday"/></td>
        </tr>
        <tr>
            <td>爱好:</td>
            <td><form:checkboxes items="${webList}" path="favorite" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交变更"/>
            </td>
        </tr>
    </table>
</form:form>
</body>
</html>

这里写图片描述

<%--
  Created by IntelliJ IDEA.
  User: chengyq
  Date: 2018/9/3
  Time: 14:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>您提交的用户信息</h2>
<table>
    <tr>
        <td>Username</td>
        <td>${username}</td>
    </tr>
    <tr>
        <td>Birthday</td>
        <td>${birthday}</td>
    </tr>
    <tr>
        <td>Sex</td>
        <td>${sex}</td>
    </tr>
    <tr>
        <td>favorite</td>
        <td>
            <%
                String[] favorite = (String[])request.getAttribute("favorite");
                for(String f: favorite) {
                    out.println(f);
                }
            %>
        </td>
    </tr>
</table>
</body>
</html>

这里写图片描述

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring MVC中,你可以使用@RequestParam注解来获取表单数据。同时,如果你使用Thymeleaf作为模板引擎,你可以在表单中使用Thymeleaf的语法来绑定表单数据。 首先,确保你已经在你的Spring MVC配置中配置了Thymeleaf视图解析器。例如,在你的`application.properties`或`application.yml`中添加以下配置: ``` spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 接下来,在你的Controller中,使用@RequestParam注解来获取表单数据。例如,假设你有一个表单提交了一个名为"name"的字段,你可以这样获取它的值: ```java @PostMapping("/submit") public String handleSubmit(@RequestParam("name") String name) { // 处理表单数据 return "redirect:/success"; } ``` 在上面的例子中,`@RequestParam("name")`表示获取名为"name"的表单字段的值,并将其赋值给`name`变量。 然后,在你的Thymeleaf模板中,你可以使用Thymeleaf的语法来绑定表单数据。例如,如果你想在表单中显示之前提交的名字,可以使用以下代码: ```html <form th:action="@{/submit}" method="post"> <input type="text" name="name" th:value="${name}" /> <button type="submit">Submit</button> </form> ``` 在上面的例子中,`${name}`表示从Controller传递过来的名为"name"的属性值。 这样,你就可以在Spring MVC获取和使用Thymeleaf表单数据了。希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值