小言_互联网的博客

SSM框架实战-搭建自己的个人博客2-UEditor编辑器的使用

450人阅读  评论(0)

目录

UEditor

博客内容提交与展示功能测试

Controller开发

新增博客页面add_ueditor.jsp

博客详情界面detail.jsp

博客新增和展示详情功能开发

博客存储

博客标题开发

标签POJO类

TagMapper

TagService

映射文件TagMapper.xml

博客类别开发

类别POJO类

CategoryMapper

CategoryService 

CategoryMapper.xml

博客内容开发

类别POJO类

ArticleMapper.xml

ConvertBlobTypeHandler 

ArticleController

 部署至服务器 

文章数据提交

后台JS处理

ArticleController

 文章数据保存至数据库

Json数据转换

ArticleMapper 

ArticleService 

ArticleController

 ArticleMapper.xml

部署至服务器 

博客内容展示

article_detail.jsp

时间格式化

 分类ID与类名转换

报错汇总

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $


UEditor

UEditor 是由百度 web 前端研发部开发所见即所得富文本web编辑器,具有轻量、可定制、注重用户体验等特点。

首先去https://github.com/fex-team/ueditor下载UEditor,然后将文件拷贝至项目中。

接下来通过一个Demo对UEditor进行测试。

通过Controller的TestDemo进行测试。


  
  1. @Controller
  2. public class TestController {
  3. @RequestMapping("/ueTest")
  4. public String test(){
  5. return "ueditortest";
  6. }
  7. }

新建一个ueditorTest.jsp,在body中调用js


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%@ taglib prefix="fm" uri="http://java.sun.com/jsp/jstl/functions" %>
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
  5. <%
  6. String path = request.getContextPath();
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  8. %>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html>
  11. <head>
  12. <title>富文本编辑器测试 </title>
  13. </head>
  14. <body class="gray-bg">
  15. <script id="container" name="content" type="text/plain">这里写你的初始化内容 </script>
  16. <script type="text/javascript" src="<%=basePath%>js/ueditor1_4_3/ueditor.config.js"> </script>
  17. <script type="text/javascript" src="<%=basePath%>js/ueditor1_4_3/ueditor.all.js"> </script>
  18. <!-- 实例化编辑器 -->
  19. <script type="text/javascript">
  20. var ue = UE.getEditor( 'container');
  21. </script>
  22. </body>
  23. </html>

此外,为了加载网页中的静态资源,如图片,js,css,需要对servlet进行配置:

根据自己的服务器选择不同包下的DefaultServlet


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns= "http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. version= "2.5">
  6. <context-param>
  7. <param-name>contextConfigLocation </param-name>
  8. <param-value> classpath:spring-core.xml </param-value>
  9. </context-param>
  10. <listener>
  11. <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
  12. </listener>
  13. <servlet>
  14. <servlet-name>Blog </servlet-name>
  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
  16. <init-param>
  17. <param-name>contextConfigLocation </param-name>
  18. <param-value>classpath:spring-mvc.xml </param-value>
  19. </init-param>
  20. </servlet>
  21. <servlet-mapping>
  22. <servlet-name>Blog </servlet-name>
  23. <url-pattern>/ </url-pattern>
  24. </servlet-mapping>
  25. <!-- 静态资源的加载-->
  26. <servlet>
  27. <servlet-name>default </servlet-name>
  28. <!--jetty容器-->
  29. <!-- <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>-->
  30. <!--Tomcat容器-->
  31. <servlet-class>org.apache.catalina.servlets.DefaultServlet </servlet-class>
  32. <init-param>
  33. <param-name>debug </param-name>
  34. <param-value>0 </param-value>
  35. </init-param>
  36. <init-param>
  37. <param-name>listings </param-name>
  38. <param-value>false </param-value>
  39. </init-param>
  40. <load-on-startup>1 </load-on-startup>
  41. </servlet>
  42. <servlet-mapping>
  43. <servlet-name>default </servlet-name>
  44. <url-pattern>*.jpg </url-pattern>
  45. </servlet-mapping>
  46. <servlet-mapping>
  47. <servlet-name>default </servlet-name>
  48. <url-pattern>*.js </url-pattern>
  49. </servlet-mapping>
  50. <servlet-mapping>
  51. <servlet-name>default </servlet-name>
  52. <url-pattern>*.css </url-pattern>
  53. </servlet-mapping>
  54. <servlet-mapping>
  55. <servlet-name>default </servlet-name>
  56. <url-pattern>*.png </url-pattern>
  57. </servlet-mapping>
  58. <servlet-mapping>
  59. <servlet-name>default </servlet-name>
  60. <url-pattern>*.woff2 </url-pattern>
  61. </servlet-mapping>
  62. <servlet-mapping>
  63. <servlet-name>default </servlet-name>
  64. <url-pattern>*.woff3 </url-pattern>
  65. </servlet-mapping>
  66. <servlet-mapping>
  67. <servlet-name>default </servlet-name>
  68. <url-pattern>*.ttf </url-pattern>
  69. </servlet-mapping>
  70. <servlet-mapping>
  71. <servlet-name>default </servlet-name>
  72. <url-pattern>*.woff </url-pattern>
  73. </servlet-mapping>
  74. <servlet-mapping>
  75. <servlet-name>default </servlet-name>
  76. <url-pattern>*.gif </url-pattern>
  77. </servlet-mapping>
  78. <servlet-mapping>
  79. <servlet-name>default </servlet-name>
  80. <url-pattern>*.map </url-pattern>
  81. </servlet-mapping>
  82. <servlet-mapping>
  83. <servlet-name>default </servlet-name>
  84. <url-pattern>*.html </url-pattern>
  85. </servlet-mapping>
  86. </web-app>

