发表于: 2017-08-27 23:10:48
1 953
今天完成的事情:完成是ssm对数据库的增删改查
使用postman测好接口
使用表单实现 相关提交
这里主要粘贴一下controller代码
// 告诉spring mvc这是一个控制器类
@Controller
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping("/listCategory")
public ModelAndView listCategory() {
ModelAndView mav = new ModelAndView();
List<Category> cs = categoryService.list();
// 放入转发参数
mav.addObject("cs", cs);
// 放入jsp路径
mav.setViewName("listCategory");
return mav;
}
@RequestMapping(value = "/a/category/all", method = RequestMethod.GET)
public String allCategory(HttpServletRequest request, HttpServletResponse response, Model model) {
try {
List<Category> categoryList = categoryService.list();
int i = categoryList.size();
model.addAttribute("category", categoryList);
} catch (Exception e) {
e.printStackTrace();
return "common/errorJson";
}
return "category/listJson";
}
@RequestMapping(value = "/a/student/select/{id}", method = RequestMethod.GET)
public String detail(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Integer id) {
if (id != null) {
try {
Category category = this.categoryService.get(id);
//将数据绑定到model上,可以传入jsp页面
model.addAttribute("category", category);
} catch (Exception e) {
e.printStackTrace();
return "common/errorJson";
}
return "category/categoryDataliJson";
} else {
return "common/errorParameterJson";
}
}
@RequestMapping(value = "/a/student/select/{id}", method = RequestMethod.POST)
public String detail1(HttpServletRequest request, HttpServletResponse response, Model model, Integer id) {
if (id != null) {
try {
Category category = this.categoryService.get(id);
//将数据绑定到model上,可以传入jsp页面
model.addAttribute("category", category);
} catch (Exception e) {
e.printStackTrace();
return "common/errorJson";
}
return "category/categoryDataliJson";
} else {
return "common/errorParameterJson";
}
}
@RequestMapping(value = "/a/category", method = RequestMethod.POST)
public String add(HttpServletRequest request, HttpServletResponse reponse, Model model, @ModelAttribute Category category) throws UnsupportedEncodingException {
try {
categoryService.add(category);
model.addAttribute("cat", category.getName());
// ModelAndView modelAndView=new ModelAndView();
// modelAndView.addObject("cat",cat);
// request.setAttribute("cat",category.getName());
} catch (Exception e) {
e.printStackTrace();
return " common/errorJson";
}
/* request.setCharacterEncoding("utf-8");
reponse.setCharacterEncoding("utf8");*/
return "common/successJson";
}
/* @RequestMapping(value="/a/category",method=RequestMethod.GET)
public String add2(HttpServletRequest request, HttpServletResponse reponse, Model model, Category category) {
if (category.getName() != null) {
try {
Integer i = categoryService.add(category);
if (i == -1) {
return "common/successJson";
}
} catch (Exception e) {
e.printStackTrace();
return "common/errorJson";
}
} else {
return "common/errorParameterJson";
}
return "common/successJson";
}*/
@RequestMapping(value="/a/category/shancu/{id}",method = RequestMethod.GET)
public String delete(HttpServletRequest request,HttpServletResponse response,Model model,@PathVariable int id){
try{
int i=categoryService.delete(id);
}catch(Exception e){
e.printStackTrace();
return " common/errorJson";
}
return "common/successJson";
}
}
逻辑还是有点混乱 还是不进行非空验证了
再把index.jsp页面展示一下
<html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<body>
<h2>Hello World!</h2>
<form action="/a/category/shancu/{id}" method="post">
id:<input type="text" name="id"/>
<br>
<input type="submit" value="Delete"/>
<br>
<form action="/a/category" method="post">
name:<input type="text" name="name"/>
<br>
<input type="submit" value="Submit"/>
</form>
<br> <br>
<a href="/a/category/all">显示全部</a>
<%--<a href="/a/student/select/{id}">查询</a>--%>
<form action="/a/student/select/{id}" method="post">
id:<input type="text" name="id"/>
<br>
<input type="submit" value="Submit"/>
</form>
<br> <br>
</body>
</html>
明天计划的事情:继续整理代码 被师兄们批评 代码不符合规范 乱用_ 使用路径随意
还是要整理知识点 学了很多也用了很多 但还是有点乱
遇到的问题:使用插入数据的方法的时候 声明的返回类型为对象 所以一直在报错
应该改成int 返回的id不是实际的id是受影响的行数
对象实际上是通过srrvice注入进去的 再使用requet来对它进行取值
@Service
public class CategoryServiceImpl implements CategoryService {
/* @Autowired
private CategoryMapper dao;
*/
@Resource
private CategoryMapper dao;
public List<Category> list(){
List<Category> category = this.dao.list();
return category;
}
public Category get(Integer id){
Category category = this.dao.get(id);
return category;
}
收获:学习了很多知识点 更详尽的使用了ModelAndView
(HttpServletRequest request, HttpServletResponse reponse, Model model, @ModelAttribute
对jsp文件使用的也更加熟练 尤其是表单
展示一下
页面还是很low
任务进度:略微延后半天 知识点比较多急需整理
禅道链接:http://task.ptteng.com/zentao/my-task-assignedTo.html
评论