Service层
3. BookmanageService.java
package com.xinxi2.service; import com.xinxi2.bean.Bookmanage; import java.util.List; public interface BookmanageService { List<Bookmanage> getlistBookmanage(Bookmanage bookmanage); int addBookmanage(Bookmanage bookmanage); Bookmanage updateByid(Integer id); int updateBookmanage(Bookmanage bookmanage); int deleteBookmanage(int id); }
4.BookmanageServiceImpl.java
package com.xinxi2.service.impl; import com.xinxi2.bean.Bookmanage; import com.xinxi2.dao.BookmanageMapper; import com.xinxi2.service.BookmanageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("bookmanageService") public class BookmanageServiceImpl implements BookmanageService { @Autowired private BookmanageMapper bookmanageMapper; @Override public List<Bookmanage> getlistBookmanage(Bookmanage bookmanage) { return bookmanageMapper.getlistBookmanage(bookmanage); } @Override public int addBookmanage(Bookmanage bookmanage) { return bookmanageMapper.addBookmanage(bookmanage); } @Override public Bookmanage updateByid(Integer id) { return bookmanageMapper.updateByid(id); } @Override public int updateBookmanage(Bookmanage bookmanage) { return bookmanageMapper.updateBookmanage(bookmanage); } @Override public int deleteBookmanage(int id) { return bookmanageMapper.deleteBookmanage(id); } }
Controller层
5. BookmanageConntroller.java
package com.xinxi2.controller; import com.xinxi2.bean.Bookmanage; import com.xinxi2.service.BookmanageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.nio.channels.Pipe; import java.util.List; @Controller @RequestMapping("/bookmanageConntroller") public class BookmanageConntroller { @Autowired private BookmanageService bookmanageService; @RequestMapping("/listBookmanage") public String ListBookmanage(Bookmanage bookmanage, Model model){ List<Bookmanage> list = bookmanageService.getlistBookmanage(bookmanage); model.addAttribute("tsList",list); return "list.jsp"; } @RequestMapping("tiaozhuan") public String tiaozhuan(){ return "add.jsp"; } @RequestMapping("/addBookmanage") public String addBookmanage(Bookmanage bookmanage){ bookmanageService.addBookmanage(bookmanage); return "redirect:/bookmanageConntroller/listBookmanage"; } @RequestMapping("/updateById") public String updateById(int id,Model model){ Bookmanage bookmanage = bookmanageService.updateByid(id); model.addAttribute("emp",bookmanage); return "update.jsp"; } @RequestMapping("/update") public String update(Bookmanage bookmanage){ bookmanageService.updateBookmanage(bookmanage); return "redirect:/bookmanageConntroller/listBookmanage"; } @RequestMapping("/delete") public String delete(int id){ bookmanageService.deleteBookmanage(id); return "redirect:/bookmanageConntroller/listBookmanage"; } }
5.7,编写jsp页面。
add.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2023/7/3 Time: 16:31 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form method="post" action="/bookmanageConntroller/addBookmanage"> 图书名称:<input type="text" name="name"><br> 图书作者:<input type="text" name="author"><br> 购买时间:<input type="text" name="time"><br> 图书分类:<input type="text" name="type"><br> <input type="submit"> </form> </body> </html>
list.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2023/7/3 Time: 16:18 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <table border="1"> <tr> <td>图书编号</td> <td>图书名称</td> <td>图书作者</td> <td>购买时间</td> <td>图书分类</td> <td>操作</td> </tr> <c:forEach items="${tsList}" var="b"> <tr> <td>${b.id}</td> <td>${b.name}</td> <td>${b.author}</td> <td>${b.time}</td> <td class="Fenlei">${b.type}</td> <td> <a href="/bookmanageConntroller/updateById?id=${b.id}">修改</a> <a href="/bookmanageConntroller/delete?id=${b.id}">删除</a> </td> </tr> </c:forEach> </table> <a href="/bookmanageConntroller/tiaozhuan">添加</a> </body> <script> var fenlei = document.getElementsByClassName("Fenlei"); for (var i=0;i<fenlei.length;i++){ var fenleiVal = fenlei[i].innerHTML; if (fenleiVal==1){ fenlei[i].innerHTML="计算机/软件" }else if (fenleiVal==2){ fenlei[i].innerHTML="小说/文摘" }else { fenlei[i].innerHTML="杂项" } } </script> </html>
update.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2023/7/3 Time: 16:40 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form method="post" action="/bookmanageConntroller/update"> <input type="hidden" name="id" value="${emp.id}"> 图书名称:<input type="text" name="name" value="${emp.name}"><br> 图书作者:<input type="text" name="author" value="${emp.author}"><br> 购买时间:<input type="text" name="time" value="${emp.time}"><br> 图书分类:<input type="text" name="type" value="${emp.type}"><br> <input type="submit"> </form> </body> </html>
6,JSON数据传递过程中的中文乱码和日期问题
6.1,解决JSON数据传递过程中出现的中文乱码问题
原因
Spring MVC框架中内置了一个处理String类型数据的消息转换器(org.springframework.http.converter.StringHttpMessageConverter),该消息转换器中固定了转换字符编码为"ISO-8859-1"
1.方法一
在控制器方法上设置编码格式
@ResponseBody @GetMapping(value="/{id}/view" ,produces={"application/json;charset=UTF-8"}) public Object view(@PathVariable String id){ //…….中间代码省略 }
2.方法2
装配消息转换器StringHttpMessageConverter
<mvc:annotation-driven> <mvc:message-converters> <bean class= "org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
6.2,解决JSON数据传递过程中出现的日期格式问题
原因
日期格式:时间戳形式(1077033600000) “yyyy-MM-dd”
1.方法一
注解方式
public class SysUser(){ @DateTimeFormat(pattern="yyyy-MM-dd") @JSONField(format="yyyy-MM-dd") private Date birthday; // 省略其他属性及getter、setter方法 }
2.方法二
装配消息转换器StringHttpMessageConverter
<mvc:annotation-driven> <mvc:message-converters> <bean class= "org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
7,多视图解析器
- 可以根据Accept值、扩展名等,把相同的数据内容以不同格式返回,呈现不同的视图效果
- 查询用户详情接口中以JSON纯数据的格式返回数据
7.1,多视图解析管理器ContentNegotiationManager
1.判断请求所要求的MIME类型,以决定所采用的视图
2.favorParameter
表示支持参数匹配,可以根据请求参数值确定MIME类型,默认的请求参数为format,默认值为true(支持)
3.favorPathExtension
表示是否支持扩展名,扩展名指xxx.json、xxx.xml等形式,默认为true(支持)
4.defaultContentType
配置默认的ContentType类型
5.mediaTypes
根据请求参数的MIME类型决定接口返回数据的展示类型,MIME类型包括json=application/json、xml=application/xml、html=text/html等
7.2,多视图解析器ContentNegotiatingViewResolver
1.contentNegotiationManager
注入自定义的多视图解析管理器
2.defaultViews
指定默认视图
3.viewResolvers
设置视图解析器