启动服务器后如下图,就说明配置成功,接下来就可以对UEditor进行编程了。

博客内容提交与展示功能测试

jsp页面:新增页面:add_ueditor    详情页:ueditor_detail

controller方法:add   addContent     detail

运行流程:

/add  ->   add方法 ->add_ueditor.jsp ->提交按钮->/addContent->detail

Controller开发


  
  1. package com.tulun.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. @Controller
  6. @RequestMapping("/test")
  7. public class TestController {
  8. private String context ;
  9. @RequestMapping("/add")
  10. public String test(){
  11. return "add_ueditor";
  12. }
  13. //点击保存按钮,将内容提交到后台
  14. @RequestMapping("/addContent")
  15. public String addContent(String description,String content) {
  16. System.out.println( "content:"+content);
  17. System.out.println( "description:"+description);
  18. context = content;
  19. return "redirect:/test/detail";
  20. }
  21. //详情页面
  22. @RequestMapping("/detail")
  23. public String detail(Model model){
  24. model.addAttribute( "content", context);
  25. return "ueditor_detail";
  26. }
  27. }

新增博客页面add_ueditor.jsp

页面中新增两个按钮,点击提交按钮调用js中的saveArticle方法进行提交


  
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*" %>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%@ taglib prefix="fm" uri="http://java.sun.com/jsp/jstl/functions" %>
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
  5. <% String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  9. <html>
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  12. <link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet">
  13. <link href="<%=basePath%>css/style.css" rel="stylesheet">
  14. <title>新增博客 </title>
  15. </head>
  16. <body>
  17. <div class="ibox-content">
  18. <form method="get" class="form-horizontal">
  19. <div class="hr-line-dashed"> </div>
  20. <div class="form-group">
  21. <label class="col-sm-2 control-label">内容: </label>
  22. <div class="col-sm-10">
  23. <script id="editor" type="text/plain"> </script>
  24. </div>
  25. </div>
  26. <div class="form-group">
  27. <div class="col-sm-4 col-sm-offset-2">
  28. <button class="btn btn-primary" type="button" onclick="saveArticle()">保存内容 </button>
  29. <button class="btn btn-white" type="button" onclick="cancelSaveArticle()">取消 </button>
  30. </div>
  31. </div>
  32. </form>
  33. </div>
  34. <!-- Mainly scripts -->
  35. <script src="<%=basePath%>js/jquery-2.1.1.min.js"> </script>
  36. <script src="<%=basePath%>js/bootstrap.min.js"> </script>
  37. <%--自定义js--%>
  38. <script src="<%=basePath%>js/article/add_article_test.js"> </script>
  39. <!-- 配置文件 -->
  40. <script type="text/javascript" src="<%=basePath%>js/ueditor1_4_3/ueditor.config.js"> </script>
  41. <!-- 编辑器源码文件 -->
  42. <script type="text/javascript" src="<%=basePath%>js/ueditor1_4_3/ueditor.all.js"> </script>
  43. <script type="text/javascript" charset="utf-8" src="<%=basePath%>js/ueditor1_4_3/lang/zh-cn/zh-cn.js"> </script>
  44. <script src="<%=basePath%>js/plugins/chosen/chosen.jquery.js"> </script>
  45. <script type="text/javascript">
  46. $( document).ready( function () {
  47. var ue = UE.getEditor( 'editor');
  48. })
  49. // 初始化多选框
  50. $( ".chosen-select").chosen({
  51. max_selected_options: 5,
  52. no_results_text: "没有找到",
  53. allow_single_deselect: true
  54. });
  55. </script>
  56. </body>
  57. </html>

在js文件中对saveArticle进行编写,获取到编辑器的内容,通过post方式进行提交,提交成功就跳转至detail进行显示,失败就返回add界面重新进行添加。 


  
  1. // 保存文章
  2. function saveArticle(){
  3. var arr = [];
  4. //获取输入的内容 getContent() (包含和页面相关的标签)
  5. arr.push(UE.getEditor( 'editor').getContent());
  6. var content = arr.join( "\n");
  7. // alert(content);
  8. // 简介 (getContentTxt() 获取纯文本内容,并且只截取前10个字)
  9. var description = UE.getEditor( 'editor').getContentTxt().substring( 0, 10);
  10. // 保存文章
  11. $.ajax({
  12. type : "POST",
  13. url : '../test/addContent', //提交内容用这个URL
  14. data : "content="+content+ "&description="+description,
  15. success : function(data) {
  16. if(data.resultCode != 'success'){
  17. //成功了就显示详情
  18. console.log( "sssssss"+content);
  19. window.location.href = "../test/detail";
  20. autoCloseAlert(data.errorInfo, 1000);
  21. return false;
  22. } else{
  23. alert( "失败哦");
  24. // 重新添加
  25. window.location.href = "../test/add";
  26. }
  27. }
  28. });
  29. }
  30. //取消后跳转到add
  31. function cancelSaveArticle(){
  32. window.location.href = "../test/add";
  33. }

 

