Agile Web Development
在Mac OSX下安装和使用GO语言
by Elton on 2010年08月13日, under Agile Web Development, Mac
Go语言是Google新推出的结合了动态语言和静态语言优势的一个新兴的语言。下面介绍一下如何在Mac系统下安装和使用这个语言。
设置环境变量
$GOROOT
GO语言的根目录,通常是$HOME/go,当然也可以是任何其他目录。
$GOOS 和 $GOARCH
标明GO语言所在的系统和处理器类型。$GOOS可以是linux, freebsd, darwin (Mac OS X 10.5 or 10.6)和 nacl (Native Client, an incomplete port)。$GOARCH可以是amd64 (64-bit x86, the most mature port), 386 (32-bit x86), arm (32-bit ARM, an incomplete port)。
你可以在你的shell profile中设置这些变量,我是放在了~/.bash_profile里了。
-
export GOROOT=$HOME/go
-
export GOARCH=amd64
-
export GOOS=darwin
-
export PATH=$PATH:$HOME/bin
其中, $HOME/bin是GO默认的可执行文件的目录,加入到path中方便使用go的各种命令。
再执行
-
source ~/.bash_profile
使最新的配置文件生效。
获得源文件
如果你的系统中没有安装Mercurial(没有安装它,你就无法使用hg命令),那么使用这个命令来安装它:
-
-
$ sudo easy_install mercurial
-
然后使用下面的命令,还获得GO语言的源文件
-
-
$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT
-
安装GO语言
-
-
$ cd $GOROOT/src
-
$ ./all.bash
-
如果一切正常,你应该可以在最后看到类似的结果:
-
-
— cd ../test
-
N known bugs; 0 unexpected bugs
-
撰写第一个Hello,World
-
-
$ cat >hello.go <<EOF
-
package main
-
-
import "fmt"
-
-
func main() {
-
fmt.Printf("hello, world\n")
-
}
-
EOF
-
-
$ 6g hello.go
-
$ 6l hello.6
-
$ ./6.out
-
hello, world
-
$
-
日后更新
Go是一个发展中的语言,它的版本会经常进行更新,可以使用以下命令,保持GO语言是最新版本的
-
-
$ cd $GOROOT/src
-
$ hg pull
-
$ hg update release
-
$ ./all.bash
-
Go的官方网站是:http://golang.org
URL设计准则
by Elton on 2010年08月10日, under Web
URL 设计是 Web 设计中常被忽视的东西,事实上 URL 非常重要,这不仅是一个网页唯一的路径,还涉及到你的站点是否干净,友好。本文讲述 URL 这个司空见惯的 Web 元素中包含的大量不应为忽视的知识,准则与最佳实践。需要注意的是 W3C 建议使用 URI 取代 URL 一说。
关于 URL 的一些准则
首先是与 URL 有关的一些准则。
一个 URL 必须唯一地,永久地代表一个在线对象
URL 的最基本的使命是唯一地代表 Internet 上的一个对象,URL 必须和 Internet 上的对象一对一匹配。然而现实中,这很难实现,我们经常可以通过多个 URL 到达同一个页面,比如, http://mysite.com/product/tv 和 http://mysite.com/product?name=tv,这种情形在现代 CMS 中更是比比皆是,针对这个问题,SEO moz 有一篇很好的文章,讲到了如何使用 Canonical URL 机制解决站点中的重复 URL 问题。
URL 应该是永久的,这就要求你在站点上线前就非常严谨地规划 URL。如果有一天,你不得不更改 URL,一定使用 HTTP 301 机制,告诉浏览器和搜索引擎,你的那个 URL 所代表的对象,已经搬迁到新地址,这个机制可以保证你旧地址所获得 PR 不会被清零。
尽可能用户友好
这是 URL 设计的根本,你的 URL 应该为最终用户而设计。保持 URL 友好的一个好办法是在保证可读性的同时让它尽可能短。比如 /about 就好过 /about-acme-corp-page,当然,保持简短不能牺牲可读性, /13d2 一类的地址短则短矣,但并不友好。如果要在 Twitter, Facebook 一类的社会媒体网络分享你的 URL,可以使用 Bit.ly 一类的网址缩短工具,但这种工具产生的缩短 URL 并不友好,在 WordPress 一类的 CMS 中,可以使用 PrettyLink Pro 或 Short URL plugin 一类的可控制的地址缩短插件。

