Sam's Notes | Sam Blog

梦想还是要有的,万一实现了呢

0%

Spring MVC 的配置 Configuring Spring MVC

* 开启Spring MVC默认配置

Java Config: 加入 @EnableWebMvc 到 某个有 @Configuration 注解的类头。

1
2
3
4
5
@Configuration
@EnableWebMvc
public class WebConfig {

}

XML方式: 增加mvc:annotation-driven 到 DispatcherServlet 的context里面。

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven />

</beans>

* 自定义Spring MVC配置

Java Config: 加入 @EnableWebMvc 到 某个有 @Configuration 注解的类头。自己实现 WebMvcConfigurer, 更多是继承 WebMvcConfigurerAdapter并重写你自己需要的方法。

1
2
3
4
5
6
7
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

// Override configuration methods...

}

XML方式: 增加mvc:annotation-driven 到 DispatcherServlet 的context里面。设置自己需要Spring MVC XML schema里面的元素值。

* 高级自定义Spring MVC配置

Java Config: 去掉 @EnableWebMvc@Configuration 注解类头,自己实现 WebMvcConfigurationSupport., 更多是继承 DelegatingWebMvcConfiguration并重写你自己需要的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Configuration
public class WebConfig extends DelegatingWebMvcConfiguration {

@Override
public void addInterceptors(InterceptorRegistry registry){
// ...
}

@Override
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
// Create or let "super" create the adapter
// Then customize one of its properties
}

}

具体内容看头部的官方文档

springmvc一

@RequestMapping URI

URI Template Patterns

spring doc

如何匹配这个url "/spring-web/spring-web-3.0.5.jar"

@RequestMapping 支持正则表达式, 语法就是 {varName:regex}

比如上面那个url就可以这样

1
2
3
4
5
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
public void handle(@PathVariable String version, @PathVariable String extension) {
// ...
}
}

Path Pattern Comparison