博客详情界面detail.jsp

 用$获取到content的值,然后显示在页面上。


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
  5. <%
  6. String path = request.getContextPath();
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  8. %>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html>
  11. <head>
  12. <link rel="stylesheet" id="main-css" href="<%=basePath%>css/demo/style.css" type="text/css" media="all">
  13. <title>博客详情 </title>
  14. </head>
  15. <body id="contain" class="home blog ui-c3">
  16. <div class="content">
  17. <article class="article-content">
  18. ${content}
  19. </article>
  20. </div>
  21. </body>
  22. </html>

网页界面:

后台打印输入:

博客新增和展示详情功能开发

  • 博客新增页面:该页面主要用于新增一篇博客,其中包含了博客类别(可选择已有类别)、博客标签(可选择已有标签)、标题以及内容。
  • 展示详情:标题、标签、类别、内容、作者以及发布时间

博客存储

博客内容一般较大,肯定不能用常规数据类型进行存储,因此可以使用MySQL提供的blog类型进行存储,根据具体需求选择不同的类型。

  • .tinyblog:仅256个字符
  • .blog 最大限制65k字节
  • .mediumblog:限制16M字节
  • .logblog:限制4G

博客标题开发

标签POJO类


  
  1. package com.tulun.model;
  2. /**
  3. * Description :
  4. * Created by Resumebb
  5. * Date :2021/4/27
  6. */
  7. public class Tag {
  8. private Integer id;
  9. private String tagName;
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getTagName() {
  17. return tagName;
  18. }
  19. public void setTagName(String tagName) {
  20. this.tagName = tagName;
  21. }
  22. }

TagMapper

定义一个接口获取全部tag


  
  1. public interface TagMapper {
  2. public List<Tag> getAllTag();
  3. }

TagService


  
  1. package com.tulun.service;
  2. import com.tulun.dao.TagMapper;
  3. import com.tulun.model.Tag;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import java.util.List;
  7. /**
  8. * Description :
  9. * Created by Resumebb
  10. * Date :2021/4/27
  11. */
  12. @Service
  13. public class TagService {
  14. @Autowired
  15. private TagMapper tagMapper;
  16. public List<Tag> selectAllTag(){
  17. return tagMapper.getAllTag();
  18. }
  19. }

映射文件TagMapper.xml

从t_tag表中查询所有数据,做一个映射


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.tulun.dao.TagMapper">
  6. <select id="getAllTag" resultType="com.tulun.model.Tag">
  7. select * from t_tag
  8. </select>
  9. </mapper>

博客类别开发

与标签开发过程一样,分别进行配置。

类别POJO类


  
  1. package com.tulun.model;
  2. /**
  3. * Description :
  4. * Created by Resumebb
  5. * Date :2021/4/27
  6. */
  7. public class Category {
  8. private Integer id;
  9. private String categoryName;
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getCategoryName() {
  17. return categoryName;
  18. }
  19. public void setCategoryName(String categoryName) {
  20. this.categoryName = categoryName;
  21. }
  22. }

CategoryMapper


  
  1. public interface CategoryMapper {
  2. public List<Category> getAllCategory();
  3. }

CategoryService 


  
  1. @Service
  2. public class CategoryService {
  3. @Autowired
  4. private CategoryMapper categoryMapper;
  5. public List<Category> selectAllCategory(){
  6. return categoryMapper.getAllCategory();
  7. }
  8. }

CategoryMapper.xml

查询t_category表中的数据


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.tulun.dao.CategoryMapper">
  6. <select id="getAllCategory" resultType="com.tulun.model.Category">
  7. select * from t_category
  8. </select>
  9. </mapper>

博客内容开发

首先创建对应的POJO类,所有元素均与数据库一一对应

类别POJO类


  
  1. package com.tulun.model;
  2. import java.util.Date;
  3. /**
  4. * Description :
  5. * Created by Resumebb
  6. * Date :2021/4/27
  7. */
  8. public class Article {
  9. private Integer id;
  10. private Integer categoryId;
  11. private String title;
  12. private String content;
  13. private String decription;
  14. private Integer status;
  15. private String author;
  16. private Date createTime;
  17. private Integer showCount;
  18. public Integer getId() {
  19. return id;
  20. }
  21. public void setId(Integer id) {
  22. this.id = id;
  23. }
  24. public Integer getCategoryId() {
  25. return categoryId;
  26. }
  27. public void setCategoryId(Integer categoryId) {
  28. this.categoryId = categoryId;
  29. }
  30. public String getTitle() {
  31. return title;
  32. }
  33. public void setTitle(String title) {
  34. this.title = title;
  35. }
  36. public String getContent() {
  37. return content;
  38. }
  39. public void setContent(String content) {
  40. this.content = content;
  41. }
  42. public String getDecription() {
  43. return decription;
  44. }
  45. public void setDecription(String decription) {
  46. this.decription = decription;
  47. }
  48. public Integer getStatus() {
  49. return status;
  50. }
  51. public void setStatus(Integer status) {
  52. this.status = status;
  53. }
  54. public String getAuthor() {
  55. return author;
  56. }
  57. public void setAuthor(String author) {
  58. this.author = author;
  59. }
  60. public Date getCreateTime() {
  61. return createTime;
  62. }
  63. public void setCreateTime(Date createTime) {
  64. this.createTime = createTime;
  65. }
  66. public Integer getShowCount() {
  67. return showCount;
  68. }
  69. public void setShowCount(Integer showCount) {
  70. this.showCount = showCount;
  71. }
  72. @Override
  73. public String toString() {
  74. return "Article{" +
  75. "id=" + id +
  76. ", catagoryId=" + categoryId +
  77. ", title='" + title + '\'' +
  78. ", content='" + content + '\'' +
  79. ", decription='" + decription + '\'' +
  80. ", status=" + status +
  81. ", author='" + author + '\'' +
  82. ", createTime=" + createTime +
  83. ", showCount=" + showCount +
  84. '}';
  85. }
  86. }