URL 的设计切忌使用一些对用户来说没有意义的内容,比如数据库的 ID 号, /products/23 这样的 URL 地址对用户是极不友好的,应当使用 /products/ballpoint-pen 一类的地址。
保持一致性
站点内的所有 URL 必须保持一致的格式和结构,这样可以为用户带来信任感,如果你必须更改 URL 格式和结构,需要使用 HTTP 301 机制。
可预测的 URL
这也是 URL 一致性的一个表现,如果你的 URL 拥有很好的一致性,用户可以根据 URL 猜测别的内容的 URL,假如 /events/2010/01 指向 2010 年 1 月份的日程内容,那
/events/2009/01 应当指向 2009 年 1 月的日程。
/events/2010 应当指向 2010 年全年的日程。
/events/2010/01/21 应当指向2010年1月21日的日程。
URL中的关键词
URL 中应该包含本页重点内容的关键词,比如 /posts/2010/07/02/trip-best-buy-memory-cards 一类的 URL 本身就是对页面内容的反应。在 URL 包含重点内容关键词,也可以提高 SEO 性能。SEO 的一个很重要的原则就是,在 URL 地址中包含内容关键词。
关于 URL 的技术细节
下面说的是有关 URL 的一些技术细节。
URL 不应包含 .html, aspx, cfm 一类的后缀
这类信息对最终用户是没有意义的,却占了额外的空间,一个例外是 .atom, .rss, .json 一类的特殊地址,这类地址是有特别的意义的。译者注:在某些虚拟主机式 Web 服务器,这种做法未必现实。
URL 不应包含 WWW 部分
WWW 部分并不包含任何意义,是一个额外的负担,不友好。可以使用 HTTP 301 机制,将 www.domain.com 定向到 domain.com 。
URL 的格式
URL 的格式如下:
domain.com/[key information]/[name]/?[modifiers]
Key information 部分一般代表信息的类型或类别。Modifiers 部分则属于查询字符串范畴,它不应当代表数据结构,应当代表数据的修饰。Key information 部分应当尽可能简短,同时应当表现出一种层级关系,比如 http://domain.com/posts/servers/nginx-ubuntu-10.04,或 http://domain.com/news/tech/2007/11/05/google-announces-android。
Google News 对新闻源有一个有趣的要求,Google 要求新闻源页面的 URL 中必须包含至少 3 位唯一的数字,因为他们会忽略年份数字,因此,应该使用一个5位或5位以上的数字。另外,也应该提供 Google News 站点地图 。如果你想向 Google 提供新闻,必须按这样的结构提供 URL,当然保持一致性,可以预测性也是必需的。
使用小写字符
URL 中所有字符都应使用小写,这更容易阅读。
URL 中包含的行为元素
URL 查询字符串中可能包含一些表示行为的元素,比如 show, delete, edit 等。非破坏性的行为可以体现在 URL 中,破坏性的行为应该使用 POST 。
使用 URL 友好字符
在 URL 中体现网页标题的时候,往往会用到一些特殊字符,应当把它们转换为 URL 友好字符:
全部大写字符换成小写
诸如 é 一类的字符应转换成对应的 e
空格使用短划线代替
诸如 !, @, #, $, %, ^, &, * 一类的字符应该使用短划线代替
双短划线应该使用单短划线代替
另外,没有必要的话,避免使用 %20 一类的 URL 逃逸符。
更多观点
Chris Shiflett 建议,可以使用一些类似句子的 URL,如:
chriscoyier.net/authored/digging-into-wordpress/
chriscoyier.net/has-worked-for/chatman-design/
chriscoyier.net/likes/trailer-park-boys
jacobwg.com/thinks/this-post/is/basically-done
译者补充:URL 的长度上限
URL 的最大长度是多少?W3C 的 HTTP 协议 并没有限定,然而,在实际应用中,经过试验,不同浏览器和 Web 服务器有不同的约定:
IE 的 URL 长度上限是 2083 字节,其中纯路径部分不能超过 2048 字节。
Firefox 浏览器的地址栏中超过 65536 字符后就不再显示。
Safari 浏览器一致测试到 80000 字符还工作得好好的。
Opera 浏览器测试到 190000 字符的时候,还正常工作。
Web 服务器:
Apache Web 服务器在接收到大约 4000 字符长的 URL 时候产生 413 Entity Too Large” 错误。
IIS 默认接收的最大 URL 是 16384 字符。
通过URL Rewrite来设置JBoss的301跳转
by Elton on 2010年07月28日, under Java, Linux
Introduction
The rewrite valve implements URL rewrite functionnality in a way that is very similar to mod_rewrite from Apache HTTP Server.
Configuration
The rewrite valve is configured as a regular valve, by adding the following to server.xml as child of an Engine or Host element (or inside a context.xml file):
-
<Valve className="org.jboss.web.rewrite.RewriteValve" />
The valve will then use a rewrite.properties file containing the rewrite directives, located according to the container it is assocaited to:
If associated with an engine, it should be placed in a folder named [engine_name] placed either in the classloader, or in the conf folder of the current JBoss profile
If associated with a host, it should be placed in a folder named [engine_name]/[host_name] placed either in the classloader, or in the conf folder of the current JBoss profile
If associated with a context, it should be placed in the WEB-INF folder of the web application
Directives
The rewrite.properties file contains a list of directives which closely resemble the directives used by mod_rewrite, in particular the central RewriteRule and RewriteCond directives.
Note: This section is a modified version of the mod_rewrite documentation, which is Copyright 1995-2006 The Apache Software Foundation, and licensed under the under the Apache License, Version 2.0.
RewriteCond
Syntax: RewriteCond TestString CondPattern
The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.
TestString is a string which can contain the following expanded constructs in addition to plain text:
RewriteRule backreferences: These are backreferences of the form $N (0 <= N <= 9), which provide access to the grouped parts (in parentheses) of the pattern, from the RewriteRule which is subject to the current set of RewriteCond conditions..
RewriteCond backreferences: These are backreferences of the form %N (1 <= N <= 9), which provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions.
RewriteMap expansions: These are expansions of the form ${mapname:key|default}. See the documentation for RewriteMap for more details.
Server-Variables: These are variables of the form %{ NAME_OF_VARIABLE } where NAME_OF_VARIABLE can be a string taken from the following list:
HTTP headers: connection & request:
HTTP_USER_AGENT
HTTP_REFERER
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_ACCEPT
REMOTE_ADDR
REMOTE_HOST
REMOTE_PORT
REMOTE_USER
REMOTE_IDENT
REQUEST_METHOD
SCRIPT_FILENAME
REQUEST_PATH
CONTEXT_PATH
SERVLET_PATH
PATH_INFO
QUERY_STRING
AUTH_TYPE
server internals: date and time: specials:
DOCUMENT_ROOT
SERVER_NAME
SERVER_ADDR
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE
TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME
THE_REQUEST
REQUEST_URI
REQUEST_FILENAME
HTTPS
These variables all correspond to the similarly named HTTP MIME-headers and Servlet API methods. Most are documented elsewhere in the Manual or in the CGI specification. Those that are special to the rewrite valve include those below.
REQUEST_PATH
Corresponds to the full path that is used for mapping.
CONTEXT_PATH
Corresponds to the path of the mapped context.
SERVLET_PATH
Corresponds to the servlet path.
THE_REQUEST
The full HTTP request line sent by the browser to the server (e.g., "GET /index.html HTTP/1.1"). This does not include any additional headers sent by the browser.
REQUEST_URI
The resource requested in the HTTP request line. (In the example above, this would be "/index.html".)
REQUEST_FILENAME
The full local filesystem path to the file or script matching the request.
HTTPS
Will contain the text "on" if the connection is using SSL/TLS, or "off" otherwise.
Other things you should be aware of:
The variables SCRIPT_FILENAME and REQUEST_FILENAME contain the same value - the value of the filename field of the internal request_rec structure of the Apache server. The first name is the commonly known CGI variable name while the second is the appropriate counterpart of REQUEST_URI (which contains the value of the uri field of request_rec).
%{ENV:variable}, where variable can be any Java system property, is also available.
%{SSL:variable}, where variable is the name of an SSL environment variable, are not implemented yet. Example: %{SSL:SSL_CIPHER_USEKEYSIZE} may expand to 128.
%{HTTP:header}, where header can be any HTTP MIME-header name, can always be used to obtain the value of a header sent in the HTTP request. Example: %{HTTP:Proxy-Connection} is the value of the HTTP header ``Proxy-Connection:''.
CondPattern is the condition pattern, a regular expression which is applied to the current instance of the TestString. TestString is first evaluated, before being matched against CondPattern.
Remember: CondPattern is a perl compatible regular expression with some additions:
You can prefix the pattern string with a '!' character (exclamation mark) to specify a non-matching pattern.
There are some special variants of CondPatterns. Instead of real regular expression strings you can also use one of the following:
'
'>CondPattern’ (lexicographically follows)
Treats the CondPattern as a plain string and compares it lexicographically to TestString. True if TestString lexicographically follows CondPattern.
‘=CondPattern’ (lexicographically equal)
Treats the CondPattern as a plain string and compares it lexicographically to TestString. True if TestString is lexicographically equal to CondPattern (the two strings are exactly equal, character for character). If CondPattern is “” (two quotation marks) this compares TestString to the empty string.
‘-d’ (is directory)
Treats the TestString as a pathname and tests whether or not it exists, and is a directory.
‘-f’ (is regular file)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
‘-s’ (is regular file, with size)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file with size greater than zero.
. All of these tests can also be prefixed by an exclamation mark (‘!’) to negate their meaning.
You can also set special flags for CondPattern by appending [flags] as the third argument to the RewriteCond directive, where flags is a comma-separated list of any of the following flags:
‘nocase|NC’ (no case)
This makes the test case-insensitive – differences between ‘A-Z’ and ‘a-z’ are ignored, both in the expanded TestString and the CondPattern. This flag is effective only for comparisons between TestString and CondPattern. It has no effect on filesystem and subrequest checks.
‘ornext|OR’ (or next condition)
Use this to combine rule conditions with a local OR instead of the implicit AND. Typical example:
-
-
RewriteCond %{REMOTE_HOST} ^host1.* [OR]
-
RewriteCond %{REMOTE_HOST} ^host2.* [OR]
-
RewriteCond %{REMOTE_HOST} ^host3.*
-
RewriteRule …some special stuff for any of these hosts…
Without this flag you would have to write the condition/rule pair three times.
Example:
To rewrite the Homepage of a site according to the “User-Agent:” header of the request, you can use the following:
-
-
RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*
-
RewriteRule ^/$ /homepage.max.html [L]
-
-
RewriteCond %{HTTP_USER_AGENT} ^Lynx.*
-
RewriteRule ^/$ /homepage.min.html [L]
-
-
RewriteRule ^/$ /homepage.std.html [L]
-
Explanation: If you use a browser which identifies itself as ‘Mozilla’ (including Netscape Navigator, Mozilla etc), then you get the max homepage (which could include frames, or other special features). If you use the Lynx browser (which is terminal-based), then you get the min homepage (which could be a version designed for easy, text-only browsing). If neither of these conditions apply (you use any other browser, or your browser identifies itself as something non-standard), you get the std (standard) homepage.
RewriteMap
Syntax: RewriteMap name rewriteMapClassName optionalParameters
The maps are implemented using an interface that users must implement. Its class name is org.jboss.web.rewrite.RewriteMap, and its code is:
-
-
package org.jboss.web.rewrite;
-
-
public interface RewriteMap {
-
}
-
RewriteRule
Syntax: RewriteRule Pattern Substitution
The RewriteRule directive is the real rewriting workhorse. The directive can occur more than once, with each instance defining a single rewrite rule. The order in which these rules are defined is important – this is the order in which they will be applied at run-time.
Pattern is a perl compatible regular expression, which is applied to the current URL. “Current” means the value of the URL when this rule is applied. This may not be the originally requested URL, which may already have matched a previous rule, and have been altered.
Some hints on the syntax of regular expressions:
Text:
. Any single character
[chars] Character class: Any character of the class “chars”
[^chars] Character class: Not a character of the class “chars”
text1|text2 Alternative: text1 or text2
Quantifiers:
? 0 or 1 occurrences of the preceding text
* 0 or N occurrences of the preceding text (N > 0)
+ 1 or N occurrences of the preceding text (N > 1)
Grouping:
(text) Grouping of text
(used either to set the borders of an alternative as above, or
to make backreferences, where the Nth group can
be referred to on the RHS of a RewriteRule as $N)
Anchors:
^ Start-of-line anchor
$ End-of-line anchor
Escaping:
\char escape the given char
(for instance, to specify the chars “.[]()” etc.)
For more information about regular expressions, have a look at the perl regular expression manpage (“perldoc perlre”). If you are interested in more detailed information about regular expressions and their variants (POSIX regex etc.) the following book is dedicated to this topic:
Mastering Regular Expressions, 2nd Edition
Jeffrey E.F. Friedl
O’Reilly & Associates, Inc. 2002
ISBN 0-596-00289-0
In the rules, the NOT character (‘!’) is also available as a possible pattern prefix. This enables you to negate a pattern; to say, for instance: “if the current URL does NOT match this pattern”. This can be used for exceptional cases, where it is easier to match the negative pattern, or as a last default rule.
Note: When using the NOT character to negate a pattern, you cannot include grouped wildcard parts in that pattern. This is because, when the pattern does NOT match (ie, the negation matches), there are no contents for the groups. Thus, if negated patterns are used, you cannot use $N in the substitution string!
The substitution of a rewrite rule is the string which is substituted for (or replaces) the original URL which Pattern matched. In addition to plain text, it can include
back-references ($N) to the RewriteRule pattern
back-references (%N) to the last matched RewriteCond pattern
server-variables as in rule condition test-strings (%{VARNAME})
mapping-function calls (${mapname:key|default})
Back-references are identifiers of the form $N (N=0..9), which will be replaced by the contents of the Nth group of the matched Pattern. The server-variables are the same as for the TestString of a RewriteCond directive. The mapping-functions come from the RewriteMap directive and are explained there. These three types of variables are expanded in the order above.
As already mentioned, all rewrite rules are applied to the Substitution (in the order in which they are defined in the config file). The URL is completely replaced by the Substitution and the rewriting process continues until all rules have been applied, or it is explicitly terminated by a flag.
There is a special substitution string named ‘-’ which means: NO substitution! This is useful in providing rewriting rules which only match URLs but do not substitute anything for them. It is commonly used in conjunction with the C (chain) flag, in order to apply more than one pattern before substitution occurs.
Additionally you can set special flags for Substitution by appending [flags] as the third argument to the RewriteRule directive. Flags is a comma-separated list of any of the following flags:
‘chain|C’ (chained with next rule)
This flag chains the current rule with the next rule (which itself can be chained with the following rule, and so on). This has the following effect: if a rule matches, then processing continues as usual – the flag has no effect. If the rule does not match, then all following chained rules are skipped. For instance, it can be used to remove the “.www” part, inside a per-directory rule set, when you let an external redirect happen (where the “.www” part should not occur!).
‘cookie|CO=NAME:VAL:domain[:lifetime[:path]]’ (set cookie)
This sets a cookie in the client’s browser. The cookie’s name is specified by NAME and the value is VAL. The domain field is the domain of the cookie, such as ‘.apache.org’, the optional lifetime is the lifetime of the cookie in minutes, and the optional path is the path of the cookie
‘env|E=VAR:VAL’ (set environment variable)
This forces an environment variable named VAR to be set to the value VAL, where VAL can contain regexp backreferences ($N and %N) which will be expanded. You can use this flag more than once, to set more than one variable. The variables can later be dereferenced in many situations, most commonly from within XSSI (via ) or CGI ($ENV{‘VAR’}). You can also dereference the variable in a later RewriteCond pattern, using %{ENV:VAR}. Use this to strip information from URLs, while maintaining a record of that information.
‘forbidden|F’ (force URL to be forbidden)
This forces the current URL to be forbidden – it immediately sends back a HTTP response of 403 (FORBIDDEN). Use this flag in conjunction with appropriate RewriteConds to conditionally block some URLs.
‘gone|G’ (force URL to be gone)
This forces the current URL to be gone – it immediately sends back a HTTP response of 410 (GONE). Use this flag to mark pages which no longer exist as gone.
‘host|H=Host’ (apply rewriting to host)
Rather that rewrite the URL, the virtual host will be rewritten.
‘last|L’ (last rule)
Stop the rewriting process here and don’t apply any more rewrite rules. This corresponds to the Perl last command or the break command in C. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL (‘/’) to a real one, e.g., ‘/e/www/’.
‘next|N’ (next round)
Re-run the rewriting process (starting again with the first rewriting rule). This time, the URL to match is no longer the original URL, but rather the URL returned by the last rewriting rule. This corresponds to the Perl next command or the continue command in C. Use this flag to restart the rewriting process – to immediately go to the top of the loop.
Be careful not to create an infinite loop!
‘nocase|NC’ (no case)
This makes the Pattern case-insensitive, ignoring difference between ‘A-Z’ and ‘a-z’ when Pattern is matched against the current URL.
‘noescape|NE’ (no URI escaping of output)
This flag prevents the rewrite valve from applying the usual URI escaping rules to the result of a rewrite. Ordinarily, special characters (such as ‘%’, ‘$’, ‘;’, and so on) will be escaped into their hexcode equivalents (‘%25′, ‘%24′, and ‘%3B’, respectively); this flag prevents this from happening. This allows percent symbols to appear in the output, as in RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE] which would turn ‘/foo/zed’ into a safe request for ‘/bar?arg=P1=zed’.
‘qsappend|QSA’ (query string append)
This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.
‘redirect|R [=code]‘ (force redirect)
Prefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force a external redirection. If no code is given, a HTTP response of 302 (MOVED TEMPORARILY) will be returned. If you want to use other response codes in the range 300-400, simply specify the appropriate number or use one of the following symbolic names: temp (default), permanent, seeother. Use this for rules to canonicalize the URL and return it to the client – to translate “/~” into “/u/”, or to always append a slash to /u/user, etc.
Note: When you use this flag, make sure that the substitution field is a valid URL! Otherwise, you will be redirecting to an invalid location. Remember that this flag on its own will only prepend http://thishost[:thisport]/ to the URL, and rewriting will continue. Usually, you will want to stop rewriting at this point, and redirect immediately. To stop rewriting, you should add the ‘L’ flag.
‘skip|S=num’ (skip next rule(s))
This flag forces the rewriting engine to skip the next num rules in sequence, if the current rule matches. Use this to make pseudo if-then-else constructs: The last rule of the then-clause becomes skip=N, where N is the number of rules in the else-clause. (This is not the same as the ‘chain|C’ flag!)
‘type|T=MIME-type’ (force MIME type)
Force the MIME-type of the target file to be MIME-type. This can be used to set up the content-type based on some conditions. For example, the following snippet allows .php files to be displayed by mod_php if they are called with the .phps extension: RewriteRule ^(.+\.php)s$ $1 [T=application/x-httpd-php-source]
参考:http://www.jboss.org/file-access/default/members/jbossweb/freezone/modules/rewrite/index.html
MySQL Query Cache
by Elton on 2010年07月5日, under Database
顾名思义,MySQL Query Cache 就是用来缓存和 Query 相关的数据的。具体来说,Query Cache 缓存了我们客户端提交给 MySQL 的 SELECT 语句以及该语句的结果集。大概来讲,就是将 SELECT 语句和语句的结果做了一个 HASH 映射关系然后保存在一定的内存区域中。
在大部分的 MySQL 分发版本中,Query Cache 功能默认都是打开的,我们可以通过调整 MySQL Server 的参数选项打开该功能。主要由以下5个参数构成:
query_cache_limit:允许 Cache 的单条 Query 结果集的最大容量,默认是1MB,超过此参数设置的 Query 结果集将不会被 Cache
query_cache_min_res_unit:设置 Query Cache 中每次分配内存的最小空间大小,也就是每个 Query 的 Cache 最小占用的内存空间大小
query_cache_size:设置 Query Cache 所使用的内存大小,默认值为0,大小必须是1024的整数倍,如果不是整数倍,MySQL 会自动调整降低最小量以达到1024的倍数
query_cache_type:控制 Query Cache 功能的开关,可以设置为0(OFF),1(ON)和2(DEMAND)三种,意义分别如下:
0(OFF):关闭 Query Cache 功能,任何情况下都不会使用 Query Cache
1(ON):开启 Query Cache 功能,但是当 SELECT 语句中使用的 SQL_NO_CACHE 提示后,将不使用Query Cache
2(DEMAND):开启 Query Cache 功能,但是只有当 SELECT 语句中使用了 SQL_CACHE 提示后,才使用 Query Cache
query_cache_wlock_invalidate:控制当有写锁定发生在表上的时刻是否先失效该表相关的 Query Cache,如果设置为 1(TRUE),则在写锁定的同时将失效该表相关的所有 Query Cache,如果设置为0(FALSE)则在锁定时刻仍然允许读取该表相关的 Query Cache。
Query Cache 如何处理子查询的?
这是我遇到的最为常见的一个问题。其实 Query Cache 是以客户端请求提交的 Query 为对象来处理的,只要客户端请求的是一个 Query,无论这个 Query 是一个简单的单表查询还是多表 Join,亦或者是带有子查询的复杂 SQL,都被当作成一个 Query,不会被分拆成多个 Query 来进行 Cache。所以,存在子查询的复杂 Query 也只会产生一个Cache对象,子查询不会产生单独的Cache内容。UNION[ALL] 类型的语句也同样如此。
Query Cache 是以 block 的方式存储的数据块吗?
不是,Query Cache 中缓存的内容仅仅只包含该 Query 所需要的结果数据,是结果集。当然,并不仅仅只是结果数据,还包含与该结果相关的其他信息,比如产生该 Cache 的客户端连接的字符集,数据的字符集,客户端连接的 Default Database等。
Query Cache 为什么效率会非常高,即使所有数据都可以 Cache 进内存的情况下,有些时候也不如使用 Query Cache 的效率高?
Query Cache 的查找,是在 MySQL 接受到客户端请求后在对 Query 进行权限验证之后,SQL 解析之前。也就是说,当 MySQL 接受到客户端的SQL后,仅仅只需要对其进行相应的权限验证后就会通过 Query Cache 来查找结果,甚至都不需要经过 Optimizer 模块进行执行计划的分析优化,更不许要发生任何存储引擎的交互,减少了大量的磁盘 IO 和 CPU 运算,所以效率非常高。
客户端提交的 SQL 语句大小写对 Query Cache 有影响吗?
有,由于 Query Cache 在内存中是以 HASH 结构来进行映射,HASH 算法基础就是组成 SQL 语句的字符,所以必须要整个 SQL 语句在字符级别完全一致,才能在 Query Cache 中命中,即使多一个空格也不行。
一个 SQL 语句在 Query Cache 中的内容,在什么情况下会失效?
为了保证 Query Cache 中的内容与是实际数据绝对一致,当表中的数据有任何变化,包括新增,修改,删除等,都会使所有引用到该表的 SQL 的 Query Cache 失效。
为什么我的系统在开启了 Query Cache 之后整体性能反而下降了?
当开启了 Query Cache 之后,尤其是当我们的 query_cache_type 参数设置为 1 以后,MySQL 会对每个 SELECT 语句都进行 Query Cache 查找,查找操作虽然比较简单,但仍然也是要消耗一些 CPU 运算资源的。而由于 Query Cache 的失效机制的特性,可能由于表上的数据变化比较频繁,大量的 Query Cache 频繁的被失效,所以 Query Cache 的命中率就可能比较低下。所以有些场景下,Query Cache 不仅不能提高效率,反而可能造成负面影响。
如何确认一个系统的 Query Cache 的运行是否健康,命中率如何,设置量是否足够?
MySQL 提供了一系列的 Global Status 来记录 Query Cache 的当前状态,具体如下:
Qcache_free_blocks:目前还处于空闲状态的 Query Cache 中内存 Block 数目
Qcache_free_memory:目前还处于空闲状态的 Query Cache 内存总量
Qcache_hits:Query Cache 命中次数
Qcache_inserts:向 Query Cache 中插入新的 Query Cache 的次数,也就是没有命中的次数
Qcache_lowmem_prunes:当 Query Cache 内存容量不够,需要从中删除老的 Query Cache 以给新的 Cache 对象使用的次数
Qcache_not_cached:没有被 Cache 的 SQL 数,包括无法被 Cache 的 SQL 以及由于 query_cache_type 设置的不会被 Cache 的 SQL
Qcache_queries_in_cache:目前在 Query Cache 中的 SQL 数量
Qcache_total_blocks:Query Cache 中总的 Block 数量
可以根据这几个状态计算出 Cache 命中率,计算出 Query Cache 大小设置是否足够,总的来说,我个人不建议将 Query Cache 的大小设置超过256MB,这也是业界比较常用的做法。
MySQL Cluster 是否可以使用 Query Cache?
其实在我们的生产环境中也没有使用 MySQL Cluster,所以我也没有在 MySQL Cluster 环境中使用 Query Cache 的实际经验,只是 MySQL 文档中说明确实可以在 MySQL Cluster 中使用 Query Cache。从 MySQL Cluster 的原理来分析,也觉得应该可以使用,毕竟 SQL 节点和数据节点比较独立,各司其职,只是 Cache 的失效机制会要稍微复杂一点。
引自:http://www.javaeye.com/news/16744-mysql-query-cache-summary
Tomcat 设计模式分析
by Elton on 2010年05月29日, under Java
门面设计模式
门面设计模式在 Tomcat 中有多处使用,在 Request 和 Response 对象封装中、Standard Wrapper 到 ServletConfig 封装中、ApplicationContext 到 ServletContext 封装中等都用到了这种设计模式。
门面设计模式的原理
这么多场合都用到了这种设计模式,那这种设计模式究竟能有什么作用呢?顾名思义,就是将一个东西封装成一个门面好与人家更容易进行交流,就像一个国家的外交部一样。
这种设计模式主要用在一个大的系统中有多个子系统组成时,这多个子系统肯定要涉及到相互通信,但是每个子系统又不能将自己的内部数据过多的暴露给其它系统,不然就没有必要划分子系统了。每个子系统都会设计一个门面,把别的系统感兴趣的数据封装起来,通过这个门面来进行访问。这就是门面设计模式存在的意义。
门面设计模式示意图如下:
图 1. 门面示意图

