开始构建你的第一个Appengine应用 (一)

很早的时候我就在google appengine 上面申请过一个 账号, 一直都没将它用起来觉得非常可惜。在国庆之际,特意为这事申请了个域名,以便在这上面开发一个自己的应用。

好了,根据我自己以前的熟悉程序,将在这上面开发一个超简单的CMS应用(简直就是一个留言本的demo)。

Okay, 开始了。

1. 准备工作

eclipse去 www.eclipse.org 下载
appengine.google.com可以下载到 App Engine的 SDK和 eclipse的 plugin.

安装了appengine插件的eclipse多了三个新按钮,如下:


[img]http://dl.iteye.com/upload/attachment/153826/d9401257-ce50-3ea6-b7f7-d43cc84e4e28.jpg[/img]


构建技术:maven和ant, 它们分别可以到 maven.apache.org和ant.apache.org下载。


2. 技术准备

是的,在开发应用之前,需要知道自己要用哪些开源组合来架构自己的应用。不可能任何事情都从头开始。 查阅一下Google App Engine的 文档,觉得采用 SSJ的架构组合正合我意. (SSJ, Struts, Spring, JPA). 这是一个典型的小型应用架构方式。

这里面有几点要注意:
尽量用Maven来管理Jar包,这样提交源码的时候,至少Jar包是可以不用提交的。为国家带宽能源节约多做贡献是每个程序员应有的公德。
应用程序的开发一定要从简到繁,否则很容易还没开发完就已经放弃。
不要用自己不懂的技术。

3. 新建一个Appengine的应用,我以GWT一点都不通,所以暂时决定不用它(不过它真的很酷,很难讲我下周会不会学一学。)

[img]http://dl.iteye.com/upload/attachment/153828/f70b43c6-6265-348f-8393-9ebcb15884ec.jpg[/img]

新建好的代码结构如下:

[img]http://dl.iteye.com/upload/attachment/153830/a57aa48f-8f29-3a4c-b98f-7126452a82c6.jpg[/img]

4. 加入SSJ相应的Jar包,新建一个pom.xml文件,加入Struts,Spring等的dependencies. 这里要注意的是我加入maven的原因是我只想用它的管理jar包,并不想用它来打包或是什么的,因为我根本不知道要怎么的结构才能上传到Appengine正确运行。最好的方式是用Appengine eclipse Plugin.

然后写一个build.xml, 加入如下的代码:


<project name="hznvren" default="copyLib" xmlns:artifact="urn:maven-artifact-ant">

<target name="copyLib">

<copy todir="war/WEB-INF/lib/">
<fileset refid="maven.fileset" />
<mapper type="flatten" />
</copy>

</target>

<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
<classpath>
<pathelement location="lib/maven-artifact-ant-2.0.2-dep.jar" />
</classpath>
</typedef>

<artifact:dependencies pathId="maven.classpath" filesetId="maven.fileset">
<artifact:pom file="pom.xml" />
</artifact:dependencies>


</project>



然后进入项目目录,运行一下ant命令,则jar包就会自动下载到你的lib目录。如果它不能工作,则表示你没有下载让ant与maven一起工作的jar包。这一点我在这里不作详细说明。


5. 加入Struts, Spring的配置文件,我们暂定为各Action由Struts进行控制,Action, 以及Control层的Manager对像由Spring负责创建。Persistence 层负责数据的 CRUD.


struts.action.extension=do,jspa,jspx
struts.objectFactory=spring
struts.enable.DynamicMethodInvocation=false
struts.devMode=true
struts.locale=zh_CN
struts.i18n.encoding=GBK
struts.ui.theme=simple


其中struts.objectFactory=spring 表示对像由Spring来创建。



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="default" extends="struts-default">

<!-- default retun type is freemarker -->
<result-types>
<result-type name="freemarker"
class="org.apache.struts2.views.freemarker.FreemarkerResult"
default="true" />
</result-types>

<!-- when validation failed, return show_error.html -->
<global-results>
<result name="input">/WEB-INF/common/show_error.html</result>
</global-results>

</package>

<!-- one admin struts config and one front struts config -->
<include file="com/yourhz/core/struts-default.xml" />
<include file="com/yourhz/core/struts-front.xml" />

</struts>




<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">


<!--
Use annotation auto register beans,and check @Required,@Autowired
-->
<context:component-scan base-package="com.yourhz" />

</beans>



这一段表示所有的Beans都由Spring 自动扫进来,而不是一个个来定义。


定义web.xml


<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/yourhz/core/applicationContext.xml,
classpath:applicationContext.xml
</param-value>
</context-param>


<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>*.jspa</url-pattern>
</filter-mapping>

<!-- spring Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--GAE for Struts2 Listener-->
<listener>
<listener-class>com.yourhz.core.GaeStruts2Listener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.do</welcome-file>
</welcome-file-list>


</web-app>


6. 接下来是设计与代码时间, 很晚了,明天再写代码细节吧。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值