ArticleMapper.xml

 返回类型采用resultMap类型,这里使用到了一个类型转换的处理器类ConvertBlobTypeHandler 


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.tulun.dao.ArticleMapper">
  6. <resultMap id="articleMap" type="com.tulun.model.Article">
  7. <result column="content" property="content" typeHandler="com.tulun.util.ConvertBlobTypeHandler"> </result>
  8. </resultMap>
  9. </mapper>

ConvertBlobTypeHandler 

该工具类主要用于将博客中的blog类型转换为String类型。


  
  1. package com.tulun.util;
  2. import org.apache.ibatis.type.BaseTypeHandler;
  3. import org.apache.ibatis.type.JdbcType;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.UnsupportedEncodingException;
  6. import java.sql.*;
  7. /**
  8. * @desc Blob转换String处理器,解决MyBatis存储blob字段后,出现乱码的问题
  9. */
  10. public class ConvertBlobTypeHandler extends BaseTypeHandler<String> {
  11. private static final String DEFAULT_CHARSET = "utf-8";
  12. @Override
  13. public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
  14. Blob blob = rs.getBlob(columnName);
  15. if ( null == blob) {
  16. return null;
  17. }
  18. byte[] returnValue = null;
  19. returnValue = blob.getBytes( 1, ( int) blob.length());
  20. try {
  21. // ###把byte转化成string
  22. return new String(returnValue, DEFAULT_CHARSET);
  23. } catch (UnsupportedEncodingException e) {
  24. throw new RuntimeException( "Blob Encoding Error!");
  25. }
  26. }
  27. @Override
  28. public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  29. Blob blob = rs.getBlob(columnIndex);
  30. if ( null == blob) {
  31. return null;
  32. }
  33. byte[] returnValue = null;
  34. returnValue = blob.getBytes( 1, ( int) blob.length());
  35. try {
  36. // ###把byte转化成string
  37. return new String(returnValue, DEFAULT_CHARSET);
  38. } catch (UnsupportedEncodingException e) {
  39. throw new RuntimeException( "Blob Encoding Error!");
  40. }
  41. }
  42. @Override
  43. public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
  44. Blob blob = cs.getBlob(columnIndex);
  45. if ( null == blob) {
  46. return null;
  47. }
  48. byte[] returnValue = null;
  49. returnValue = blob.getBytes( 1, ( int) blob.length());
  50. try {
  51. // ###把byte转化成string
  52. return new String(returnValue, DEFAULT_CHARSET);
  53. } catch (UnsupportedEncodingException e) {
  54. throw new RuntimeException( "Blob Encoding Error!");
  55. }
  56. }
  57. @Override
  58. public void setNonNullParameter(PreparedStatement ps, int parameterIndex, String parameter, JdbcType jdbcType)
  59. throws SQLException {
  60. ByteArrayInputStream bis = null;
  61. try {
  62. // ###把String转化成byte流
  63. bis = new ByteArrayInputStream(parameter.getBytes(DEFAULT_CHARSET));
  64. } catch (UnsupportedEncodingException e) {
  65. throw new RuntimeException( "Blob Encoding Error!");
  66. }
  67. ps.setBinaryStream(parameterIndex, bis, parameter.length());
  68. }
  69. }

ArticleController

同样设计二级映射,方便归类,注入标签与类别的Service实例,通过实例调用select**方法,获取到的对象存储于List中,通过Model类的addAttribute方法进行添加。


  
  1. package com.tulun.controller;
  2. import com.tulun.model.Category;
  3. import com.tulun.model.Tag;
  4. import com.tulun.service.CategoryService;
  5. import com.tulun.service.TagService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.Model;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import java.util.List;
  11. /**
  12. * Description :
  13. * Created by Resumebb
  14. * Date :2021/4/27
  15. */
  16. @Controller
  17. @RequestMapping("/article")
  18. public class ArticleController {
  19. @Autowired
  20. private TagService tagService;
  21. @Autowired
  22. private CategoryService categoryService;
  23. @RequestMapping("/add")
  24. public String add(Model model) {
  25. //获取类别数据
  26. List<Category> categories = categoryService.selectAllCategory();
  27. //获取标签数据
  28. List <Tag> tags = tagService.selectAllTag();
  29. model.addAttribute( "categoryList", categories);
  30. model.addAttribute( "tagList", tags);
  31. return "article/add_article";
  32. }
  33. }