Client 只能访问到 Façade 中提供的数据是门面设计模式的关键,至于 Client 如何访问 Façade 和 Subsystem 如何提供 Façade 门面设计模式并没有规定死。
Tomcat 的门面设计模式示例
Tomcat 中门面设计模式使用的很多,因为 Tomcat 中有很多不同组件,每个组件要相互交互数据,用门面模式隔离数据是个很好的方法。
下面是 Request 上使用的门面设计模式:
图 2. Request 的门面设计模式类图

从图中可以看出 HttpRequestFacade 类封装了 HttpRequest 接口能够提供数据,通过 HttpRequestFacade 访问到的数据都被代理到 HttpRequest 中,通常被封装的对象都被设为 Private 或者 Protected 访问修饰,以防止在 Façade 中被直接访问。
观察者设计模式
这种设计模式也是常用的设计方法通常也叫发布 – 订阅模式,也就是事件监听机制,通常在某个事件发生的前后会触发一些操作。
观察者模式的原理
观察者模式原理也很简单,就是你在做事的时候旁边总有一个人在盯着你,当你做的事情是它感兴趣的时候,它就会跟着做另外一些事情。但是盯着你的人必须要到你那去登记,不然你无法通知它。观察者模式通常包含下面这几个角色:
Subject 就是抽象主题:它负责管理所有观察者的引用,同时定义主要的事件操作。
ConcreteSubject 具体主题:它实现了抽象主题的所有定义的接口,当自己发生变化时,会通知所有观察者。
Observer 观察者:监听主题发生变化相应的操作接口。
Tomcat 的观察者模式示例
Tomcat 中观察者模式也有多处使用,前面讲的控制组件生命周期的 Lifecycle 就是这种模式的体现,还有对 Servlet 实例的创建、Session 的管理、Container 等都是同样的原理。下面主要看一下 Lifecycle 的具体实现。
Lifecycle 的观察者模式结构图:
图 3. Lifecycle 的观察者模式结构图