多重匹配优先规则:

  • 越少数量URI variables 和 wild cards 的表达式越优先;例如/hotels/{hotel}/*/hotels/{hotel}/** 优先
  • 数量一致,越长的表达式越优先;例如/foo/bar*/foo/* 优先
  • 数量和长度一致, 越少wild cards的表达式越优先;例如/hotels/{hotel}/hotels/* 优先
  • 例外的 /api/{a}/{b}/{c/** 优先

Path Pattern Matching By Suffix

默认Spring MVC会自动增加后缀 ".*" 来匹配URI。比如 /person 会自动映射成 /person.* , 可以自动匹配文件类型,如/person.pdf, /person.xml,不能匹配 /person.com
.com 不是文件类型。

Matrix Variables

需要修改下默认配置 ,,,,就可以使用 “Matrix URIs”

1
2
3
4
5
6
7
8
9
// GET /pets/42;q=11;r=22

@RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET)
public void findPet(@PathVariable String petId, @MatrixVariable int q) {

// petId == 42
// q == 11

}
1
2
3
4
5
6
7
8
9
10
11
// GET /owners/42;q=11/pets/21;q=22

@RequestMapping(path = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
public void findPet(
@MatrixVariable(name="q", pathVar="ownerId") int q1,
@MatrixVariable(name="q", pathVar="petId") int q2) {

// q1 == 11
// q2 == 22

}
1
2
3
4
5
6
7
8
// GET /pets/42

@RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET)
public void findPet(@MatrixVariable(required=false, defaultValue="1") int q) {

// q == 1

}
1
2
3
4
5
6
7
8
9
10
11
// GET /owners/42;q=11;r=12/pets/21;q=22;s=23

@RequestMapping(path = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
public void findPet(
@MatrixVariable Map<String, String> matrixVars,
@MatrixVariable(pathVar="petId"") Map<String, String> petMatrixVars) {

// matrixVars: ["q" : [11,22], "r" : 12, "s" : 23]
// petMatrixVars: ["q" : 11, "s" : 23]

}

BindingResult and @ModelAttribute

BindingResult必须直接跟在@ModelAttribute后面

1
2
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, Model model) { ... }

各种参数绑定

@PathVariable

  • 绑定URI的参数,若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable(“name”)指定uri template中的名称。

@RequestHeader

  • 可以把Request请求header部分的值绑定到方法的参数
    1
    2
    3
    4
    5
    6
    7
    @RequestMapping("/displayHeaderInfo.do")  
    public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
    @RequestHeader("Keep-Alive") long keepAlive) {

    //...

    }

@CookieValue

  • 把Request header中关于cookie的值绑定到方法的参数

@RequestParam

  • 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String–> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;
  • 用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;
  • 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定

@RequestBody

  • 该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;
  • 它是通过使用HandlerAdapter 配置的HttpMessageConverters来解析post data body,然后绑定到相应的bean上的。
  • 因为配置有FormHttpMessageConverter,所以也可以用来处理 application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap<String, String>里,这种情况在某些特殊需求下使用,详情查看FormHttpMessageConverter api;

@SessionAttributes

  • 绑定HttpSession中的attribute对象的值

@ModelAttribute

该注解有两个用法,一个是用于方法上,一个是用于参数上;
用于方法上时: 通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model;
用于参数上时: 用来通过名称对应,把相应名称的值绑定到注解的参数bean上;要绑定的值来源于:

  • @SessionAttributes 启用的attribute 对象上;
  • @ModelAttribute 用于方法上时指定的model对象;
  • 上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean中。

HttpPutFormContentFilter

支持PUT方法

@InitBinder

在controller里面配置web数据绑定

1
2
3
4
5
6
7
8
9
10
11
12
@Controller
public class MyFormController {

@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

// ...
}

最新的Spring 4.2 里面

1
2
3
4
5
6
7
8
9
10
@Controller
public class MyFormController {

@InitBinder
public void initBinder(WebDataBinder binder) {
binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
}

// ...
}

@ControllerAdvice

被注解的类可以搭配 @ExceptionHandler, @InitBinder, and @ModelAttribute,全局处理

Resolving views

Spring Doc : Resolving views

Spring’s multipart (file upload) support

Handling exceptions

  • HandlerExceptionResolver
  • @ExceptionHandler

Standard Spring MVC Exceptions

主要内容

jenkins-2.235.3-1.1 安装 使用

更新历史
2020-08-04 增加jenkins的清华镜像设置
2020-11-14 国内镜像源设置

阅读全文 »

和开发比运维,和运维比开发

服务器监控

命令

  • 查看整体空间 (-h 按M单位)

    1
    $ df -h
  • 查看输出当前目录下各个子目录所使用的空间(-h 按M单位)

    1
    $ du -h --max-depth=1

排序

1
$ du  --max-depth=1 |sort -rn
  • 手动清空缓存

    1
    2
    $ sync
    $ echo 3 > /proc/sys/vm/drop_caches
  • 查找以前用过的命令
    ctrl + r

  • Netstat
    Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。
    Linux netstat命令详解 链接
    程序端口占用情况

    1
    2
    3
    4
    5
    $ netstat –apn 

    $ netstat -anp | grep portno

    $ ps -aux | grep java
  • 抓包 tcpdump / wireshark

  • rsync 增量备份,保持原有属性
    -a
    -z
    -p
    –delete

  • inotify + rsync 实时备份
    inotify 安装

inotifywait
-e 监控事件
-m 持续监控
-r 递归
-q 简化信息

  • ctrl + r 查找历史命令

  • rs 上传

  • waf : web application firewall
    modsecurity

  • Linux下高效数据恢复软件extundelete

  • curl wget lynx
    curl

    1
    2
    3
    4
    $ curl -I ip/domain #查看web服务器类型和版本
    $ wget -C URL #断点续传 -t 重试次数 -T 超时时间
    $ wget --user username --password pass URL #认证
    $ curl -C URL #断点续传

常用技巧

特殊变量

变量 含义
$0 当前脚本的文件名
$n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。
!$ 上个命令最后一个参数
$# 传递给脚本或函数的参数个数。
$* 传递给脚本或函数的所有参数。
$@ 传递给脚本或函数的所有参数。被双引号(“ “)包含时,与 $* 稍有不同,下面将会讲到。
$? 上个命令的退出状态,或函数的返回值。
$$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。
$! 执行上一个后台程序的PID
$*$@ 都表示传递给函数或脚本的所有参数,不被双引号(“ “)包含时,都以”$1” “$2” … “$n” 的形式输出所有参数。
但是当它们被双引号(“ “)包含时,$* 会将所有的参数作为一个整体,以”$1 $2 … $n”的形式输出所有参数;$@ 会将各个参数分开,以”$1” “$2” … “$n” 的形式输出所有参数。

常用PATTERN:

`^#`:   以#开头
`#$`:   以#结尾
`^$`:   空行

知乎

基础知识

最小存储单位

名称 最小存储单位
硬盘 扇区(512B)
文件系统 block(1K,4K)
RAID chunk (512) mdadm -c
LVM PE (16M 自定义)

调教 kaffeine 播放各类媒体格式

kaffeine 是基于 libxine2, openSUSE 官方提供的 libxine2 缺少一些插件,那用户要怎么安装这些编解码器呢? 我们先说安装 mkv, mp4, wmv, m4v, mov, 3gp, 3g2 等视频文件的编解码器,这些可以很方便的从 packman 安装。核心的包是 libxine2-codecs ,我以 openSUSE 13.2 为例进行讲解:

1
2
3
sudo zypper ar -f http://packman.inode.at/suse/openSUSE_13.2/ packman
sudo zypper update
sudo zypper install libxine2-codecs

安全相关

last
lastb
lastlog
history
/var/log/secure : 登录的ssh公钥签名可以通过 ssh-keygen -l -f xxx.pub 获得 远程日志服务

网络实时流量监测 iftop

单服务器

网络流量监控与分析 Ntop 和 Ntopng

网络性能评估 iperf

rootkit 检测工具

  • chkrootkit
  • RKHunter

7月5日左右搭建成功的,算比较新鲜吧:) 记录一下操作步骤,感谢前人,希望能帮到其他人。主要涉及github,hexo,多说。
github: 我想你懂得,万一你不懂,百度可以帮助你;
hexo : 博客博客模板框架 - 基于nodejs
多说 : 一个现成的评论模块,据说是国内最好的

下面每个讲讲,nodejs和git不说了。

github

首先你得有一个github的账户, ssh链接也配置好。

GitHub Pages 建立博客

GitHub Pages分两种,一种是你的GitHub用户名建立的username.github.io这样的用户&组织页(站),另一种是依附项目的pages。
建立个人的博客使用第一种,像 sam2099.github.io 。
GitHub Pages

hexo

安装Hexo

1
$ npm install -g hexo-cli

初始化

找个干净好找的地方建个文件夹hexo,然后执行

1
$ hexo init

hexo会自动建好目录和文件。

看看初始效果

在当前目录(hexo)运行,然后到浏览器输入localhost:4000看看。

1
2
$ hexo g
$ hexo s

哈哈,正常的话,可以看到了。

主题

目前来说 NexT 这个主题最符合我的口味。

  • 安装
    1
    $ git clone https://github.com/theme-next/hexo-theme-next themes/next
  • 主题配置
    themes/next目录的 _config.yml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    # ---------------------------------------------------------------
    # Scheme Settings
    # ---------------------------------------------------------------

    # Schemes
    #scheme: Muse
    #scheme: Mist
    scheme: Pisces
    #scheme: Gemini

    # Dark Mode
    darkmode: false

    # 菜单
    menu:
    home: / || fa fa-home
    #about: /about/ || fa fa-user
    tags: /tags/ || fa fa-tags
    categories: /categories/ || fa fa-th
    archives: /archives/ || fa fa-archive
    #schedule: /schedule/ || fa fa-calendar
    sitemap: /sitemap.xml || fa fa-sitemap
    #commonweal: /404/ || fa fa-heartbeat
    RSS: /atom.xml || fa fa-rss

    # 代码
    codeblock:
    # Code Highlight theme
    # Available values: normal | night | night eighties | night blue | night bright | solarized | solarized dark | galactic
    # See: https://github.com/chriskempson/tomorrow-theme
    highlight_theme: night eighties
    # Add copy button on codeblock
    copy_button:
    enable: true
    # Show text copy result.
    show_result: default
    # Available values: default | flat | mac
    style:

    # Local Search
    # Dependencies: https://github.com/theme-next/hexo-generator-searchdb
    local_search:
    enable: true


    favicon:
    small: /favicon.ico
    medium: /favicon.ico
    apple_touch_icon: /favicon.ico
    safari_pinned_tab: /favicon.ico
    #android_manifest: /images/manifest.json
    #ms_browserconfig: /images/browserconfig.xml
  • hexo 配置
    Hexo目录下的config.yml配置文件中的theme属性,将其设置为 next
    1
    theme: next

Quick Start

下面是hexo init 时生成的操作命令,够用了。
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

hexo 设置

根据自己爱好来对Hexo生成的网站进行设置了,对整站的设置,只要修改项目目录的_config.yml就可以了。
注意空格, 冒号后面一定跟一个空格;层级关系也需要空格。
我所有的 博客源文件 可以和blog对照着看。
主要设置的地方:

1
2
3
4
Site
plugins
theme
deploy

插件

RSS

安装

1
$ npm install hexo-generator-feed   --save   

配置
参考主题的 menu

sitemap

安装

1
$ npm install hexo-generator-sitemap   --save   

配置
参考主题的 menu

hexo-util

Next主题需要
安装

        npm install hexo-util  --save
        

github pages

上传到 github pages 需要
安装

    npm install hexo-deployer-git --save
    

搜索

NexT 主题自带了一个搜索功能 Local Search,即在编译文件时本地生成一个数据库,放在网站根目录下,用户借助此数据库进行搜索查询。 安装:

1
npm install hexo-generator-searchdb --save

配置
参考主题的 local_search

域名映射

在source根路径下,创建文件CNAME(无后缀,大写);内容就一行,是你需要映射的域名,比如我的

1
giveme5.top

图片缩放

启用NexT内嵌的图像缩放插件,以允许放大图像。NexT提供了两个插件:fanncybox和mediumzoom,前者功能更加强大,而后者更加轻巧所以网页加载更快,按需选择。
修改主题配置文件,如fancybox: true即可, 不要2个同时为true。

藏在最后

这是第一篇,我想这个作为笔记,有需要就可以拿出来看看,省得记载纸上找不到;所以这个大概是给自己看的。