新增页面jsp开发


  
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*" %>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%@ taglib prefix="fm" uri="http://java.sun.com/jsp/jstl/functions" %>
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
  5. <%
  6. String path = request.getContextPath();
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  8. %>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  10. <html>
  11. <head>
  12. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <meta name="renderer" content="webkit">
  15. <link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet">
  16. <link href="<%=basePath%>font-awesome/css/font-awesome.css" rel="stylesheet">
  17. <!-- Data Tables -->
  18. <link href="<%=basePath%>css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
  19. <link href="<%=basePath%>css/animate.css" rel="stylesheet">
  20. <link href="<%=basePath%>css/style.css" rel="stylesheet">
  21. <link rel="stylesheet" type="text/css" href="<%=basePath%>js/flavr/flavr/css/animate.css" />
  22. <link rel="stylesheet" type="text/css" href="<%=basePath%>js/flavr/flavr/css/flavr.css" />
  23. <link href="<%=basePath%>css/plugins/datapicker/datepicker3.css" rel="stylesheet">
  24. <link href="<%=basePath%>css/plugins/chosen/chosen.css" rel="stylesheet">
  25. <style>
  26. .input-upload{ position: relative;}
  27. .input-upload input [type="file"]{ position: absolute; left: 0px; top: 0px; width: 72px; height: 35px; opacity:. 0; filter: alpha(opacity= 00);}
  28. </style>
  29. <title>新增博客 </title>
  30. </head>
  31. <body>
  32. <div id="wrapper">
  33. <div id="page-wrapper" class="gray-bg dashbard-1">
  34. <%--<div class="row wrapper border-bottom white-bg page-heading">--%>
  35. <%--</div>--%>
  36. <div class="wrapper wrapper-content animated fadeInRight">
  37. <div class="row">
  38. <div class="col-lg-12">
  39. <div class="ibox float-e-margins">
  40. <div class="ibox-content">
  41. <form method="get" class="form-horizontal">
  42. <div class="form-group">
  43. <label class="col-sm-2 control-label">类别: </label>
  44. <div class="col-sm-4">
  45. <select class="form-control m-b" id="categoryId">
  46. <option value="-1"></option>
  47. <c:forEach items="${categoryList}" var="category">
  48. <option value="${category.id}">${category.categoryName} </option>
  49. </c:forEach>
  50. </select>
  51. </div>
  52. </div>
  53. <div class="form-group">
  54. <label class="col-sm-2 control-label">博客标题 </label>
  55. <div class="col-sm-10">
  56. <input type="text" id="title" class="form-control" placeholder="请填写博客名">
  57. </div>
  58. </div>
  59. <div class="hr-line-dashed"> </div>
  60. <div class="form-group">
  61. <label class="col-sm-2 control-label">博客内容: </label>
  62. <div class="col-sm-10">
  63. <script id="editor" type="text/plain"> </script>
  64. </div>
  65. </div>
  66. <div class="hr-line-dashed"> </div>
  67. <div class="form-group">
  68. <label class="col-sm-2 control-label">标签: </label>
  69. <div class="col-sm-10">
  70. <select data-placeholder="请选择标签" class="chosen-select" multiple style="width:450px;" tabindex="5">
  71. <c:forEach items="${tagList}" var="tag">
  72. <option value="${tag.id}" hassubinfo="true">${tag.tagName} </option>
  73. </c:forEach>
  74. </select>
  75. </div>
  76. </div>
  77. <div class="form-group">
  78. <div class="col-sm-4 col-sm-offset-2">
  79. <button class="btn btn-primary" type="button" onclick="saveArticle()">保存 </button>
  80. <button class="btn btn-white" type="button" onclick="cancelSaveArticle()">取消 </button>
  81. </div>
  82. </div>
  83. </form>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. <!-- Mainly scripts -->
  93. <script src="<%=basePath%>js/jquery-2.1.1.min.js"> </script>
  94. <script src="<%=basePath%>js/bootstrap.min.js"> </script>
  95. <script src="<%=basePath%>js/plugins/metisMenu/jquery.metisMenu.js"> </script>
  96. <script src="<%=basePath%>js/plugins/slimscroll/jquery.slimscroll.min.js"> </script>
  97. <script src="<%=basePath%>js/hplus.js"> </script>
  98. <script src="<%=basePath%>js/plugins/pace/pace.min.js"> </script>
  99. <script src="<%=basePath%>js/validation.js"> </script>
  100. <%--自定义js--%>
  101. <script src="<%=basePath%>js/article/add_article.js"> </script>
  102. <script type="text/javascript" src="<%=basePath%>js/flavr/flavr/js/flavr.min.js"> </script>
  103. <script type="text/javascript" src="<%=basePath%>js/flavr/base.js"> </script>
  104. <script type="text/javascript" src="<%=basePath%>js/zTree_v3/js/jquery.ztree.all-3.5.js"> </script>
  105. <script src="<%=basePath%>js/plugins/datapicker/bootstrap-datepicker.js"> </script>
  106. <script type="text/javascript" src="<%=basePath%>js/ueditor1_4_3/ueditor.config.js"> </script>
  107. <script type="text/javascript" src="<%=basePath%>js/ueditor1_4_3/ueditor.all.js"> </script>
  108. <script type="text/javascript" charset="utf-8" src="<%=basePath%>js/ueditor1_4_3/lang/zh-cn/zh-cn.js"> </script>
  109. <script type="text/javascript" src="<%=basePath%>js/ajaxfileupload.js"> </script>
  110. <script src="<%=basePath%>js/plugins/chosen/chosen.jquery.js"> </script>
  111. <script type="text/javascript" src="<%=basePath%>js/imageUtils.js"> </script>
  112. <script type="text/javascript">
  113. $( document).ready( function () {
  114. var UEDITOR_HOME_URL = "/js/ueditor1_4_3/";
  115. var ue = UE.getEditor( 'editor');
  116. })
  117. $( ".chosen-select").chosen({
  118. max_selected_options: 5,
  119. no_results_text: "û���ҵ�",
  120. allow_single_deselect: true
  121. });
  122. </script>
  123. </body>
  124. </html>

 

 部署至服务器 

