• SpringMVC如何获取复选框中的值呢?


    转自:

    SpringMVC如何获取复选框中的值呢?

    复选框在Web开发中常用于用户批量勾选某一类值的操作,
       如:用户的喜好
    那么SpringMVC如何获取用户复选框中的选中值呢?下文将通过示例的方式,讲述其具体的实现方式,如下所示:

    实现思路:
         在SpringMVC中Controller中获取参数信息,根据html控件的name进行匹配,所以我们可采用获取其它文本控件的方式获取其值

    例:
    定义一个jsp页面

     

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    
    
    
    
    Insert title here
    
    
    
     
        userName:
        email:  
        password:
        hobby:                              
        remarks:                

    二、在Entity中加入相应的字段存储相应值

     package com.java265.helloworld.Entity;
    
    public class User {
    
    	private String userName;
    	private String eMail;
    	private String passWord;
    	private String remarks;
    	private String hobby; // 爱好
    
    	public String getHobby() {
    		return hobby;
    	}
    
    	public void setHobby(String hobby) {
    		this.hobby = hobby;
    	}
    
    	public User(String userName, String eMail, String passWord, String remarks, String hobby) {
    		super();
    		this.userName = userName;
    		this.eMail = eMail;
    		this.passWord = passWord;
    		this.remarks = remarks;
    		this.hobby = hobby;
    	}
    
    	public String getRemarks() {
    		return remarks;
    	}
    
    	public void setRemarks(String remarks) {
    		this.remarks = remarks;
    	}
    
    	public User(String userName, String eMail, String passWord, String remarks) {
    		super();
    		this.userName = userName;
    		this.eMail = eMail;
    		this.passWord = passWord;
    		this.remarks = remarks;
    	}
    
    	public User() {
    		super();
    	}
    
    	public User(String userName, String eMail) {
    		super();
    		this.userName = userName;
    		this.eMail = eMail;
    	}
    
    	public User(String userName, String eMail, String passWord) {
    		super();
    		this.userName = userName;
    		this.eMail = eMail;
    		this.passWord = passWord;
    	}
    
    	public String getUserName() {
    		return userName;
    	}
    
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    
    	public String geteMail() {
    		return eMail;
    	}
    
    	public void seteMail(String eMail) {
    		this.eMail = eMail;
    	}
    
    	public String getPassWord() {
    		return passWord;
    	}
    
    	public void setPassWord(String passWord) {
    		this.passWord = passWord;
    	}
    
    }
    
    

    三、Controller中的方法获取相应的值

    package com.java265.helloworld.Controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import com.java265.helloworld.Entity.User;
    
    @Controller
    public class UserController {
    
    	/**
    	 * 用户返回用户注册
    	 *
    	 */
    	@RequestMapping(value = "/userReg", method = RequestMethod.GET)
    	public String userReg() {
    
    		return "userReg";
    	}
    
    	/**
    	 * 
    	 * 使用Spring MVC获取文本变量
    	 * 
    	 * @param userName      :此出的名字缺省情况下只需同表单中的name对应即可获取数值
    	 * @param emailAddress: 同上
    	 * 
    	 * @return
    	 */
    	@RequestMapping(value = "/userReg", method = RequestMethod.POST)
    	public String userReg(String userName, String emailAddress, String passWord, String txtRemarks, String chkHobby,
    			Model model) {
    
    		User u = new User(userName, emailAddress, passWord, txtRemarks, chkHobby);
    		model.addAttribute("u", u);
    
    		return "showUserInfo";
    
    	}
    
    }
    
    
    

    四、页面数据显示进行相应的处理

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    
    
    
    
    Insert title here
    
    
    
    java265.com--ShowUserInfo 

    userName: ${requestScope.u.userName}  
    email:    ${requestScope.u.eMail}

    passWord:    ${requestScope.u.passWord}

    hobby:    ${requestScope.u.hobby} 
    remarks:    ${requestScope.u.remarks} 

    运行效果如下图所示:

  • 相关阅读:
    IP地址相关
    文献阅读01_基于深度学习的个性化新闻推荐方法研究_20221114
    Swift 高阶函数详解(forEach、filter、map、flatMap、compactMap、reduce、sorted)
    Vue3实战教程(快速入门)
    Druid使用详解
    C#简单晶圆wafermapping显示示范demo
    论文解读:PF磷酸:基于机器学习的磷酸化位点预测疟原虫蛋白的工具
    Part4_场景_第52章 场景描述&第53章 柏林I:BVG场景&第54章 柏林II:CEMDAP-Matsim-Cadyts方案
    bloaty
    树莓派——5、Ubuntu18-04虚拟机搭建VMware版本
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/127830422