上面的结构图中,LifecycleListener 代表的是抽象观察者,它定义一个 lifecycleEvent 方法,这个方法就是当主题变化时要执行的方法。 ServerLifecycleListener 代表的是具体的观察者,它实现了 LifecycleListener 接口的方法,就是这个具体的观察者具体的实现方式。Lifecycle 接口代表的是抽象主题,它定义了管理观察者的方法和它要所做的其它方法。而 StandardServer 代表的是具体主题,它实现了抽象主题的所有方法。这里 Tomcat 对观察者做了扩展,增加了另外两个类:LifecycleSupport、LifecycleEvent,它们作为辅助类扩展了观察者的功能。LifecycleEvent 使得可以定义事件类别,不同的事件可区别处理,更加灵活。LifecycleSupport 类代理了主题对多观察者的管理,将这个管理抽出来统一实现,以后如果修改只要修改 LifecycleSupport 类就可以了,不需要去修改所有具体主题,因为所有具体主题的对观察者的操作都被代理给 LifecycleSupport 类了。这可以认为是观察者模式的改进版。
LifecycleSupport 调用观察者的方法代码如下:
清单 1. LifecycleSupport 中的 fireLifecycleEvent 方法
-
-
LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
-
LifecycleListener interested[] = null;
-
synchronized (listeners) {
-
interested = (LifecycleListener[]) listeners.clone();
-
}
-
for (int i = 0; i < interested.length; i++)
-
interested[i].lifecycleEvent(event);
-
}
-
主题是怎么通知观察者呢?看下面代码:
清单 2. 容器中的 start 方法
-
-
public void start() throws LifecycleException {
-
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
-
lifecycle.fireLifecycleEvent(START_EVENT, null);
-
started = true;
-
synchronized (services) {
-
for (int i = 0; i < services.length; i++) {
-
if (services[i] instanceof Lifecycle)
-
((Lifecycle) services[i]).start();
-
}
-
}
-
lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
-
}
-
命令设计模式
前面把 Tomcat 中两个核心组件 Connector 和 Container,比作一对夫妻。男的将接受过来的请求以命令的方式交给女主人。对应到 Connector 和 Container,Connector 也是通过命令模式调用 Container 的。
命令模式的原理
命令模式主要作用就是封装命令,把发出命令的责任和执行命令的责任分开。也是一种功能的分工。不同的模块可以对同一个命令做出不同解释。
下面是命令模式通常包含下面几个角色:
Client:创建一个命令,并决定接受者
Command 命令:命令接口定义一个抽象方法
ConcreteCommand:具体命令,负责调用接受者的相应操作
Invoker 请求者:负责调用命令对象执行请求
Receiver 接受者:负责具体实施和执行一次请求
Tomcat 中的命令模式的示例
Tomcat 中命令模式在 Connector 和 Container 组件之间有体现,Tomcat 作为一个应用服务器,无疑会接受到很多请求,如何分配和执行这些请求是必须的功能。
下面看一下 Tomcat 是如何实现命令模式的,下面是 Tomcat 命令模式的结构图:
图 4. Tomcat 命令模式的结构图