通过select标签可以进行类别与标签的选择。

文章数据提交

博客编辑好了之后,下一步就需要进行数据的提交。

后台JS处理

后台js对数据进行解析,其中的每一项都需要选中信息或填入信息,如果没有信息,就利用ajax进行弹窗提示,时长为500ms。之后再通过param进行数据加密,防止数据明文显示被抓包。如果保存成功将重定位至list。


  
  1. // 保存文章
  2. function saveArticle(){
  3. var param = {};
  4. // 收集参数 校验
  5. //$("#categoryId").val()
  6. var categoryId = $( "#categoryId").val();
  7. if(categoryId == '-1'){
  8. autoCloseAlert( "请选择栏目", 500);
  9. return false;
  10. }
  11. param[ "categoryId"] = categoryId;
  12. var title = $( "#title").val();
  13. if(isEmpty(title)){
  14. autoCloseAlert( "请输入标题", 500);
  15. return false;
  16. }
  17. param[ "title"] = title;
  18. var arr = [];
  19. arr.push(UE.getEditor( 'editor').getContent());
  20. var content = arr.join( "\n");
  21. // 简介
  22. var description = UE.getEditor( 'editor').getContentTxt().substring( 0, 500);
  23. // 标签
  24. var tagId = $( ".chosen-select").val();
  25. // alert(tagId);
  26. if(!isEmpty(tagId)){
  27. var ids = (tagId+ "").split( "\,");
  28. var tagArray = [];
  29. for( var i= 0;i<ids.length;i++){
  30. tagObj = { "id":ids[i]};
  31. // alert(tagObj.id);
  32. tagArray.push(tagObj);
  33. }
  34. param[ "tagList"] = tagArray;
  35. console.info(tagArray);
  36. } else{
  37. autoCloseAlert( "请输入标签", 500);
  38. return false;
  39. }
  40. // alert('param='+encodeURI(encodeURI(JSON.stringify(param))));
  41. // 保存文章
  42. $.ajax({
  43. type : "POST",
  44. url : '../article/addContent',
  45. data : 'param='+ encodeURI( encodeURI( JSON.stringify(param)))+ "&content="+ encodeURI( encodeURI(content)).replace( /\&/g, "%26").replace( /\+/g, "%2B")+ "&description="+ encodeURI( encodeURI(description)),
  46. success : function(data) {
  47. if(data.resultCode != 'success'){
  48. autoCloseAlert(data.errorInfo, 1000);
  49. return false;
  50. } else{
  51. // 调到列表页
  52. window.location.href = "../article/list";
  53. }
  54. }
  55. });
  56. }
  57. function cancelSaveArticle(){
  58. window.location.href = "../article/list";
  59. }

ArticleController

在ArticleController中新增addContent方法,利用URLDecoder进行数据的解密。


  
  1. @RequestMapping("/addContent")
  2. @ResponseBody
  3. public Result addContent(String param, String content, String description) throws UnsupportedEncodingException {
  4. String param1 = URLDecoder.decode(param, "utf-8");
  5. String content1 = URLDecoder.decode(content, "utf-8");
  6. String description1 = URLDecoder.decode(description, "utf-8");
  7. System.out.println( "content:"+content);
  8. System.out.println( "description:"+description);
  9. System.out.println( "content1:"+content1);
  10. System.out.println( "description1:"+description1);
  11. return new Result( "success", "处理成功");
  12. }

这里用到一个Result类,它是用于存储返回结果的,属性主要包含操作结果,错误信息,以及附属对象。


  
  1. package com.tulun.model;
  2. import org.apache.log4j.Logger;
  3. /**
  4. * 封装统一的结果集给前端
  5. */
  6. public class Result {
  7. // 操作结果
  8. private String resultCode;
  9. // 错误信息
  10. private String errorInfo;
  11. // 附属对象
  12. private Object object;
  13. public Result(String resultCode, String errorInfo) {
  14. super();
  15. this.resultCode = resultCode;
  16. //this.errorInfo = "错误";
  17. this.errorInfo = errorInfo;
  18. System.out.println(errorInfo);
  19. }
  20. public Result(String resultCode, String errorInfo, Object object) {
  21. super();
  22. this.resultCode = resultCode;
  23. this.errorInfo = errorInfo;
  24. this.object = object;
  25. }
  26. public String getResultCode() {
  27. return resultCode;
  28. }
  29. public void setResultCode(String resultCode) {
  30. this.resultCode = resultCode;
  31. }
  32. public String getErrorInfo() {
  33. return errorInfo;
  34. }
  35. public void setErrorInfo(String errorInfo) {
  36. this.errorInfo = errorInfo;
  37. }
  38. public Object getObject() {
  39. return object;
  40. }
  41. public void setObject(Object object) {
  42. this.object = object;
  43. }
  44. @Override
  45. public String toString() {
  46. return "Result{" +
  47. "resultCode='" + resultCode + '\'' +
  48. ", errorInfo='" + errorInfo + '\'' +
  49. ", object=" + object +
  50. '}';
  51. }
  52. }

