click是apache的一个开源j2ee框架,简单易于配置。官网:http://click.apache.org/
1.下载相应jar包
2.创建一个web工程
3.编写web.xml 如下:
<servlet>
<servlet-name>ClickServlet</servlet-name>
<servlet-class>org.apache.click.ClickServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ClickServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
4.在WEB-INF下新建click.xml,内容如下:
<click-app>
<!--包路径(指定为你编写的路径)-->
<pages package="com.xxx.pages" automapping="true"/>
<mode value="debug"/>
</click-app>
5.将click所需包导入到工程(如下图)
6.编写helloworld类
public class HelloWorld extends Page{ private static final long serialVersionUID = 1L; private ActionLink control=new ActionLink("hello",this,"onClick"); private ActionLink forward=new ActionLink("forward",this,"onForward"); @Override public void onInit() { this.addModel("time", new Date()); this.addControl(control); this.addControl(forward); } //点击“hello”触发,返回消息到页面 public Boolean onClick(){ String msg = "this is control listener!"; addModel("msg", msg); return true; } //点击“forward”触发,跳转到指定页面 public Boolean onForward(){ this.setForward("xxx.htm"); return true; } }
7.编写helloworld.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>click</title> </head> <body> now time is:$time <div> <p>$hello.href</p> <p>$msg</p> click <a href="$hello.href">here</a> #if ($msg) <div> $msg </div> #else <div>hello world!</div> #end </div> click <a href="$forward.href">here</a> to skip </body> </html>
8.将工程部署到tomcat,启动并访问
说明:通过这个例子可以看出,当用户未点击页面的链接时,msg没有值,页面显示“hello world”。当用户点击了链接之后,则会触发定义的onClick函数,为msg赋值并返回给页面,这时页面显示的就是赋给msg的值。