美文网首页
spring boot复选框

spring boot复选框

作者: 棵_8adb | 来源:发表于2017-11-07 15:38 被阅读0次

极客工作室个人项目日更表

项目/任务名称

  • spring boot复选框

仓库地址(选填)

业务需求/问题
确认用户的需求的复选框

解决方法
通过网络查找相应的教程

完成进度

  • 创建一个名称为 Checkbox 的动态WEB项目
  • 在 com.yiibai.springmvc 包下创建两个Java类User,UserController
  • 在jsp子文件夹下创建两个视图文件:user.jsp,userlist.jsp
  • 最后一步是创建所有源和配置文件的内容并运行应用程序

User.java

package com.yiibai.springmvc;

public class User {

   private String username;
   private String password;
   private String address;
   private boolean receivePaper;    

   public String getUsername() {
      return username;
   }
   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return password;
   }
   public void setPassword(String password) {
      this.password = password;
   }
   public String getAddress() {
      return address;
   }
   public void setAddress(String address) {
      this.address = address;
   }
   public boolean isReceivePaper() {
      return receivePaper;
   }
   public void setReceivePaper(boolean receivePaper) {
      this.receivePaper = receivePaper;
   }
}

UserController.java

package com.yiibai.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;

@Controller
public class UserController {

   @RequestMapping(value = "/user", method = RequestMethod.GET)
   public ModelAndView user() {
      return new ModelAndView("user", "command", new User());
   }

   @RequestMapping(value = "/addUser", method = RequestMethod.POST)
   public String addUser(@ModelAttribute("SpringWeb")User user, 
      ModelMap model) {
      model.addAttribute("username", user.getUsername());
      model.addAttribute("password", user.getPassword());
      model.addAttribute("address", user.getAddress());
      model.addAttribute("receivePaper", user.isReceivePaper());
      return "userlist";
   }
}

user.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
   <title>Spring MVC表单处理(复选框)</title>
</head>
<body>

<h2>用户信息 - </h2>
<form:form method="POST" action="/Checkbox/addUser">
   <table>
      <tr>
         <td><form:label path="username">用户名:</form:label></td>
         <td><form:input path="username" /></td>
      </tr>
      <tr>
         <td><form:label path="password">密码:</form:label></td>
         <td><form:password path="password" /></td>
      </tr>  
      <tr>
         <td><form:label path="address">地址:</form:label></td>
         <td><form:textarea path="address" rows="5" cols="30" /></td>
      </tr>  
      <tr>
         <td><form:label path="receivePaper">订阅新闻?</form:label></td>
         <td><form:checkbox path="receivePaper" /></td>
      </tr> 
      <tr>
         <td colspan="2">
            <input type="submit" value="提交"/>
         </td>
      </tr>
   </table>  
</form:form>
</body>
</html>

userlist.jsp

<%@ page contentType="text/html; charset=UTF-8"%>

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC表单处理(复选框)</title>
</head>
<body>

<h2>提交的用户信息</h2>
   <table>
      <tr>
         <td>用户名:</td>
         <td>${username}</td>
      </tr>
      <tr>
         <td>密码:</td>
         <td>${password}</td>
      </tr>    
      <tr>
         <td>地址:</td>
         <td>${address}</td>
      </tr>  
      <tr>
         <td>是否订阅新闻</td>
         <td>${receivePaper}</td>
      </tr>          
   </table>  
</body>
</html>

经验分享(选填)

其他问题
注意细节与自己项目结合

编写日期
2017年11月3日

相关文章

网友评论

      本文标题:spring boot复选框

      本文链接:https://www.haomeiwen.com/subject/unakmxtx.html