Connector 作为抽象请求者,HttpConnector 作为具体请求者。HttpProcessor 作为命令。Container 作为命令的抽象接受者,ContainerBase 作为具体的接受者。客户端就是应用服务器 Server 组件了。Server 首先创建命令请求者 HttpConnector 对象,然后创建命令 HttpProcessor 命令对象。再把命令对象交给命令接受者 ContainerBase 容器来处理命令。命令的最终是被 Tomcat 的 Container 执行的。命令可以以队列的方式进来,Container 也可以以不同的方式来处理请求,如 HTTP1.0 协议和 HTTP1.1 的处理方式就会不同。
责任链模式
Tomcat 中一个最容易发现的设计模式就是责任链模式,这个设计模式也是 Tomcat 中 Container 设计的基础,整个容器的就是通过一个链连接在一起,这个链一直将请求正确的传递给最终处理请求的那个 Servlet。
责任链模式的原理
责任链模式,就是很多对象有每个对象对其下家的引用而连接起来形成一条链,请求在这条链上传递,直到链上的某个对象处理此请求,或者每个对象都可以处理请求,并传给下一家,直到最终链上每个对象都处理完。这样可以不影响客户端而能够在链上增加任意的处理节点。
通常责任链模式包含下面几个角色:
Handler(抽象处理者):定义一个处理请求的接口
ConcreteHandler(具体处理者):处理请求的具体类,或者传给下家
Tomcat 中责任链模式示例
在 tomcat 中这种设计模式几乎被完整的使用,tomcat 的容器设置就是责任链模式,从 Engine 到 Host 再到 Context 一直到 Wrapper 都是通过一个链传递请求。
Tomcat 中责任链模式的类结构图如下:
图 5. Tomcat 责任链模式的结构图

