| |
基于J2EE的电子商务网站实例解析 |
|
时间: 2006-05-24 来自:天极开发 |
 |
|
3.
定制后台Action
根据MVC的精神,View和Model设计好之后应该是将开发重点转移到控制器的开发上。控制器是根据用户行为进行响应的处理模块,比如用户通过首页的搜索条对服务信息进行检索,这时,web服务中的SearchToTradeEntityAction(对应SearchToTradeEntityAction.java文件)会对用户这一动作进行处理。以下对这一Action进行详细分析:
package com.routease.action.totradeentity; import
java.util.Collection; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
org.apache.commons.beanutils.PropertyUtils; import
org.apache.commons.lang.StringUtils; import
org.apache.struts.action.ActionForm; import
org.apache.struts.action.ActionForward; import
org.apache.struts.action.ActionMapping; import
com.routease.action.PagingAction; import
com.routease.action.helper.UserHelper; import
com.routease.db.dao.DataSource; import
com.routease.db.dao.totradeentity.SearchingCriteria; import
com.routease.db.dao.totradeentity.ToTradeEntityDAO; import
com.routease.db.util.Constants; import
com.routease.db.util.Page; public class SearchToTradeEntityAction
extends PagingAction { public SearchToTradeEntityAction()
{ super(); } //
executeWithDataSource方法为该Action默认执行的方法 public ActionForward
executeWithDataSource(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response, DataSource ds)
throws Exception { //首先接受用户提交的表单数据 String objective = (String)
PropertyUtils.getSimpleProperty(actionForm, "objective"); String
keyWords = (String) PropertyUtils.getSimpleProperty(actionForm,
"keyWords"); String keyWordsRange = (String)
PropertyUtils.getSimpleProperty(actionForm, "keyWordsRange"); if
(StringUtils.isEmpty(keyWordsRange)) { keyWordsRange =
SearchingCriteria.KEY_WORDS_RANGE_NAME; } String industryLevel1
= (String) PropertyUtils.getSimpleProperty(actionForm,
"industryLevel1"); String industryLevel2 = (String)
PropertyUtils.getSimpleProperty(actionForm, "industryLevel2"); String
startingPrice = (String) PropertyUtils.getSimpleProperty(actionForm,
"startingPrice"); String endingPrice = (String)
PropertyUtils.getSimpleProperty(actionForm, "endingPrice"); String
city = (String) PropertyUtils.getSimpleProperty(actionForm,
"city"); String province = (String)
PropertyUtils.getSimpleProperty(actionForm, "province"); String
startNoStr = (String) PropertyUtils.getSimpleProperty(actionForm,
"startNumber"); String lengthStr = (String)
PropertyUtils.getSimpleProperty(actionForm, "length"); if
(StringUtils.isEmpty(startNoStr)) { startNoStr =
"1"; } //根据用户提交的数据,创建查询表达式对象SC int startNumber =
Integer.parseInt(startNoStr); int length =
UserHelper.getPagingLength(ds, request); ToTradeEntityDAO serviceDAO
= new ToTradeEntityDAO(ds); SearchingCriteria sc = new
SearchingCriteria(); sc.setCity(city); sc.setProvince(province); sc.setEndingPrice(endingPrice); sc.setIndustryLevel1(industryLevel1); sc.setIndustryLevel2(industryLevel2); sc.setKeyWords(keyWords); sc.setKeyWordsRange(keyWordsRange); sc.setObjective(objective); sc.setStartingPrice(startingPrice); if
(Constants.IS_TEST) { System.out.println("start of page:" +
startNumber); } //提交查询对象SC,并获得查询结果集,将其结果集放入Request对象中,便于返回 Page
result = serviceDAO.searchToTradeEntities(sc, startNumber,
length); Collection industries =
serviceDAO.findIndustryDistribution(sc); result.setSizePerPage(length); request.setAttribute(Constants.TO_TRADE_ENTITY,
result); request.setAttribute("MY_INDUSTRIES",industries); request.setAttribute("MY_PAGE",
result); //业务逻辑处理完毕之后,返回成功页面 return
actionMapping.findForward("SUCCESS_PAGE"); } } | SearchToTradeEntityAction是一个典型的Action,由前面注解不难看出,一般Action分为三部分:
a.
接受用户表单数据
b. 处理用户表单数据
c. 返回处理结果及页面
4.
修改页面为JSP文件
凡是涉及到与用户状态相关的页面均应改造为动态页面(JSP文件),改造是在前面静态文件的基础上进行的,用服务器端返回的数据(存放在Request对象里)替换静态文本,由于这部分相对技术性不强,所以不再详细赘述了。
通过前面四部分的介绍,基本概述了如易网技术实施的主要过程,在下面的一章里介绍网站技术中的几个重要技巧。
|
|
|
|
|
|
|
|