john's tech blog

hope is coming


  • 首页

  • 标签

  • 归档

slick_plainSQL

发表于 2017-05-27 | 更新于 2019-05-07

slick3.1.1 使用sql语句查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
val db  = Database.forURL("jdbc:mysql://ip:port/db?useUnicode=true",
driver = "com.mysql.jdbc.Driver",
user="root",
password="root")

def insertInvoice(): DBIO[Int] =
sqlu"insert invoicelog values('sss','2017-05-26 12:00:00','sasas')"

val b:Future[Int] = db.run(insertInvoice())
Await.result(b, Duration.Inf)

implicit val getSupplierResult = GetResult(r => InvoiceLog(r.nextString(), r.nextString, r.nextString))
def selectInvoice(): DBIO[Seq[InvoiceLog]] =
sql"select * from invoicelog".as[InvoiceLog]

var a: Future[Seq[InvoiceLog]] = db.run(selectInvoice)
val s = Await.result(a, Duration.Inf)

tomcat_errorpage

发表于 2017-05-12 | 更新于 2019-05-07

scalatra 自定义404文件

在scalatra中,如果访问servlet不存在的路由时,默认为返回
Requesting "%s %s" on servlet "%s" but only have: %s

那么我就去源码看了看,发现如下代码:

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
53
54
55
protected var doNotFound: Action = () => {
serveStaticResource() getOrElse resourceNotFound()
}

/**
* Attempts to find a static resource matching the request path. Override
* to return None to stop this.
*/

protected def serveStaticResource(): Option[Any] =

servletContext.resource(request) map { _ =>
servletContext.getNamedDispatcher("default").forward(request, response)
}

/**
* Called by default notFound if no routes matched and no static resource
* could be found.
*/

protected def resourceNotFound(): Any = {
response.setStatus(404)
if (isDevelopmentMode) {
val error = "Requesting \"%s %s\" on servlet \"%s\" but only have: %s"
response.getWriter println error.format(
request.getMethod,
Option(request.getPathInfo) getOrElse "/",
request.getServletPath,
routes.entryPoints.mkString("<ul><li>", "</li><li>", "</li></ul>"))
}
}protected var doNotFound: Action = () => {
serveStaticResource() getOrElse resourceNotFound()
}

/**
* Attempts to find a static resource matching the request path. Override
* to return None to stop this.
*/

protected def serveStaticResource(): Option[Any] =

servletContext.resource(request) map { _ =>
servletContext.getNamedDispatcher("default").forward(request, response)
}

/**
* Called by default notFound if no routes matched and no static resource
* could be found.
*/

protected def resourceNotFound(): Any = {
response.setStatus(404)
if (isDevelopmentMode) {
val error = "Requesting \"%s %s\" on servlet \"%s\" but only have: %s"
response.getWriter println error.format(
request.getMethod,
Option(request.getPathInfo) getOrElse "/",
request.getServletPath,
routes.entryPoints.mkString("<ul><li>", "</li><li>", "</li></ul>"))
}
}

发现它如果没找到路由时,会尝试通过defaultServlet找静态资源,如果还找不到那么就会进入resourceNotFound方法,

如果是开发模式下,那么就会返回一开始提到的内容。。

如果我们通过在web.xml中配置:

1
2
3
4
<context-param>
<param-name>org.scalatra.environment</param-name>
<param-value>production</param-value>
</context-param>

就不会显示配置的404页面,那是怎么回事呢?

