发表于: 2019-11-20 23:06:16
1 971
今天完成得事情:
1.学习了mvc
关于mvc得流程:
3.视图解析器
2. 学习了springmvc框架注解@requestmapping
可以放在类上 也可以放在方法上
value ,等同于path
params 指定传入参数
//1. path(value) 设置方法名,类中 方法均可
//方便处理映射器(handlerMapping)调用 返回给前端控制器(DispatcherServlet)
//
//2.params 指定前端传入参数必须有此项内容
//
//3.method 指定请求方式(post put get delete)
举例:
这些概念 我根据昨天写的代码
算是搞清楚了大概的流程
今天根据这些已经学习到的概念 自己写了一个增删查改的controller层
不过没有配置业务 所以只是先尝试
文件结构,
Conreoller层
//控制器类 把controllers 注册到到ioc容器里
@Controller
@RequestMapping(path = "/CrudStudent")
public class Controllers {
//@RequestMapping 注解得作用 建立请求url和处理请求方法之间的对应关系
//1. path(value) 设置方法名,类中 方法均可
//方便处理映射器(handlerMapping)调用 返回给前端控制器(DispatcherServlet)
//
//2.params 指定前端传入参数必须有此项内容
//
//3.method 指定请求方式(post put get delete)
@Autowired
StudentDao studentDao;
//根据学生id查询
@RequestMapping(path ="/findId",params = {"id"},method = {RequestMethod.GET})
public String searchById(int id){
studentDao.searchById(id);
return "findid";
}
//根据学生姓名查询
@RequestMapping(path ="/findName",params = {"name"},method = {RequestMethod.GET})
public String searchByName(String name){
studentDao.searchByName(name);
return "findname";
}
//查询所有学生
@RequestMapping(path = "/findAll",method = {RequestMethod.GET})
public String searchAll(){
studentDao.searchAll();
return "findall";
}
//根据学生id,更改学生职业
@RequestMapping(path = "/updateById",method = {RequestMethod.PUT})
public String updateById(String type,int id){
studentDao.updateById(type,id);
return "updatebyid";
}
//根据学生id删除
@RequestMapping(path = "/deleteById",params = {"id"},method = {RequestMethod.DELETE})
public String deleteById(int id){
studentDao.deleteById(id);
return "deletebyid";
}
//添加一个学生
@RequestMapping(path = "/insertStudent",method = {RequestMethod.POST})
public String insertStudent(Student student){
studentDao.insertStudent(student);
return "insertstudent";
}
}
springmvc文件
<!-- 开启注解扫描 这里扫描的是我的控制层包 用来扫描@Controller注解 -->
<context:component-scan base-package="mycontroller"/>
<context:component-scan base-package="dao"/>
<!-- 创建一个视图解析器(InternalResourceViewResolver)对象 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 开启SpringMVC框架注解的支持 用来扫描@requestmapping注解 -->
<!-- 添加此配置 可以自动加载处理映射器(requestMappinghandleMapping) 和处理适配器(requestMappinghandleMapping)-->
<mvc:annotation-driven />
</beans>
index.jsp 文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>增删查改跳转主页面</title>
</head>
<body>
<h3> 请选择CRUD选项 </h3>
<a href ="CrudStudent/findId">按照id查询学生 </a><br>
<a href ="CrudStudent/findName">按照name查询学生 </a><br>
<a href ="CrudStudent/findAll">查询所有学生 </a><br>
<a href ="CrudStudent/updateById">根据学生id修改学生name </a><br>
<a href ="CrudStudent/deleteById">按照id删除学生 </a><br>
<a href ="CrudStudent/insertStudent">添加一个学生 </a><br>
</body>
</html>
这些对应的jsp文件 不过里面得内容还么写 随便编了两句话
这样
运行
点击链接 每个连接都报错500
错误显示无法创建bean controllers 和 studentdao
可我明明加注解了
也添加了注解扫描
不懂,难道是spring-mvc扫描包方式不一样?
明天再尝试解决这个问题
3.任务要求看用 spring rest 大概看了下 还有一些rest ful概念
感觉很抽象 不太明白
就记住了两个注解的作用
- @RequestMapping(value="/user",method = RequestMethod.GET)
- public @ResponseBody
- User printUser(@RequestParam(value = "id", required = false, defaultValue = "0")
- int id) {
- User user = new User();
- user = userService.getUserById(id);
- return user;
- }
- @RequestMapping(value="/user/{id:\\d+}",method = RequestMethod.GET)
- public @ResponseBody
- User printUser2(@PathVariable int id) {
- User user = new User();
- user = userService.getUserById(id);
- return user;
- }
还有个注解 只是看了大概作用 但是 深入的还是不懂
@ResponseBody
作用: 其实是将java对象转为json格式的数据。
关于 rest 和restful
知乎上看了关于 rest 和restful
https://www.zhihu.com/question/28557115/answer/48094438
总结了仨概念
虽然不是很理解 但先记下把
明天要做的事情:
解决mvc的错误
尝试完成任务3
根据任务 是要学习spring rest
和json tag-lib
问题:
对任务涉及的新知识点 不太了解
得一个一个学习
收获:
了解了mvc组件 流程
评论