上图基本描述了四个子容器使用责任链模式的类结构图,对应的责任链模式的角色,Container 扮演抽象处理者角色,具体处理者由 StandardEngine 等子容器扮演。与标准的责任链不同的是,这里引入了 Pipeline 和 Valve 接口。他们有什么作用呢?
实际上 Pipeline 和 Valve 是扩展了这个链的功能,使得在链往下传递过程中,能够接受外界的干预。Pipeline 就是连接每个子容器的管子,里面传递的 Request 和 Response 对象好比管子里流的水,而 Valve 就是这个管子上开的一个个小口子,让你有机会能够接触到里面的水,做一些额外的事情。
为了防止水被引出来而不能流到下一个容器中,每一段管子最后总有一个节点保证它一定能流到下一个子容器,所以每个容器都有一个 StandardXXXValve。只要涉及到这种有链式是处理流程这是一个非常值得借鉴的模式。
利用CSS3实现文字内阴影效果
by Elton on 2010年03月22日, under Web
最终效果

在没有CSS3之前,要做到这种文字效果只能借助于Photoshop之类的图片处理软件。 现在很多浏览器已经支持CSS3了(除了该死的IE)。下面将教您如何使用CSS3完成上述效果
Step 1: HTML标签
下面是很简单的HTML标签,用以后面加上CSS特效
-
-
<div id="insetBgd">
-
<h1 class="insetType">Inset Typography</h1>
-
</div>
-
Step 2: 背景
我们想给文字加一个渐变的背景,从#003471 过渡到 #448CCB.
-
-
#insetBgd {
-
width: 550px;
-
height: 100px;
-
background: -moz-linear-gradient(-90deg, #003471, #448CCB);
-
background: -webkit-gradient(linear, left top, left bottom, from(#003471), to(#448CCB));
-
}
-

Step 3: 定义文字的样式
首先定义文字的字体,大小和颜色。
-
-
h1.insetType {
-
font-family: Rockwell, Georgia, "Times New Roman", Times, serif;
-
font-size: 50px;
-
color: #0D4383;
-
}
-
Step 4: 实现内阴影效果
这是最关键的一步,也是最后一步。用来实现类似于photoshop的“inner shadow”效果。但是CSS的text-shadow并没有这个效果,我们不得不使用多个黑色和白色的阴影来实现这个效果:
-
-
text-shadow: rgba(0,0,0,0.5) -1px 0, rgba(0,0,0,0.3) 0 -1px, rgba(255,255,255,0.5) 0 1px, rgba(0,0,0,0.3) -1px -2px;
-
下面是完整代码:
-
-
#insetBgd {
-
width: 550px;
-
height: 100px;
-
background: -moz-linear-gradient(-90deg,#003471,#448CCB);
-
background: -webkit-gradient(linear, left top, left bottom, from(#003471), to(#448CCB));
-
}
-
-
h1.insetType {
-
padding-left: 50px; /* The padding is just there to move the h1 element to the center of the div */
-
padding-top: 17px;
-
font-family: Rockwell, Georgia, "Times New Roman", Times, serif;
-
font-size: 50px;
-
color: #0D4383;
-
text-shadow: rgba(0,0,0,0.5) -1px 0, rgba(0,0,0,0.3) 0 -1px, rgba(255,255,255,0.5) 0 1px, rgba(0,0,0,0.3) -1px -2px;
-
}
-
最终效果

Ruby on Rails: RubyGem version error: rack(1.0.0 not ~> 1.0.1) (RuntimeError)
by Elton on 2010年03月7日, under Rails
当你升级了rails后,有时候会出现rack版本跟rails版本不匹配的情况。 当你执行./script/generate命令的时候,会出现以下错误信息:
-
-
/Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/initializer.rb:271:in `require_frameworks‘: RubyGem version error: rack(1.0.0 not ~> 1.0.1) (RuntimeError)
-
from /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/initializer.rb:134:in `process’
-
from /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/initializer.rb:113:in `send‘
-
from /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/initializer.rb:113:in `run’
-
from /Volumes/Data/study/ruby/blog/config/environment.rb:9
-
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require‘
-
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require’
-
from /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/commands/generate.rb:1
-
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require‘
-
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require’
-
from ./script/generate:3
-
解决方案:
-
-
$ sudo gem uninstall rack
-
-
Select gem to uninstall:
-
1. rack-1.0.0
-
2. rack-1.1.0
-
3. All versions
-
> 1
-
-
You have requested to uninstall the gem:
-
rack-1.0.0
-
actionpack-2.3.5 depends on [rack (~> 1.0.0)]
-
actionpack-2.3.4 depends on [rack (~> 1.0.0)]
-
If you remove this gems, one or more dependencies will not be met.
-
Continue with Uninstall? [Yn] y
-
Successfully uninstalled rack-1.0.0
-
然后再执行
-
-
sudo gem install -v=2.3.5 rails
-
-
Successfully installed rack-1.0.1
-
1 gem installed
-
Installing ri documentation for rack-1.0.1…
-
Installing RDoc documentation for rack-1.0.1…
-
问题解决
如何使用MyFace快速构建基于JSF的应用
by Elton on 2010年02月2日, under Java
如果大家使用Apache MyFace的JSF实现来搭建JSF应用,可以利用Apache提供的便捷maven方法来快速搭建一个初始的应用。
-
-
mvn archetype:generate -DarchetypeCatalog=http://myfaces.apache.org
-
……
-
Choose archetype:
-
1: http://myfaces.apache.org -> myfaces-archetype-helloworld (Simple Web application using Apache Myfaces)
-
2: http://myfaces.apache.org -> myfaces-archetype-helloworld-facelets (Simple Web application using Apache Myfaces and Facelets)
-
3: http://myfaces.apache.org -> myfaces-archetype-helloworld-portlets (Simple Web application using Apache Myfaces and Portlets)
-
4: http://myfaces.apache.org -> myfaces-archetype-jsfcomponents (Simple JSF Component using Apache Myfaces)
-
5: http://myfaces.apache.org -> myfaces-archetype-trinidad (Simple Web application using Apache Myfaces and Trinidad)
-
Choose a number: (1/2/3/4/5):
-
……
-
可以看到它提供了你5个初始项目进行选择,你可以根据你的需要选择响应的选项。之后填写响应的参数后,你的应用程序框架就生成了。
然后再使用以下命令,来下载必要的依赖包,假设你的groupId=myAppId,artifactId=yourapp
-
-
cd yourapp
-
mvn package
-
之后你就搭建了一个初始框架,你可以继续使用maven来操作这个框架。
It appears the JSP version of the container is older than 2.1 and unable to locate the EL RI expression factory, com.sun.el.ExpressionFactoryImpl.
by Elton on 2010年01月16日, under Java
今天调试一个jboss应用的时候发现这个错误。
严重: Unable to instantiate ExpressionFactory ‘com.sun.el.ExpressionFactoryImpl’
2008-9-12 11:36:47 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class com.sun.faces.config.ConfigureListener
com.sun.faces.config.ConfigurationException: It appears the JSP version of the container is older than 2.1 and unable to locate the EL RI expression factory, com.sun.el.ExpressionFactoryImpl. If not using JSP or the EL RI, make sure the context initialization parameter, com.sun.faces.expressionFactory, is properly set.
at com.sun.faces.config.ConfigureListener.registerELResolverAndListenerWithJsp(ConfigureListener.java:545)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:211)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3692)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4127)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:603)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:535)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:470)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1118)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1020)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:680)
at org.apache.catalina.startup.Catalina.start(Catalina.java:540)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409)
看提示是说jsp的版本有问题,其实是缺少了一个jar包
el-ri.jar
可以在这里下载:http://code.google.com/p/seam-forum/downloads/detail?name=el-ri.jar&can=2&q=
将这个包放在jbosshome/server/default/lib中
在JBOSS服务器上使用Myfaces的JSF实现
by Elton on 2010年01月16日, under Java
JBOSS AS服务器默认使用的JSF实现是SUN的RI,要把他替换成Myfaces需要:
1.移除JBoss服务器的现有的JSF实现RI
修改JBOSS_HOME下的server\< config-name>\deploy\jboss-web.deployer\conf\web.xml文件,把JSF的监听器注掉,并删除jboss-web.deployer 目录下的jsf-libs文件夹
-
-
<!–
-
<context-param>
-
<param-name>com.sun.faces.injectionProvider</param-name>
-
<param-value>org.jboss.web.jsf.integration.injection.JBossInjectionProvider</param-value>
-
</context-param>
-
–>
-
<!– Comment/Remove this –>
-
<!– Configures JSF for a web application if the javax.faces.webapp.FacesServlet is declared –>
-
<!– in web.xml. –>
-
<!–
-
<listener>
-
<listener-class>org.jboss.web.jsf.integration.config.JBossJSFConfigureListener</listener-class>
-
</listener>
-
–>
-
<!– Comment/Remove this –>
-
<!– Listens to all web app lifecycle events so that @PreDestroy can be called on –>
-
<!– JSF managed beans that go out of scope. You can comment this out if you –>
-
<!– don‘t use JSF or you don’t use annotations on your managed beans. –>
-
<!–
-
<listener>
-
<listener-class>com.sun.faces.application.WebappLifecycleListener</listener-class>
-
</listener>
-
–>
-
2.安装Myfaces的JSF实现
在jboss-web.deployer 目录下新建myfaces-libs文件夹,并把myfaces的实现jar包拷贝到里面
-
-
commons-beanutils.jar
-
commons-digester-1.6.jar
-
commons-el.jar
-
commons-lang-2.1.jar
-
myfaces-api.jar
-
myfaces-impl.jar
-
在jboss-web.deployer\conf\web.xml中添加myfaces的监听器
-
-
<listener>
-
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
-
</listener>
-
3.修改jboss-web.deployer\conf\web.xml中JSF相关的init-param参数,替换
-
-
<init-param>
-
<description>JSF standard tlds</description>
-
<param-name>tagLibJar0</param-name>
-
<param-value>jsf-libs/jsf-impl.jar</param-value>
-
</init-param>
-
<init-param>
-
<description>JSTL standard tlds</description>
-
<param-name>tagLibJar1</param-name>
-
<param-value>jstl.jar</param-value>
-
</init-param>
-
为
-
-
<init-param>
-
<description>MyFaces tlds</description>
-
<param-name>tagLibJar0</param-name>
-
<param-value>myfaces-libs/myfaces-impl.jar</param-value>
-
</init-param>
-
现在启动JBOSS AS就可以看到加载的已经加载了Myfaces的JSF。可以在JBOSS服务器上使用Myfaces的JSF实现了。


