SpringMVC如何获取单选框的值呢?
单选框是web开发中常用的一种html控件,那么SpringMVC中如何获取其传送至后台的值呢?下文将一一道来,如下所示:
实现思路:
1.创建一个动态web工程
2.建立一个实体用于存储数据,建立相应的Controller方法
3.建立两个jsp页面,一个用于提交表单,一个用于展示数据,如下所示: user.java
实现思路:
1.创建一个动态web工程
2.建立一个实体用于存储数据,建立相应的Controller方法
3.建立两个jsp页面,一个用于提交表单,一个用于展示数据,如下所示: user.java
package com.java265.helloworld.Entity; public class User { private String userName; private String eMail; private String passWord; private String remarks; private String hobby; // 爱好 private String gender;// 性别 public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } 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 User(String userName, String eMail, String passWord, String remarks, String hobby, String gender) { super(); this.userName = userName; this.eMail = eMail; this.passWord = passWord; this.remarks = remarks; this.hobby = hobby; this.gender = gender; } 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 Post方法用于接收参数值
package com.java265.helloworld.Controller; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; 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 { @Autowired private HttpServletRequest request; /** * 用户返回用户注册 * */ @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, String gender, Model model) { User u = new User(userName, emailAddress, passWord, txtRemarks, chkHobby, gender); model.addAttribute("u", u); return "showUserInfo"; } }建立注册页面及数据展示页面
<%@ 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 /> Gender:<input type="radio" name="gender" id ="gender1" value="-1" ><label for="gender1" >male</label> <input type="radio" name="gender" id ="gender2" value="-1" ><label for="gender2" >female</label><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> <%@ 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 /> Gender: ${requestScope.u.gender} <br /> remarks: ${requestScope.u.remarks} <br /> </body> </html>运行效果如下图所示:
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。