Ajax请求之后台处理

Dazzlingwinter 2010-04-15 08:12:06
项目中需要用到一些Ajax来做一些异步的请求处理,主要是多级联动,后台的代码怎么写效率更高一些呢?
后台框架SpringMVC+Hibernate

//web:
//AjaxSelectRequestController
public class AjaxSelectRequestControllerimplements Controller{
private AjasSelectRequestService ajaxselectrequestservice;
public void setAjaxselectrequestservicee(AjasSelectRequestService ajaxselectrequestservice) {
this.ajaxselectrequestservice = ajaxselectrequestservice;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("UTF-8");
String type = request.getParameter("type");
String id = request.getParameter("id");
String xml_start = "<selects>";
String xml = ajaxselectrequestservice.getXmlByTypeAndIDService(type, id);
String xml_end = "</selects>";

String last_xml = xml_start + xml + xml_end;
response.getWriter().write(last_xml);
return null;
}
}
//service
//方案一:
//AjasSelectRequestServiceImpl
public AjasSelectRequestServiceImpl implements AjasSelectRequestService {
private AjaxSelectRequestDAO ajaxrequestdao;
public String getXmlByTypeAndIDService(String type, String id){
if(Strutils.isStringNotNull(type) && Strutils.isStringNotNull(id)) {
if("province".equals(type)) {
AjasSelectRequestServiceImpl
public AjasSelectRequestServiceImpl implements AjasSelectRequestService {
private AjaxSelectRequestDAO ajaxrequestdao;
public String getXmlByTypeAndIDService(String type, String id){
if(Strutils.isStringNotNull(type) && Strutils.isStringNotNull(id)) {
if("province".equals(type)) {
ajaxrequestdao...
}
if("city".equals(type)) {
ajaxrequestdao ...
}
if("area".equals(type)) {
ajaxrequestdao...
}
if("school".equals(type)) {
ajaxrequestdao ...
}
if("college".equals(type)) {
ajaxrequestdao...
}
if("major".equals(type)) {
ajaxrequestdao...
}
if("class".equals(type)) {
ajaxrequestdao...
}
...
}
}
}
//方案二:
AjasSelectRequestServiceImpl
public AjasSelectRequestServiceImpl implements AjasSelectRequestService {
private ProvinceManageDAO provincemanagedao;
private CityManageDAO citymanagedao;
private AreaManageDAO areamanagedao;
private SchoolManageDAO schoolmanagedao;
private CollegeManageDAO collegemanagedao;
private MajorManageDAO majormanagedao;
private ClassManageDAO classmanagedao;
public String getXmlByTypeAndIDService(String type, String id){
if(Strutils.isStringNotNull(type) && Strutils.isStringNotNull(id)) {
if("province".equals(type)) {
provincemanagedao ...
}
if("city".equals(type)) {
citymanagedao...
}
if("area".equals(type)) {
areamanagedao...
}
if("school".equals(type)) {
schoolmanagedao...
}
if("college".equals(type)) {
collegemanagedao...
}
if("major".equals(type)) {
majormanagedao...
}
if("class".equals(type)) {
classmanagedao...
}
...
}
}
}

方案一用AjaxSelectRequestDAO为AjaxSelectRequestServiceImpl提供数据访问,并且实现了依据请求类型request和请求id对不同数据库表的访问,并返回对应的List;
方案二用AjaxSelectRequestServiceImpl持有了各类请求的DAO数据库访问各自的一个对象,通过不同的对象为提供不同的数据库访问服务;
个人的一些看法:
方案一仅持有一个数据库访问层的访问对象,对于频繁的Ajax访问请求,处理时创建的对象少一些,至少创建的DAO引用少,但是他破坏了数据访问的底层完整性,也就是说,数据库不同类型的访问应有不同的DAO来处理
方案二保持了数据库底层访问的访问完整性,但对于频繁Ajax处理,会力不从心,虽然Spring会做好持有对象的创建工作,可是对于仅仅一个Ajax请求,就创建了N多个DAO的引用,用完就被废弃,如果不及时垃圾回收,down掉都有可能
彰显智慧火花,说出你的想法,大家共分享,齐进步~~~
...全文
388 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dazzlingwinter 2010-04-16
  • 打赏
  • 举报
回复
web区冷淡呀。。。
hoojo 2010-04-15
  • 打赏
  • 举报
回复

最好返回的是json的字符串类型,xml占用带宽要多些。
soulx 2010-04-15
  • 打赏
  • 举报
回复
一般做系统还是用第一种吧,扩展方便符合设计模式,但如果service是单例的话需要做同步。

不过对于系统不大或者说不可能加入新的、对象也不多,第二种也可以用。

看具体情况了。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