发表于: 2017-08-21 19:02:28

3 1128


今天完成的事情:

POSTMAN,网页调试和发送HTTP请求的工具

可以把进行调试的url直接写到postman的地址栏里,然后前边可以设置GET、POST方法

左边竖栏是工具栏,目前只知道可以保存请求

点url栏右边的save

选择保存的名称,描述,文件夹,就可以了,我先把CRUD保存起来

利用controller和rest风格接口来完成增删改查

首先建好目录

把任务一完成的对应的实体类,dao层,service层,service实现类impl都导进去,嗯..

beans.xml里边就配置了数据源,数据库连接池,mybatis会话工厂,sql语句和dao的映射关系类

sql语句还是写在xml里的

去controller里写一个简单的查询试试

@Controller
//@RequestMapping("/select")
public class StudentController {
@Autowired
   private StudentService studentService;
   @RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
public String in(@PathVariable("id") int id, Model model) {
Student student = studentService.selectById(id);

       System.out.println(student);

       model.addAttribute("student", student);
       return "select";
   }

jetty运行一下

503...

报了一大堆错误,直接蒙圈了(*@ο@*)

听师兄的一点点测试排查

dao层

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:beans.xml"})
public class test {
@Autowired
   private StudentDao studentDao;
   @Autowired
   ApplicationContext ctx;


@Test

   public void testSelect1(){
StudentDao studentDao= (StudentDao) ctx.getBean("studentDao");
       Student student =studentDao.selectById(2);
       System.out.println(student);
   }
}

一开始就有错误,排查之后发现是,少了一个jdbc的jar包

这个jar 文件包含对Spring 对JDBC 数据访问进行封装的所有类。

外部依赖spring-beans,spring-dao。

然后测service

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:beans.xml"})
public class ServiceTest {

@Autowired
   private StudentService studentService;
   @Autowired
   ApplicationContext ctx;
   @Test
   public void test1(){
Student student=studentService.selectById(3);
       System.out.println(student);
   }
}

嗯...又报错,无法创建bean

这个问题后来发现是测试写错了,那个自动注入忘了写了..

然后测试服务器

继续报错

这次是配置文件之间的关联关系没弄好,

beans.xml导入applicationContext-service.xml,applicationContext-service.xml导入Dispatcher-servlet.xml,web.xml再将拦截的请求交给Dispatcher-servlet.xml处理,主要是views

运行一下

成功出现了index.jsp的页面

用postman测一下

get请求的 /select/3      就是查询3号

结果

没问题,这个jsp我就瞎写的,所以丑了点

写了一下input

@Controller
@RequestMapping("/student")
public class Wawawa {
@Autowired
   private StudentService studentService;
   private final static Map<String, Student> students = new HashMap<String, Student>();

   @RequestMapping(value = "/add", method = RequestMethod.GET)
public String addStudent(Model model) {
model.addAttribute(new Student());
       return "add";
   }

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addStudent(Student student) {
students.put(student.getName(), student);
       return "redirect:/list";
   }

嗯,贴一下jsp页面吧

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form method="post" modelAttribute="student">


name: <form:input path="name"/><br/>
   qq: <form:input path="qq"/><br/>
   type: <form:input path="type"/><br/>
   time: <form:input path="time"/><br/>
   school: <form:input path="school"/><br/>
   numble: <form:input path="number"/><br/>
   link: <form:input path="link"/><br/>
   hope: <form:input path="hope"/><br/>
   referrer: <form:input path="referrer"/><br/>
   from_where: <form:input path="from_where"/><br/>
   create_at: <form:input path="create_at"/><br/>
   update_at: <form:input path="update_at"/><br/>

   <input type="submit" value="添加新用户"/>
</form:form>

然后运行

哇,成功了!

(PS:其实这个我做了3个小时,一直报错,然后发现全都是写错了)

然后添加信息

点一下

HTTP ERROR 400

400错误就是发起的请求不符合对请求的一些限制。。不对呀,明明格式应该都是对的

debug中...

现在发现是list的jsp写错了

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://www.springframework.org/tags/form" %>
<c:forEach items="${students}" var="student">
${student.value.name}----${student.value.qq}----${student.value.type}----${student.value.time}----${student.value.school}----${student.value.numble}----${student.value.link}----${student.value.hope}----${student.value.referrer}----${student.value.from_where}----${student.value.id}----${student.value.create_at}----${student.value.update_at}
   <a href="<%=request.getContextPath()%>/user/${user.value.username}">查看</a>
   <a href="<%=request.getContextPath()%>/user/${user.value.username}/update">编辑</a>
   <a href="<%=request.getContextPath()%>/user/${user.value.username}/delete">删除</a>
   <br/>
</c:forEach>
<br/>
<a href="<%=request.getContextPath()%>/user/add">继续添加用户</a>

好像forEach用不了,为什么呢

明天计划的事情:

写完CRUD,大概是要换个教程了

遇到的问题:

input操作的增加之后的页面显示不了,也就是POST还是没实现

收获:

基本的spring+springMVC+mybatis操作,可以用springmvc操作数据库了,然后jsp我觉得要学一下

禅道链接:http://task.ptteng.com/zentao/project-task-286.html (我点不了完成任务或者开始任务)


返回列表 返回列表
评论

    分享到