Skip to content

Commit 35143e5

Browse files
author
Michael Zhou
committed
改进archetype,添加更多示例
1 parent 4bea88c commit 35143e5

File tree

19 files changed

+410
-26
lines changed

19 files changed

+410
-26
lines changed

archetype-webx-quickstart/src/main/resources/archetype-resources/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ target
55
.classpath
66
.svn
77

8+
*.iml
89
/out
910
/.idea/workspace.xml
11+
/.idea/modules.xml
12+
/.idea/libraries/Maven*.xml
13+
/.idea/artifacts/*.xml
1014
/*.iws
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package ${package}.app1;
1919

20-
public class SimpleObject {
20+
public class Visitor {
2121
private String name;
2222

2323
public String getName() {
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
import com.alibaba.citrus.turbine.Navigator;
2121
import com.alibaba.citrus.turbine.dataresolver.FormGroup;
2222

23-
import ${package}.app1.SimpleObject;
23+
import ${package}.app1.Visitor;
2424

25-
public class SimpleAction {
26-
public void doGreeting(@FormGroup("simple") SimpleObject simple, Navigator nav) {
27-
String name = simple.getName();
28-
nav.redirectTo("app1Link").withTarget("hello").withParameter("name", name);
25+
public class RegisterAction {
26+
public void doRegister(@FormGroup("register") Visitor visitor, Navigator nav) {
27+
String name = visitor.getName();
28+
nav.redirectTo("app1Link").withTarget("form/welcome").withParameter("name", name);
2929
}
3030
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18-
package ${package}.app1.module.screen;
18+
package ${package}.app1.module.screen.form;
1919

2020
import com.alibaba.citrus.turbine.Context;
2121
import com.alibaba.citrus.turbine.dataresolver.Param;
2222

23-
public class Hello {
23+
public class Welcome {
2424
public void execute(@Param("name") String name, Context context) {
2525
context.put("name", name);
2626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2002-2012 Alibaba Group Holding Limited.
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package ${package}.app1.module.screen.list;
19+
20+
import com.alibaba.citrus.turbine.Context;
21+
22+
public class Default {
23+
public void execute(Context context) {
24+
context.put("list", new String[] {
25+
"Adobe Photoshop",
26+
"Adobe Acrobat",
27+
"Adobe Lightroom",
28+
"Apple Aperture",
29+
"Microsoft Office",
30+
"IntelliJ IDEA",
31+
"<<\"Objective-C\"指南>>"
32+
});
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2002-2012 Alibaba Group Holding Limited.
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package ${package}.app1.module.screen.simple;
19+
20+
import static com.alibaba.citrus.util.StringEscapeUtil.*;
21+
22+
import java.io.PrintWriter;
23+
import javax.servlet.http.HttpServletResponse;
24+
25+
import com.alibaba.citrus.service.requestcontext.buffered.BufferedRequestContext;
26+
import com.alibaba.citrus.turbine.dataresolver.Param;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
29+
/**
30+
* 在浏览器上显示计数。
31+
* <p/>
32+
* 多数动态WEB页面是这样的:先在内存中生成整个页面,然后一次性提交给浏览器。这样,在页面完全生成之前,用户必须等待。
33+
* <p/>
34+
* 此程序展示了一种技术,能让页面一边生成、一边展示给用户。虽然页面还没有完全生成,但用户已经能够看到部分页面。
35+
* 这不仅改善了用户的体验,也使浏览器处理页面、下载、服务器生成页面三者实现了并发,从而加快了显示页面的总时间。
36+
*
37+
* @author Michael Zhou
38+
*/
39+
public class Count {
40+
@Autowired
41+
private HttpServletResponse response;
42+
43+
@Autowired
44+
private BufferedRequestContext brc;
45+
46+
public void execute(@Param("to") int toNumber) throws Exception {
47+
// 必须关闭buffering,未完成的页面才会被显示在浏览器上。
48+
brc.setBuffering(false);
49+
50+
// 设置content type,但不需要设置charset,框架会设置正确的charset。
51+
response.setContentType("text/html");
52+
53+
PrintWriter out = response.getWriter();
54+
55+
out.println("<html>");
56+
out.println("<head>");
57+
out.println(" <title>Count to " + toNumber + "</title>");
58+
out.println("</head>");
59+
out.println("<body>");
60+
61+
for (int i = 1; i <= toNumber; i++) {
62+
for (int j = 0; j < 10000; j++) {
63+
out.print(i);
64+
}
65+
66+
out.println();
67+
out.flush(); // 将当前的结果立即显示到浏览器上
68+
69+
Thread.sleep(1000); // 特意等待1秒,仅用于演示。
70+
}
71+
72+
out.println("</body>");
73+
out.println("</html>");
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2002-2012 Alibaba Group Holding Limited.
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package ${package}.app1.module.screen.simple;
19+
20+
import static com.alibaba.citrus.util.ObjectUtil.*;
21+
import static com.alibaba.citrus.util.StringEscapeUtil.*;
22+
import static org.apache.commons.lang.StringUtils.*;
23+
24+
import java.io.PrintWriter;
25+
import javax.servlet.http.HttpServletResponse;
26+
27+
import com.alibaba.citrus.service.requestcontext.buffered.BufferedRequestContext;
28+
import com.alibaba.citrus.turbine.dataresolver.Param;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
31+
/**
32+
* 动态生成下载文件。
33+
*
34+
* @author Michael Zhou
35+
*/
36+
public class Download {
37+
@Autowired
38+
private HttpServletResponse response;
39+
40+
@Autowired
41+
private BufferedRequestContext brc;
42+
43+
public void execute(@Param("filename") String filename) throws Exception {
44+
// 为了增强用户体验,关闭buffering,让下载立即开始,而不是等待整个文件生成完才通知用户下载。
45+
brc.setBuffering(false);
46+
47+
// 设置headers,下载文件名必须避免非us-ascii字符,因为各浏览器的兼容程度不同。
48+
filename = defaultIfNull(trimToNull(filename), "image") + ".txt";
49+
filename = "\"" + escapeURL(filename) + "\"";
50+
51+
response.setHeader("Content-disposition", "attachment; filename=" + filename);
52+
53+
// 只要设置了正确的content type,你就可以让用户下载任何文本或二进制的内容:
54+
// HTML、JSON、JavaScript、JPG、PDF、EXCEL等。
55+
response.setContentType("text/plain");
56+
57+
PrintWriter out = response.getWriter();
58+
59+
for (int i = 0; i < 100; i++) {
60+
out.flush(); // 立即提示用户下载
61+
62+
for (int j = 0; j < 100000; j++) {
63+
out.print(i);
64+
}
65+
66+
out.println();
67+
68+
Thread.sleep(100); // 故意延迟,以减缓下载速度
69+
}
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2002-2012 Alibaba Group Holding Limited.
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package ${package}.app1.module.screen.simple;
19+
20+
import java.io.PrintWriter;
21+
import javax.servlet.http.HttpServletResponse;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
25+
/**
26+
* 这是最简单的页面:不需要模板,直接输出到浏览器,就像最简单的servlet一样。
27+
*
28+
* @author Michael Zhou
29+
*/
30+
public class SayHi {
31+
@Autowired
32+
private HttpServletResponse response;
33+
34+
public void execute() throws Exception {
35+
// 设置content type,但不需要设置charset。框架会设置正确的charset。
36+
response.setContentType("text/plain");
37+
38+
// 如同servlet一样:取得输出流。
39+
PrintWriter out = response.getWriter();
40+
41+
out.println("Hi there, how are you doing today?");
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2002-2012 Alibaba Group Holding Limited.
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package ${package}.app1.module.screen.simple;
19+
20+
import static com.alibaba.citrus.util.ObjectUtil.*;
21+
import static com.alibaba.citrus.util.StringEscapeUtil.*;
22+
import static org.apache.commons.lang.StringUtils.*;
23+
24+
import java.awt.*;
25+
import java.awt.image.BufferedImage;
26+
import java.io.IOException;
27+
import java.io.OutputStream;
28+
import javax.imageio.ImageIO;
29+
import javax.servlet.http.HttpServletResponse;
30+
31+
import com.alibaba.citrus.service.requestcontext.buffered.BufferedRequestContext;
32+
import com.alibaba.citrus.turbine.dataresolver.Param;
33+
import org.springframework.beans.factory.annotation.Autowired;
34+
35+
/**
36+
* 动态创建二进制图片。
37+
*
38+
* @author Michael Zhou
39+
*/
40+
public class SayHiImage {
41+
@Autowired
42+
private HttpServletResponse response;
43+
44+
@Autowired
45+
private BufferedRequestContext brc;
46+
47+
public void execute() throws Exception {
48+
// 为了节省内存,关闭buffering。
49+
brc.setBuffering(false);
50+
51+
// 只要设置了正确的content type,你就可以输出任何文本或二进制的内容:
52+
// HTML、JSON、JavaScript、JPG、PDF、EXCEL等。
53+
response.setContentType("image/jpeg");
54+
55+
OutputStream out = response.getOutputStream();
56+
57+
writeImage(out);
58+
}
59+
60+
private void writeImage(OutputStream out) throws IOException {
61+
BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
62+
Graphics2D g2d = img.createGraphics();
63+
64+
g2d.setPaint(Color.red);
65+
g2d.setFont(new Font("Serif", Font.BOLD, 36));
66+
g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight());
67+
g2d.dispose();
68+
69+
ImageIO.write(img, "jpg", out);
70+
}
71+
}

archetype-webx-quickstart/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/app1/form.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
- Simple form
3232
- ===============================================
3333
-->
34-
<services:group name="simple" extends="csrfCheck">
34+
<services:group name="register" extends="csrfCheck">
3535
<services:field name="name" displayName="你的名字">
3636
<required-validator>
3737
<message>必须填写 ${displayName}</message>

0 commit comments

Comments
 (0)