接下来部署到服务器进行打印测试:

上面为加密信息,下面为解密信息 ,可以看出加密解密成功实现。

 文章数据保存至数据库

Json数据转换

该工具类的作用是实现Json字符串与对象的相互转换,因为在插入数据库过程中,将其转换为对象操作起来更加方便,通过Set方法就可以进行设置。


  
  1. package com.tulun.util;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import com.google.gson.JsonSyntaxException;
  5. import javax.servlet.ServletOutputStream;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.UnsupportedEncodingException;
  11. public class JsonUtil {
  12. public static Gson getGson() {
  13. return new Gson();
  14. }
  15. /**
  16. * 将对象转为JSON字符串(忽略NULL值)
  17. *
  18. * @param src
  19. * @return
  20. */
  21. public static String toJson(Object src) {
  22. return getGson().toJson(src);
  23. }
  24. /**
  25. * 将对象转为JSON字符串(不忽略NULL值)
  26. *
  27. * @param src
  28. * @param serializeNulls
  29. * @return
  30. */
  31. public static String toJson(Object src, boolean serializeNulls) {
  32. if (serializeNulls)
  33. return new GsonBuilder().serializeNulls().create().toJson(src);
  34. return toJson(src);
  35. }
  36. /**
  37. * 将JSON字符串转为对象
  38. *
  39. * @param json
  40. * @param classOfT
  41. * @return
  42. * @throws JsonSyntaxException
  43. */
  44. public static <T> T fromJson(String json, Class<T> classOfT)
  45. throws JsonSyntaxException {
  46. return getGson().fromJson(json, classOfT);
  47. }
  48. /**
  49. * 从请求体中读取客户端发送的JSON串
  50. *
  51. * @param stream
  52. * 输入流
  53. * @return String 类型,接收到的JSON串
  54. */
  55. public static String readStringFromRequestBody(InputStream stream) {
  56. StringBuffer sb = new StringBuffer();
  57. char[] buf = new char[ 2048];
  58. int len = - 1;
  59. try {
  60. InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
  61. while ((len = reader.read(buf)) != - 1) {
  62. sb.append( new String(buf, 0, len));
  63. }
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. } finally {
  67. if (stream != null) {
  68. try {
  69. stream.close();
  70. } catch (IOException e) {
  71. }
  72. }
  73. }
  74. return sb.toString();
  75. }
  76. /**
  77. * 回写响应
  78. * @param json
  79. * @param response
  80. */
  81. public static void writeString(String json, HttpServletResponse response) {
  82. ServletOutputStream os = null;
  83. try {
  84. os = response.getOutputStream();
  85. os.write(json.getBytes( "UTF-8"));
  86. } catch (UnsupportedEncodingException e) {
  87. e.printStackTrace();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. } finally{
  91. if( null != os){
  92. try {
  93. os.close();
  94. } catch (IOException e) {
  95. }
  96. }
  97. }
  98. }
  99. }

ArticleMapper 

主要定义两个方法,一个新增文章,另一个通过id号查询文章数据。 


  
  1. public interface ArticleMapper {
  2. public int addArticle(Article article);
  3. public Article getArticleById(Integer id);
  4. }

 

ArticleService 


  
  1. @Service
  2. public class ArticleService {
  3. @Autowired
  4. ArticleMapper articleMapper;
  5. public int addArticle(Article article){
  6. return articleMapper.addArticle(article);
  7. }
  8. public Article getArticleById(Integer id){
  9. return articleMapper.getArticleById(id);
  10. }
  11. }

 

ArticleController

 调用JsonUtil类将字符串转换为Article类,通过set方法进行传参,根据Service的返回结果进行判断,i>0就插入成功,后台打印信息,数据库的数据也插入成功。


  
  1. @RequestMapping("/addContent")
  2. @ResponseBody
  3. public Result addContent(String param, String content, String description) throws UnsupportedEncodingException {
  4. String param1 = URLDecoder.decode(param, "utf-8");
  5. String content1 = URLDecoder.decode(content, "utf-8");
  6. String description1 = URLDecoder.decode(description, "utf-8");
  7. //将String字符串解析为对象
  8. Article article = JsonUtil.fromJson(param1, Article.class);
  9. article.setContent(content1);
  10. article.setDescription(description1);
  11. article.setStatus( 0);
  12. article.setCreateTime( new Date());
  13. article.setAuthor( "杜甫");
  14. System.out.println(article);
  15. int i = articleService.addArticle(article);
  16. if(i> 0){
  17. return new Result( "success", "处理成功");
  18. }
  19. return new Result( "fail", "处理失败");
  20. }

 ArticleMapper.xml

