发表于: 2018-06-09 23:21:58

2 963


一、今日完成


重写一个Controller,利用表单提交信息

@Controller
@RequestMapping("/rest")
public class StuController {
   @Autowired
   private UserServiceImpl userService;
   //查
   @RequestMapping(value = "user", method = RequestMethod.GET)
   public String userList(User user, Model model) throws Exception {
       List<User> user1 = userService.getAllUser();
       model.addAttribute("itemsList", user1);
       return "CRUD/home";
   }
   //增
   @RequestMapping(value = "a", method = RequestMethod.PUT, produces = "application/test;charset=utf-8")
   public String addUser(User user) throws Exception {
       System.out.println(user);
       userService.saveUser(user);
       return "redirect:/rest/user/";
   }
   //删
   @ResponseBody
   @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
   public int delete(@PathVariable int id) throws Exception {
       System.out.println(id);
       return userService.deleteUser(id);
   }


home.jsp


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page isErrorPage="true" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
   <title>查询个人信息</title>
</head>

<style>
   table, table td, table th {
       border: 1px solid;
       border-collapse: collapse;
       text-align: center;
   }
</style>
<%--sprict--%>
<script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.min.js"></script>
<script type="text/javascript">
   function sendBth(id) {
       var url = "${pageContext.request.contextPath}/rest/" + id;
       $.ajax({
           url: url,
           type: 'DELETE',
           success: function (result) {
               if (result) {
                   alert("id:" + id + "删除成功,即将返回页面")
                   window.location.reload();
               } else {
                   alert("id: " + id + "删除失败")
               }
           }
       });
   }
</script>
<body>

<form action="${pageContext.request.contextPath}/rest/a" method="POST">
   <input type="hidden" name="_method" value="PUT"/>
   <fieldset>
       <legend>新增学生</legend>
       <table width="100%" border=1>
           <tr>
               <td>名字</td>
               <td>QQ</td>
               <td>修真类型</td>
               <td>预计入学时间</td>
               <td>毕业学校</td>
               <td>学院学号</td>
               <td>日报连接</td>
               <td>愿望</td>
               <td>线上师兄</td>
               <td>操作</td>
           </tr>
           <tr>
               <td><input type="text" style="width: 80px;" name="name"></td>
               <td><input type="text" style="width: 100px;" name="qq"></td>
               <td><input type="text" style="width: 100px;" name="learning_type"></td>
               <td><input type="text" name="entrance_time"></td>
               <td><input type="text" style="width: 120px;" name="school"></td>
               <td><input type="text" style="width: 65px;" name="online_id"></td>
               <td><input type="text" name="daily_link"></td>
               <td><input type="text" name="wish"></td>
               <td><input type="text" style="width: 65px;" name="bre"></td>
               <td>
                   <input type="submit" value="提交">
               </td>
           </tr>
       </table>
   </fieldset>
</form>
<fieldset>
   <legend>学生信息</legend>
   <table width="100%" border=1>
       <tr>
           <td>id</td>
           <td>名字</td>
           <td>QQ</td>
           <td>修真类型</td>
           <td>预计入学时间</td>
           <td>毕业学校</td>
           <td>学院学号</td>
           <td>日报连接</td>
           <td>愿望</td>
           <td>线上师兄</td>
           <td>操作</td>
       </tr>
       <c:forEach items="${itemsList}" var="items">
           <tr>
               <td>${items.id}</td>
               <td>${items.name}</td>
               <td>${items.qq}</td>
               <td>${items.learning_type}</td>
               <td><${items.entrance_time}></td>
               <td>${items.school}</td>
               <td>${items.online_id}</td>
               <td>${items.daily_link}</td>
               <td>${items.wish}</td>
               <td>${items.tutor}</td>
               <td><a href="${pageContext.request.contextPath}/rest/${items.id}">修改</a>
                   <a href="" onclick="sendBth(${items.id})">删除</a>
               </td>
           </tr>
       </c:forEach>
   </table>
</fieldset>
</body>
</html>


显示页面信息



尝试新增数据



数据能添加,但是中文乱码


之前利用ajax序列化提交表单时也出现乱码,尝试很多方法没有解决


删除功能


删除成功



二、明日计划

完善全部功能,找出乱码原因,解决乱码

三、收获与问题

乱码的问题,尝试了很多方法,依旧没有很好的解决


返回列表 返回列表
评论

    分享到