$.ajax提交数组到SpringMVC

前台数据提交

$.ajax({
                'type': 'POST',
                'url':"${pageContext.request.contextPath}/web/${webId}/menu/saveList",
                'contentType': 'application/json',
                'data': JSON.stringify(menu.menuList),
                'dataType': 'json',
                success: function(data) {

                }
            });

后台数据接收

    @PostMapping("/saveList")
    @ResponseBody
    public ResponseDTO saveList(@RequestBody List<MenuDto> menuDtoList) {
        menuService.saveList(menuDtoList);
        return response(menuDtoList.size());
    }

@RequestBody会通过jackson将json字符串转化成java对象

数据提交

http://xhope.top/wp-content/uploads/2017/08/1.png

参考链接
https://stackoverflow.com/questions/14459171/spring-mvc-and-jquery-post-request-with-array-of-pojo

MySQL的root密码找回

1 . 查找my.conf位置

[root@iZ28t57xzbcZ ~]# whereis my.cnf
my: /etc/my.cnf

2 . 修改my.conf文件,新增skip-grant-tables

[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-grant-tables

3 . 重启mysql

systemctl restart mariadb

4 . 登陆修改root密码

[root@localhost log]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.23-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql
Database changed
mysql> update user set password = password('root') where user = 'root';
Query OK, 4 rows affected (0.91 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

mysql> quit
Bye

5 . 删除之前my.conf文件中新增的skip-grant-tables

6 . 重启DB,登陆正常

7 . 参数skip-grant-tables说明
这个参数看名字就是skip权限用的,通常用来找回root密码用,相对危险,慎用,特别是在生产上

参考:
https://my.oschina.net/Kenyon/blog/87632

Nodejs + Express + EJS搭建带有母版的网站

通过nodejs express ejs ejs-mate 搭建具有母版页面的网站页面,省略nodejs的安装过程。

Express

  • 安装Express
$ npm install express --save
  • Express 应用生成器搭建网站
$ npm install express-generator -g
$ express myapp
$ cd myapp 
$ npm install
$ DEBUG=myapp npm start

后在浏览器中打开 http://localhost:3000/ 网址就可以看到这个应用了。注意这时默认的模版引擎是jade。需要做相应的修改才能支持ejs
通过 Express 应用生成器创建的应用一般都有如下目录结构:

.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│   └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
├── error.jade
├── index.jade
└── layout.jade

7 directories, 9 files

EJS & ejs-mate

  • 安装
$ npm install ejs
$ npm install ejs-mate --save
  • 修改文件app.js,使用母版引擎ejs
var engine = require('ejs-mate');
app.engine('ejs', engine);
app.set('view engine', 'ejs');
  • 添加模版文件layout.ejs
<!DOCTYPE html>
<html>
  <head>
    <title>It's <%= who %></title>
  </head>
  <body>
    <section>
      <%- body -%>
    </section>
  </body>
</html>
  • index.ejs使用母版文件
<% layout('layout') -%>
<h1>I am the <%= what %> template</h1>
  • Express4.0 渲染
var express = require('express'),
  engine = require('ejs-mate'),
  app = express();

// use ejs-locals for all ejs templates:
app.engine('ejs', engine);

app.set('views',__dirname + '/views');
app.set('view engine', 'ejs'); // so you can render('index')

// render 'index' into 'boilerplate':
app.get('/',function(req,res,next){
  res.render('index', { what: 'best', who: 'me' });
});

app.listen(3000);

渲染后将会得到代码

<!DOCTYPE html>
<html>
  <head>
    <title>It's me</title>
  </head>
  <body>
    <section>
      <h1>I am the best template</h1>
    </section>
  </body>
</html>

参考链接
www.expressjs.com.cn/
http://www.embeddedjs.com/
https://github.com/JacksonTian/ejs-mate

富文本编辑器CKEditor使用

1. 下载

下载地址: http://ckeditor.com/download

下载分为2种

  • 直接下载

    • Basic Package
    • Standard Package(默认)
    • Full Package
  • 手动配置

    • Customize

我觉得手动配置插件Enhanced Image,上传图片是非常实用的。

2. 学习使用编辑器

文件下载完成后,打开路径samples/index,查看demo

  • 学习CKEditor使用
CKEDITOR.editorConfig = function( config ) {

    config.toolbarGroups = [
                            { name: 'document', groups: [ 'mode', 'document', 'doctools' ] },
                            { name: 'clipboard', groups: [ 'clipboard', 'undo' ] },
                            { name: 'editing', groups: [ 'find', 'selection', 'spellchecker', 'editing' ] },
                            { name: 'forms', groups: [ 'forms' ] },
                            { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
                            { name: 'paragraph', groups: [ 'list', 'indent', 'blocks', 'align', 'bidi', 'paragraph' ] },
                            { name: 'styles', groups: [ 'styles' ] },
                            { name: 'colors', groups: [ 'colors' ] },
                            { name: 'insert', groups: [ 'insert' ] },
                            { name: 'links', groups: [ 'links' ] },
                            { name: 'tools', groups: [ 'tools' ] },
                            { name: 'others', groups: [ 'others' ] },
                            { name: 'about', groups: [ 'about' ] }
                        ];

                        config.removeButtons = 'Save,NewPage,Cut,Copy,Find,Replace,SelectAll,Form,Checkbox,Radio,TextField,Textarea,Select,Button,HiddenField,About,ShowBlocks,Language,Flash,Iframe,Scayt,Superscript,Subscript';


    config.filebrowserImageUploadUrl= ctx+"ckeditor/uploadImg"; //待会要上传的action或servlet
};

页面

<textarea id="contentForm-content" name="content"></textarea>

样式和事件

CKEDITOR.replace('contentForm-content', {
            height : 200
        });


        CKEDITOR.instances["contentForm-content"].on("instanceReady", function () {  
            //set keyup event  
            this.document.on("keyup", function() {


            });  

        });  
  • TOOLBAR CONFIGURATOR,右上角点击可以配置显示

3. 自定义插件

如果添加文件上传插件,插件的名字叫file
目录结构如下:
http://xhope.top/wp-content/uploads/2017/03/2.png

a. 创建目录 ckeditor\plugins\file
b. 创建文件plugin.js

/**
 * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or http://ckeditor.com/license
 */

/**
 * @fileOverview File plugin
 */

( function() {
    CKEDITOR.plugins.add('file',  
            {         
                requires : ['dialog'],  
                icons: 'file', // %REMOVE_LINE_CORE%
                init : function (editor)  
                {  
                    var pluginName = 'file';  

                    //加载自定义窗口,就是dialogs前面的那个/让我纠结了很长时间  
                    CKEDITOR.dialog.add(pluginName,this.path + "/dialogs/file.js");  

                    //给自定义插件注册一个调用命令  
                    editor.addCommand( pluginName, new CKEDITOR.dialogCommand( pluginName ) );  
                    //注册一个按钮,来调用自定义插件  
                    editor.ui.addButton('File',  
                            {  
                                //editor.lang.mine是我在zh-cn.js中定义的一个中文项,  
                                //这里可以直接写英文字符,不过要想显示中文就得修改zh-cn.js  
                                label : '上传附件',  
                                command : pluginName,
                                toolbar:"insert,25" 
                            });  
                }  
            }  
        );  

} )();

c. 创建文件file.js

( function() {
    CKEDITOR.dialog.add( 'file', function( editor )  
            {  
                return {   
                    title : '附件上传',  
                    minWidth : 600,  
                    minHeight : 200,  
                    contents : [  
                        {  
                            elements :  
                            [  
                                {  
                                    type : 'html',
                                    html : '<input type="file" id="upload" name="upload" multiple/><div id="fileList" style="padding:2px;height:200px; border:1px solid #BCBCBC; margin-top:5px; overflow:auto;">\
                                         </div>'
                                }  
                            ]  
                        }  
                    ],
                    onLoad:function() {
                        $('#upload').fileupload({
                            url: ""+ctx+"ckeditor/uploadFile",
                            dataType: 'json',
                            done: function (e, data) {
                                $.each(data.result, function (index, file) {
                                    var a = "<span><img style='position:relative;top:3px;' src=\""+ctx+"static/images/"+common.getImage(1,file.name)+"\"/><a style='margin-left:2px;' href=\"javascript:void(0)\" onclick=\"common.postSubmit('"+(file.downloadUrl+"&fileName="+file.name+"")+"');\">"+file.name+"</a>&nbsp;<span style='font-weight:normal;color:#BCBCBC;font-size:11px;'>"+common.getFileSize(file.size)+"</span>" +
                                            "\<a class='delClass' style='color:#3D5E86;margin-left:5px;' href='javascript:void(0);' onclick=\"$(this).parent().remove();\">删除</a></span><br/>";
                                    $("#fileList").append(a);
                                }); 
                            } 
                        }).prop('disabled', !$.support.fileInput)
                            .parent().addClass($.support.fileInput ? undefined : 'disabled');

                        $("#fileList").niceScroll({cursorcolor:"#BCBCBC"}); // First scrollable DIV
                    },
                    onShow:function() {
                         $('#upload').val('');  
                         $("#fileList").html('');
                    },
                    onOk: function() {
                        $("#fileList a.delClass").remove();
                        $("#fileList a.delClass").next().remove();
                        editor.insertHtml($("#fileList").html());
                    }
                };  
            } );  
} )();

d. icons目录下放入图标file.png
e. 使用插件

//自定义插件,上传附件
    config.extraPlugins = 'file';

使用效果
http://xhope.top/wp-content/uploads/2017/03/1.png

4.注意点

你保存正文后显示,如果你在设计页面的时候用了重置样式,那么富文本编辑器内容显示会受到影响。要么取消重置样式,要么单独将重置的样式还原回来。我非常痛苦地选择了后者。

nginx 80端口根据不同域名转发到不同端口

现在有一个外网服务器地址:121.42.151.190

绑定两个二级域名:

  • botao.xhope.top
  • richtech.xhop.top

这两个程序部署的端口为8080 8081
如果不使用nginx,可能需要这么访问:

问题来了,如何才能做到访问,即使不显示地加端口,也能寻找。

按照如下配置

1.安装nginx,windows下载地址http://nginx.org/en/download.html
2.配置nginx.conf

server {
        listen       80;
        server_name  botao.xhope.top;
        location / {
            proxy_pass   http://127.0.0.1:8080/;
        }
    }

server {
        listen       80;
        server_name  richtech.xhope.top;
        location / {
            proxy_pass   http://127.0.0.1:8081/;
        }
    }

3.运行nginx

nginx

4.关闭nginx

    nginx -s stop