映射文件中进行ResultMap的配置以及SQL语句的书写,注意配置过程中每一个属性映射的时候要保证前后端名称一致,否则就会报错。


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.tulun.dao.ArticleMapper">
  6. <resultMap id="articleMap" type="com.tulun.model.Article">
  7. <result column="id" property="id"> </result>
  8. <result column="categoryId" property="categoryId"> </result>
  9. <result column="title" property="title"> </result>
  10. <result column="content" property="content"> </result>
  11. <result column="description" property="description"> </result>
  12. <result column="status" property="status"> </result>
  13. <result column="author" property="author"> </result>
  14. <result column="createTime" property="createTime"> </result>
  15. <result column="showCount" property="showCount"> </result>
  16. <result column="content" property="content" typeHandler="com.tulun.util.ConvertBlobTypeHandler"> </result>
  17. </resultMap>
  18. <insert id="addArticle" parameterType="com.tulun.model.Article">
  19. insert into t_article (categoryId,title,content,description,status,author,createTime)
  20. values (#{categoryId},#{title},#{content},#{description},#{status},#{author},#{createTime})
  21. </insert>
  22. <select id="getArticleById" parameterType="int" resultMap="articleMap">
  23. select * from t_article where id = #{id}
  24. </select>
  25. </mapper>

部署至服务器 

 

博客内容展示

内容展示的jsp如下,分别获取到博客的名称,作者,内容,分类等等信息,其中要注意的是发表时间以及分类,因为通过Date获取的时间并不是一个格式化时间,需要对它进行转换,另一个是分类从数据库读取的是分类号,而不是类名,同时也要做相应的转换。

article_detail.jsp


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
  5. <%
  6. String path = request.getContextPath();
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  8. %>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html> <head>
  11. <link rel="stylesheet" id="main-css" href="<%=basePath%>css/demo/style.css" type="text/css" media="all">
  12. <link href="<%=basePath%>js/ueditor1_4_3/third-party/SyntaxHighlighter/shCoreDefault.min.css" rel="stylesheet" type="text/css" />
  13. <meta charset="UTF-8">
  14. <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8">
  15. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
  16. <meta name="baidu-site-verification" content="emEdenaBVA">
  17. <meta http-equiv="Cache-Control" content="no-siteapp">
  18. <title> 博客系统 </title>
  19. </head>
  20. <body id="contain" class="home blog ui-c3">
  21. <section class="container">
  22. <header class="header">
  23. <div class="logo_right"> <span class="glyphicon glyphicon-search degfy_search"> </span> </div>
  24. <div class="logo_left"> </div>
  25. <h1 class="logo"> <a href="">博客系统 </a> </h1>
  26. <div class="widget_head"> </div>
  27. </header>
  28. <div class="content-wrap">
  29. <div class="content">
  30. <header class="article-header">
  31. <h1 class="article-title">${article.title} </h1>
  32. <ul class="article-meta">
  33. <li>作者:${article.author} &nbsp; &nbsp; &nbsp; &nbsp;发布于:${article.time} </li>
  34. <li>分类: <a rel="category tag" data-original-title="" title="">${article.categoryName} </a> </li>
  35. </ul>
  36. </header>
  37. <article class="article-content">
  38. ${article.content}
  39. </article>
  40. <div class="article-tags">
  41. 标签:
  42. <c:forEach items="${article.id}" var="tag">
  43. <a href="javascript:void(0)" rel="tag" data-original-title="" title="">${tag.tagName} </a>
  44. </c:forEach>
  45. </div>
  46. </div>
  47. </section>
  48. <div id="back_hidden"> </div>
  49. <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"> </script>
  50. <script src="//cdn.bootcss.com/jquery.lazyload/1.9.1/jquery.lazyload.js"> </script>
  51. </body>
  52. </html>

时间格式化

在Article的POJO类中新增time属性,通过SimpleDateFormat类格式化时间,然后通过get方法进行返回。


  
  1. public String getTime() {
  2. SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  3. return simpleDateFormat.format(createTime);
  4. }

 分类ID与类名转换

在Article的POJO类中新增categoryName属性,通过数据库的联合查询来返回一个categoryName。


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.tulun.dao.ArticleMapper">
  6. <resultMap id="articleMap" type="com.tulun.model.Article">
  7. <result column="id" property="id"> </result>
  8. <result column="categoryId" property="categoryId"> </result>
  9. <result column="title" property="title"> </result>
  10. <result column="content" property="content"> </result>
  11. <result column="description" property="description"> </result>
  12. <result column="status" property="status"> </result>
  13. <result column="author" property="author"> </result>
  14. <result column="createTime" property="createTime"> </result>
  15. <result column="categoryName" property="categoryName"> </result>
  16. <result column="showCount" property="showCount"> </result>
  17. <result column="content" property="content" typeHandler="com.tulun.util.ConvertBlobTypeHandler"> </result>
  18. </resultMap>
  19. <insert id="addArticle" parameterType="com.tulun.model.Article">
  20. insert into t_article (categoryId,title,content,description,status,author,createTime)
  21. values (#{categoryId},#{title},#{content},#{description},#{status},#{author},#{createTime})
  22. </insert>
  23. <select id="getArticleById" parameterType="int" resultMap="articleMap">
  24. select a.*,c.categoryName categoryName from t_article a, t_category c where a.categoryId = c.id and a.id = #{id}
  25. </select>
  26. </mapper>

 最后部署至服务器可以看见初步效果

报错汇总

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

 原因是POJO类的与mapper.xml中select标签上的id名不一致导致的


转载:https://blog.csdn.net/qq_41573860/article/details/116168392
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场