发表于: 2017-08-04 21:21:53

1 1056


今日完成:

任务二的bug解决,现在在完善jsp页面,并且postman有中文乱码问题,先解决一下;

今天讲了一下小课堂,然后解决了一个很奇怪的bug

需要把昨天的错误Controller代码更新一下

package com.jnshu.controller;

import com.jnshu.entitiy.Student;
import com.jnshu.service.StudentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
* Created by Administrator on 2017/07/25.
*/
@Controller
public class RestController {

private static Logger log = Logger.getLogger(RestController.class);
   @Autowired
   private StudentService studentService;


   //获取列表
   @RequestMapping(value = "/stus",method = RequestMethod.GET)
public String getAll(Model model){
List<Student> students = studentService.getAll();
       log.info("=======students:" +students);
//        logger.info(studentService.getAll());
       model.addAttribute("students",students);
       return "list";
   }



//插入信息
   //获取插入界面
   @RequestMapping(value = "/addStu",method = RequestMethod.GET)
public String input(Map<String,Object> map){
map.put("student",new Student());

       return "input";
   }
//返回列表查看更新数据,插入中文字符获取不到页面,用网页提交出现中文乱码
   @RequestMapping(value = "/addStu",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
public String insert(Student student){
System.out.println(student);
       studentService.insert(student);
       return "redirect:/stus";
   }


//由ID删除数据,页面405,找不到GET方法
   @RequestMapping(value = "/delStu/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id) {


studentService.delete(id);
       return "redirect:/stus";

   }


//由ID查找数据
   @RequestMapping(value = "/seeStu/{id}",method = RequestMethod.GET)
public String selectById(@PathVariable("id") Integer id,Model model){
Student student = studentService.selectById(id);
       model.addAttribute("student",student);
       return "detail";
   }


//由姓名查找数据,中文查找报错,网页可以,postman不行
   @RequestMapping(value = "/seeStu2/{name}",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
public String selectByName(@PathVariable("name") String name,Model model){
List<Student> students = studentService.selectByName(name);
       model.addAttribute("students",students);

       return "list";
   }


//获取更新列表
   @RequestMapping(value = "/updStu/{id}",method = RequestMethod.GET)
public String in(@PathVariable("id") Integer id,Model model){
Student student = studentService.selectById(id);

       model.addAttribute("student",student);
       return "in";

   }

//更新数据,页面400,更新不添加姓名时报错,表单有姓名信息,网页操作没问题,jsp应该加个判空条件
   // Attribute页面重定向后数据拼接到url后
   @RequestMapping(value = "/updStu/{id}",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
public String update(Student student, @PathVariable("id") Integer id) throws IOException {
studentService.update(student,id);

       return "redirect:/stus";

   }

}


bug的问题是redirect重定向的问题,这里对于重定向的书写,return "redirect:/stus";一定要写/

不然就会出现拼接URL的现象

另外

对应的jsp页面里,form表单的action动作需要和方法的url对应,不然也是会报错

如我的插入表单in.jsp

<%@ page language="java"  pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@page isELIgnored="false" %>
<html>
<head>
   <title>List</title>
</head>

<body>
<h2>学生信息</h2>
<hr>
<%--action对应的内容是所操作的方法的url,如果URL不一致,则操作不成功--%>
<form:form action="/updStu/${student.ID}" method="post" modelAttribute="student">
<%--<input hidden name="_method" value="PUT">--%>
ID:${student.ID}<br/>
<br/>
姓名:${student.SName}<br/>
姓名:<form:input path="SName"/><br/>
QQ:${student.QQ}<br/>
QQ:<form:input path="QQ"/><br/>
类型:${student.style}<br/>
类型:<form:input path="style"/><br/>
时间:${student.time}<br/>
   时间:<form:input path="time"/><br/>
学校:${student.school}<br/>
   学校:<form:input path="school"/><br/>
学号:${student.SNumber}<br/>
   学号:<form:input path="SNumber"/><br/>
链接:${student.link}<br/>
   链接:<form:input path="link"/><br/>
宣言:${student.dream}<br/>
   宣言:<form:input path="dream"/><br/>
辅导师兄:${student.FBro}<br/>
   辅导师兄:<form:input path="FBro"/><br/>
引荐师兄:${student.YBro}<br/>
   引荐师兄:<form:input path="YBro"/><br/>
何处知晓:${student.whereKnow}<br/>
   何处知晓:<form:input path="whereKnow"/><br/>
创建时间:${student.create_at}<br/>
更新时间:${student.update_at}<br/>
   更新时间:<form:input path="update_at"/><br/>
   <input type="submit" value="提交">

</form:form>
<hr>
</body>

</html>


action如果url不对应是会报错的,今天暂时将这个bug解决了,然后用postman和网页分别测试了一下接口,全部接口是可用的,但是又引出一个bug和一个知识点。


一个bug,网页上提交中文没有问题,但是postman参数提交中文就会报错,插入数据库中的也是乱码,通过日志打印,是postman传入Controller方法时,就已经出现乱码,这个bug待解决。


一个知识点,我想在list这个列表页面上为每条记录都添加一个删除按钮,点击删除后可以实现调用Controller中的DELETE方法,好像需要在jsp页面中通过ajax实现,但是不会,正在学习。


明天计划:先看情况,如果中午之前没有实现删除按钮功能和postman乱码的解决,就先把任务二提交,开始准备任务三。


问题:ajax实现删除按钮;postman中文乱码问题。


收获:对于重定向的理解;对于jsp页面的表单又有新的理解。


返回列表 返回列表
评论

    分享到