我们来看看response.setStatus(404)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Sets the status code for this response.
*
* <p>This method is used to set the return status code when there is
* no error (for example, for the SC_OK or SC_MOVED_TEMPORARILY status
* codes).
*
* <p>If this method is used to set an error code, then the container's
* error page mechanism will not be triggered. If there is an error and
* the caller wishes to invoke an error page defined in the web
* application, then {@link #sendError} must be used instead.
*
* <p>This method preserves any cookies and other response headers.
*
* <p>Valid status codes are those in the 2XX, 3XX, 4XX, and 5XX ranges.
* Other status codes are treated as container specific.
*
* @param sc the status code
*
* @see #sendError
*/

public void setStatus(int sc);

有这么段话:
If this method is used to set an error code, then the container’s

  • error page mechanism(机制) will not be triggered.

参考资料:
Default Servlet
Tomcat use DefaultServlet for static content in external directory
Tomcat处理静态文件DefaultServlet分析
Configuration
Configuring Web Application Components

angular_interpolate

发表于 2017-05-05 | 更新于 2019-05-07

angular 插值

angular interpolate

1
2
3
4
5
6
7
8
9
// Concatenating(连接) expressions makes it hard to reason about whether some combination of
// concatenated values are unsafe to use and could easily lead to XSS. By requiring that a
// single expression be used for iframe[src], object[src], etc., we ensure that the value
// that's used is assigned or constructed by some JS code somewhere that is more testable or
// make it obvious that you bound the value to some user controlled value. This helps reduce
// the load when auditing for XSS issues.
if (trustedContext && concat.length > 1) {
$interpolateMinErr.throwNoconcat(text);
}

什么时候报错呢?

像ng-src=”img/“就会报错了

1
2
3
4
5
6
7
8
9
10
11
12
var $interpolateMinErr = angular.$interpolateMinErr = minErr('$interpolate');
//https://segmentfault.com/q/1010000007677889?_ea=1423254
$interpolateMinErr.throwNoconcat = function(text) {
throw $interpolateMinErr('noconcat',
"Error while interpolating: {0}\nStrict Contextual Escaping disallows " +
"interpolations that concatenate multiple expressions when a trusted value is " +
"required. See http://docs.angularjs.org/api/ng.$sce", text);
};

$interpolateMinErr.interr = function(text, err) {
return $interpolateMinErr('interr', "Can't interpolate: {0}\n{1}", text, err.toString());
};

参考资料:
AugularJS通过服务器请求图片时总是报错 $interpolate:noconcat
浅谈AngularJS的$interpolate服务 1

javascript_base1

发表于 2017-05-02 | 更新于 2019-05-07

javascript基础

行内元素 onclick 和 jquery 绑定的 onclick 有什么区别?

HTML或原生js是单一对应绑定的,绑多了只留最后一个。jQuery是追加绑定的,绑多少执行多少。

参考资料:
在javascript或者jQuery中绑定按钮点击事件,和在HTML 标签中直接调用onclick属性有什么区别?](https://segmentfault.com/q/1010000000332625)
从国企到阿里的面试经历(一)

angular_directive1

发表于 2017-04-24 | 更新于 2019-05-07

angular 指令多个元素multiElement

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
// iterate over the attributes
//遍历所有属性 比如ng-app ng-controller
for (var attr, name, nName, ngAttrName, value, isNgAttr, nAttrs = node.attributes,
j = 0, jj = nAttrs && nAttrs.length; j < jj; j++) {
var attrStartName = false;
var attrEndName = false;

attr = nAttrs[j];//
name = attr.name;
value = trim(attr.value);

// support ngAttr attribute binding
//转换成ngController 这种
ngAttrName = directiveNormalize(name);
//ng-attr
if (isNgAttr = NG_ATTR_BINDING.test(ngAttrName)) {
//ng-attr-
name = name.replace(PREFIX_REGEXP, '')
.substr(8).replace(/_(.)/g, function(match, letter) {
return letter.toUpperCase();
});
}

var multiElementMatch = ngAttrName.match(MULTI_ELEMENT_DIR_RE);
//判断是否支持multiElement
if (multiElementMatch && directiveIsMultiElement(multiElementMatch[1])) {
attrStartName = name;
attrEndName = name.substr(0, name.length - 5) + 'end';
name = name.substr(0, name.length - 6); //去掉-start
}

nName = directiveNormalize(name.toLowerCase());
attrsMap[nName] = name;
if (isNgAttr || !attrs.hasOwnProperty(nName)) {
attrs[nName] = value;
if (getBooleanAttrName(node, nName)) {
attrs[nName] = true; // presence means true
}
}
addAttrInterpolateDirective(node, directives, value, nName, isNgAttr);
addDirective(directives, nName, 'A', maxPriority, ignoreDirective, attrStartName,
attrEndName);
}

注意到这一段代码

1
2
3
4
5
6
7
var multiElementMatch = ngAttrName.match(MULTI_ELEMENT_DIR_RE);
//判断是否支持multiElement
if (multiElementMatch && directiveIsMultiElement(multiElementMatch[1])) {
attrStartName = name;
attrEndName = name.substr(0, name.length - 5) + 'end';
name = name.substr(0, name.length - 6); //去掉-start
}

就是判断有无ng-show-start这种指令的

参考资料:
AngularJS multi-element directive
ngAttr with Angular for conditional attribute
angularJs关于指令的一些冷门属性

1…242526…47

John

232 日志
43 标签
GitHub Twitter
欢迎关注我的公众号:沉迷Spring
© 2023 johnwonder
由 Hexo 强力驱动 v3.2.0
|
主题 – NexT.Pisces v7.1.1
23039 | 24308
0%