`
carvin
  • 浏览: 209638 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

google sitebricks 学习心得--最简单web应用

阅读更多
简介

Sitebricks 目前仍然处于测试阶段,它是一种新的 Java™ Web 应用程序框架。您可能想问:“为什么我还需要另一个 Web 框架?” 通过 Google Sitebricks,您可以快速构建一个可由他人维护或操作的 Web 应用程序。Sitebricks 构建于 Guice 之上。它将许多 Guice 原则扩展到 Web。它像 Guice 一样大量使用注释将配置作为代码的一部分。要使用 Guice,不需要创建或编辑大量 XML 文件。而 Sitebricks 允许在创建 Web 应用程序时编写相对较少的代码。您编写的代码将会简单明了。可以查看 Sitebricks 代码并快速了解其表达的含义。Sitebricks 不会破坏类型安全或性能。

先决条件

Sitebricks 仍然处于测试阶段;本文使用的是 0.8.2 版本。
目前,使用 Sitebricks 最简单的方式就是随 Maven 一同使用。本文使用的是 Maven 3.0.1。当然,Maven 会引入 Sitebricks 需要的所有其他依赖项,比如 Google Guice。
依赖于 Java 中的一些高级特性,因此需要用到 JDK 1.6。本文使用的是 JDK 1.6.0_5。
您可以使用一个 Java Web 应用服务器来部署本文中的样例应用程序,不过这并非必需步骤。
此外,样例应用程序包含一个可用作测试的嵌入式 Jetty 服务器。
在样例应用程序中用到了由 Sitebricks 使用的 Guice。熟悉一下 Guice 或 Spring 等其他依赖注入框架会很有用。

使用maven创建项目

#mvn archetype:generate 


简单web项目应该是选择100

项目的POM文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>vmars</groupId>
	<artifactId>vmars</artifactId>
	<packaging>war</packaging>
	<version>1.0</version>
	<name>vmars Maven Webapp</name>
	<url>http://maven.apache.org</url>
         <repositories>
<!-- 如果搭建了maven私服,可以在私服上添加一个repository
     就可以不用以下的repository配置了
-->

        <repository> 
            <id>sonatype-google-snapshots</id> 
            <name>Sonatype Google Snapshots Repository</name>
            <url>http://oss.sonatype.org/content/repositories/
google-snapshots/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.google.sitebricks</groupId>
			<artifactId>sitebricks</artifactId>
			<version>0.8.2</version>
		</dependency>
			</dependencies>
	<build>
		<finalName>vmars</finalName>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<configuration>
					<wtpmanifest>true</wtpmanifest>
					<wtpapplicationxml>true</wtpapplicationxml>
					<wtpversion>2.0</wtpversion>
					<ajdtVersion>none</ajdtVersion>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


生成eclipse项目
#mvn eclipse:eclipse

maven 会把依赖的jar下载下来

配置web.xml文件
<web-app>
	<display-name>Archetype Created Web Application</display-name>
	<filter>
		<filter-name>webFilter</filter-name>
		<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>webFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<listener-class>org.vmars.framework.web.MyGuiceCreator</listener-class>
	</listener>
</web-app>

看到在web.xml中需要一个自己的listener,这个listener的作用是在容器启动时配置sitebricks。

写listener
public class MyGuiceCreator extends GuiceServletContextListener {
	@Override
	protected Injector getInjector() {
		return Guice.createInjector(new SitebricksModule() {
			 protected void configureSitebricks() { 
				 scan(Example.class.getPackage());    //scan class Example's package and all descendants
			 }
		});
	}
}

示例使用 scan,这是 Sitebricks 所特有的一个 API。它接收一个 Java 包并将其内容添加到由 Sitebricks 管理的类中。这的Example是一个处理请求的类。

写Example
@At("/example") //请求的路径
@Show("/pages/example.html")
//处理请求指向的页面,如果没有不写此标签。就需要在webapp目录下或者WEB-INF目录下创建一个Example.html文件。
public class Example {
	private boolean appear = false;
	private String message = "Hello";
	
	public String getMessage() {
		return message;
	}
	
	public boolean isAppear() {
		return appear;
	}
	public void setAppear(boolean appear) {
		this.appear = appear;
	}
}


写Example.html
<!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>Insert title here</title>
</head>
<body>
<a href='?appear=${!appear}'>show/hide</a>
@ShowIf(appear)
<span>
 ${message} from Sitebricks!
</span>
</body>
</html>


至此一个最简单例子就完成了。

运行示例
#mvn jetty:run

然后在浏览器中输入
http://localhost:8080/项目名称/example
就可以访问测试了。

参考:
http://www.ibm.com/developerworks/cn/web/wa-sitebricks/index.html?ca=drs-
http://code.google.com/p/google-sitebricks/wiki/GettingStarted
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics