技术开发 频道

评点Flex结合J2EE的开发与架构

3 案例介绍

案例是超级简单的,开发Web 应用的,不管你是基于 J2EE,.net 平台还是其他的 PHP 等等都很熟悉分层架构,其中五层是最熟悉不过的了,一般分为表示层,控制层,服务层,模型层和数据层,有的公司干脆就把服务层和模型层合并起来和控制层合并在一起,基于分层的开发可以横向也可以纵向,由于引入 flex 技术,因为并不是所有人都懂得 flex 开发方式,尽管他和传统的 Swing, SWT,GWT开发很相近 ,不过这样需求分析时可以构建可演进的原型系统,界面也很 RIA。

人们常提面向接口编程于是经常看到 IUser user=new User();这样的代码,似乎就是在用接口,但是 new 的动作还是有依赖,于是人们用一些设计模式比如 Factory, Proxy,反射等等模式,后来又有了更好的依赖注入,似乎很完美了,可人们又陷进了过度设计,其实接口应用比直接用原类要多耗费几个机械指令 User user=new User(),而且在一个内聚的组件中尽量用对象直接连接,该分开的时候我们去用接口分,接口的目的是为了抽象,抽象的目的是为了解耦。

本开发为了把模型层和服务层完全分开,引入接口工程,也就是单独开发一个工程来设计接口,这个工程有设计师开发。

用户接口: 

package org.blogjava.model;

/** *//**

*@authorJack

*

*/

publicinterface IUser {

long getId();

void setId(long id);

void setUserName(String name);

String getUserName();

void setPassword(String password);

String getPassword();

void setEmail(String email);

String getEmail();

void setAddress(String address);

String getAddress();

}


工厂接口: 

package org.blogjava.dao;

import java.util.List;

import org.blogjava.model.IUser;

import org.blogjava.util.InstanceUtils;

import org.blogjava.util.PropertiesUtils;

/** *//**

*@authorJack

*

*/

publicinterface DAOFactory {

publicfinal String IMPL_NAME = PropertiesUtils.getParam(

"/DaoFactoryImpl.properties", "DAOFactoryImpl");

publicfinal DAOFactory factory = (DAOFactory) InstanceUtils

.newInstance(IMPL_NAME);

boolean checkUserValid(String userName, String password);

IUser getUserById(String id);

IUser createUser(IUser user);

boolean deleteUserById(String id);

boolean updateUser(IUser user);

List<IUser> getAllUsers();

}
这两个类在单独的工程里开发,以便于模型和服务层同步开发。

在服务层不能对外暴露 IUser 接口,而他也要接受数据,这里用的是 UserVO 对象来封装数据,服务层不操作模型层的任何实现类,这样这两层之间完全解耦。 

package org.blogjava.vo;

/** *//**

*@authorJack

*

*/

publicclass UserVO {

publiclongid;

public String userName;

public String password;

public String email;

public String address;

public UserVO() {

}

}

Flex 开发和 HTML开发有很大的不同,客户端和服务端交互的不是 HTML 而是 XML,所以在服务器端的 Servlet 就应该输出 XML 流到客户端

判断登录: 

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

String userName = request.getParameter("userName");

String password = request.getParameter("password");

String sucess = "<?xml version=""1.0"" encoding=""utf-8""?><result><msg>sucess</msg></result>";

String unsucess = "<?xml version=""1.0"" encoding=""utf-8""?><result><msg>unsucess</msg></result>";

response.setContentType("text/xml");

try {

if (userName == null || "".equals(userName)) {

response.getOutputStream().write(unsucess.getBytes());

returnnull;

}

if (password == null || "".equals(password)) {

response.getOutputStream().write(unsucess.getBytes());

returnnull;

}

IUserService service = new IUserServiceImpl();

if (service.isUserValid(userName, password)) {

response.getOutputStream().write(sucess.getBytes());

} else {

response.getOutputStream().write(unsucess.getBytes());

}

} catch (IOException e) {

e.printStackTrace();

}

returnnull;

}


得到所有的用户: 

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

IUserService service = new IUserServiceImpl();

List<UserVO> users = service.getAllUsers();

try {

StringBuffer returnStr = new StringBuffer(

"<?xml version=""1.0"" encoding=""utf-8""?>");

returnStr.append("<users>");

for (UserVO user : users) {

returnStr.append("<user>");

returnStr.append("<id>" + user.id + "</id>");

returnStr.append("<userName>" + user.userName + "</userName>");

returnStr.append("<password>" + user.password + "</password>");

returnStr.append("<email>" + user.email + "</email>");

returnStr.append("<address>" + user.address + "</address>");

returnStr.append("</user>");

}

returnStr.append("</users>");

response.getOutputStream().write(returnStr.toString().getBytes());

System.out.println(returnStr.toString());

} catch (IOException e) {

e.printStackTrace();

}

returnnull;

}


最后 Flex 也是一个单独的工程,通过 HTTPService 和 Web 服务器端交互数据
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[0.72, 1.0]" backgroundGradientColors="[#FFFFFF, #FFFFFF]">

<mx:HTTPService
showBusyCursor="true"
id="loginSrv"
result="doResult();"
method="GET"
url="http://localhost:8080/UserManager/checkUser.do">
<mx:request>
<userName>
{txtname.text}
</userName>
<password>
{txtpwd.text}
</password>
</mx:request>
</mx:HTTPService>
<mx:HTTPService
showBusyCursor="true"
id="loadUsers"
method="GET" result="users = event.result.users.user"
url="http://localhost:8080/UserManager/loadAllUsers.do">
</mx:HTTPService>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.utils.ObjectUtil;
import mx.collections.ArrayCollection;

[Bindable]
var users:ArrayCollection=new ArrayCollection();
internal function doResult():void
{
var returnValue:String=loginSrv.lastResult.result.msg;
if(returnValue=="sucess")
{
login.visible=false;
loadUsers.send();
userList.visible=true;
}
else
{
Alert.show("登录失败","提示信息",Alert.OK,this,null,null,Alert.YES);
}
}
private function index_sortCompareFunc(itemA:Object, itemB:Object):int {
// Make sure itemA has an "index" property.
if (!itemA.hasOwnProperty("id")) {
itemA.index = null;
}
// Make sure itemB has an "index" property.
if (!itemB.hasOwnProperty("id")) {
itemB.index = null;
}
// Perform a numeric sort.
return ObjectUtil.numericCompare(itemA.index, itemB.index);
}
]]>
</mx:Script>
<mx:Panel id="login" x="285.5" y="127" width="341" height="238" layout="absolute" fontFamily="Arial" fontSize="13" color="#3C280B" borderStyle="none" borderColor="#CC0B0B" cornerRadius="10" alpha="0.64">
<mx:TextInput id="txtname" x="101" y="34" width="172" displayAsPassword="true" editable="true" enabled="true"/>
<mx:Button x="92" y="130" label="登录" labelPlacement="top" enabled="true" click="loginSrv.send();"/>
<mx:Button x="183" y="130" label="取消"/>
<mx:TextInput id="txtpwd" x="101" y="75" width="172" displayAsPassword="true" editable="true" enabled="true"/>
<mx:Label x="44" y="36" text="帐号:" width="49" height="20" enabled="true"/>
<mx:Label x="44" y="77" text="密码:" width="49" height="20" enabled="true"/>
</mx:Panel>
<mx:Panel id="userList" x="181.5" y="98" width="576" height="311" layout="absolute" visible="false">
<mx:DataGrid x="0" y="0" width="556" height="271" sortableColumns="true" dataProvider="{users}" editable="true" enabled="true">
<mx:columns>
<mx:DataGridColumn headerText="编号" dataField="id" />
<mx:DataGridColumn headerText="姓名" dataField="userName" />
<mx:DataGridColumn headerText="密码" dataField="password"/>
<mx:DataGridColumn headerText="邮件" dataField="email"/>
<mx:DataGridColumn headerText="地址" dataField="address"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</mx:Application>


4 小结

在写这篇文章的时候已经是深夜了,可能有很多地方表述有问题,也可能有的思想没有表述清楚,欢迎大家讨论,就先写到这里,有时间我会继续补上一些内容。

0
相关文章