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

Java-框架王 SpringMVC 发布时间:2021-07-12 09:13:47 阅读数:12734 1

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

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

例:
定义一个jsp页面

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

 <form action="/com.java265.helloworld/userReg"  method="post">
    userName:<input type="text"   name="userName" /><br />
    email:  <input type="text"    name="emailAddress" /><br />
    password:<input type="password"   name="passWord" /><br />
    hobby:<input type="checkBox" id="hobby1" name="chkHobby" value="1" /><label for="hobby1" >one</label>
          <input type="checkBox" id="hobby2" name="chkHobby" value="2"  /> <label for="hobby2" >two</label>
          <input type="checkBox"  id="hobby3"name="chkHobby" value="3" /><label for="hobby3" >three</label>
         <br/>
    remarks:<textarea rows="3" cols="6" name="txtRemarks"></textarea>
    
    <input type="submit" value="submit" />
    
 </form>
</body>
</html>
二、在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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

java265.com--ShowUserInfo <br />
<hr />
userName: ${requestScope.u.userName}
 <br />
email:    ${requestScope.u.eMail} <br />

<br />
passWord:    ${requestScope.u.passWord} <br />

<br />
hobby:    ${requestScope.u.hobby} 

<br />
remarks:    ${requestScope.u.remarks} 
<br />


</body>
</html>
运行效果如下图所示:
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringMVC/202107/470.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者