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

SpringMVC获取表单数据 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; }@Overridepublic 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 = "https://www.it610.com/f")public class FormController {@RequestMapping(value="https://www.it610.com/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="https://www.it610.com/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 getWebList() {List webList = new ArrayList(); webList.add("SpringMVC"); webList.add("SpringBoot"); webList.add("SpringCloud"); return webList; }}

3、页面
测试form标签 - 锐客网用户信息编辑页面
姓名:
性别:
生日:
爱好:

【SpringMVC如何获取表单数据(radio和checkbox)】SpringMVC如何获取表单数据(radio和checkbox)
文章图片

Title - 锐客网您提交的用户信息
Username${username}
Birthday${birthday}
Sex${sex}
favorite

SpringMVC如何获取表单数据(radio和checkbox)
文章图片

SpringMVC获取表单参数 以下几种方式只有在已搭好的SpringMVC环境中,才能执行成功!
一、首先,写一个登陆页面和一个Bean类
登陆页面
yyx博客后台登录 - 锐客网
用户名:
密码:

java实体类
package com.yyx.model; public class User{private String userName; private String userPwd; public String getUserName() {return userName; } public void setUserName(String userName) {this.userName = userName; } public String getUserPwd() {return userPwd; } public void setUserPwd(String userPwd) {this.userPwd = userPwd; }}

二、获取表单数据的方法
1、直接把表单的参数写在Controller相应的方法的形参中
注意:控制器中方法的形参String userName,String userPwd名称必须和登陆页面的name="userName",name="userPwd"相同
package com.yyx.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller@RequestMapping("/user")public class UserController {@RequestMapping("/login")public String loginByUser(String userName, String userPwd) {System.out.println("userName is:" + userName); System.out.println("userPwd is:" + userPwd); return "success"; }}

2.通过HttpServletRequest接收
控制器中
String userName=request.getParameter("userName"); String userPwd=request.getParameter("userPwd");

的参数名称必须和登陆页面的name="userName",name="userPwd"相同
package com.yyx.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller@RequestMapping("/user")public class UserController {@RequestMapping("/login")public String loginByUser(HttpServletRequest request) {String userName=request.getParameter("userName"); String userPwd=request.getParameter("userPwd"); System.out.println("userName is:" + userName); System.out.println("userPwd is:" + userPwd); return "success"; }}

3.通过一个bean来接收
登陆页面的name="userName",name="userPwd"必须和Bean类的属性名称相同
package com.yyx.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.yyx.model.User; @Controller@RequestMapping("/user")public class UserController {@RequestMapping("/login")public String loginByUser(User user) {System.out.println("userName is:" + user.getUserName()); System.out.println("userPwd is:" + user.getUserPwd()); return "success"; }}

4.利用js中ajax请求通过json数据接收
在页面中添加js代码

控制器中的代码和方法3中的相同。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读