ajax、Struts、spring的无缝结合

2016-02-19 15:32 2 1 收藏

下面请跟着图老师小编一起来了解下ajax、Struts、spring的无缝结合,精心挑选的内容希望大家喜欢,不要忘记点个赞哦!

【 tulaoshi.com - Web开发 】

zhipingch 原创

    去年初,正好负责一个医药信息系统的设计开发,架构设计时,采用Struts+JDBC(自定义采用适配器模式封装了HashMap动态VO实现的持久层)。后来ajax热潮兴起,正好系统中有很多地方需要和服务器端交互数据,如采购销售系统中的订单头/订单明细等主从表结构的维护。
    [color=blue]数据交互过程[/color],我们考虑采用xml来组织数据结构,前台封装需要的xml,通过ajax提交---〉action解析xml ---〉改造原有的持久层实现xml持久化;
   持久层根据实际需要返回xml,document对象,---〉action 处理 --〉前台自己封装js库来解析xml,并刷新部分页面。

    ajax:已经有很多方法实现跨浏览器的方式,这里只介绍最简单的方式,同步模式下提交xmlStr给action(*.do)。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)/**  * 将数据同步传递给后台请求url  *  @return 返回xmlhttp 响应的信息  *  @param-url = '/web/module/xxx.do?p1=YY&p2=RR';  *  @param-xmlStr:xml格式的字符串 dataxpath![CDATA[数据信息]]/xpath/data  * @author zhipingch  * @date 2005-03-17  */ function sendData(urlStr, xmlStr) {     var xmlhttp =  ActiveXObject("Microsoft.XMLHTTP");     xmlhttp.open("POST", urlStr, );     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");      (xmlStr) {         xmlhttp.send(xmlStr);     }  {         xmlhttp.send();     }      xmlhttp.responseXml; }



    struts中我们扩展了Action,实现了xmlStr转化成document对象(dom4j),并且完善了转发方式。如
[quote]

     以一个Controller响应一组动作绝对是Controller界的真理,Struts的DispatchAction同样可以做到这点。

[list]
action path="/admin/user" name="userForm" scope="request" parameter="method" validate="false"
    forward name="list" path="/admin/userList.jsp"/
/action
[/list]
    其中parameter="method" 设置了用来指定响应方法名的url参数名为method,即/admin/user.do?method=list 将调用UserAction的public ActionForward list(....) 函数。   

    public ActionForward unspecified(....) 函数可以指定不带method方法时的默认方法。[/quote]
    但是这样需要在url后多传递参数[size=18][color=red]method=list [/color][/size];并且action节点配置中的[color=red]parameter="method" [/color]
也没有被充分利用,反而觉得是累赘!

    因此我们直接在BaseDispatchAction中增加xml字符串解析,并充分利用action节点配置中的[color=red]parameter="targetMethod" [/color],使得转发的时候,action能够直接转发到子类的相应方法中,减少了url参数传递,增强了配置信

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)ramework.dao.DataAccessException;  org.springframework.web.context.support.WebApplicationContextUtils;  com.ufida.haisheng.constants.Globals;  com.ufida.haisheng.converter.DateConverter;  com.ufida.haisheng.converter.TimestampConverter;  com.ufida.haisheng.exp.ExceptionDTO;  com.ufida.haisheng.exp.ExceptionDisplayDTO;  com.ufida.haisheng.exp.exceptionhandler.ExceptionHandlerFactory;  com.ufida.haisheng.exp.exceptionhandler.ExceptionUtil;  com.ufida.haisheng.exp.exceptionhandler.IExceptionHandler;  com.ufida.haisheng.exp.exceptions.BaseAppException;  com.ufida.haisheng.exp.exceptions.MappingConfigException;  com.ufida.haisheng.exp.exceptions.NoSuchBeanConfigException; /**  * 系统的Ajax转发基类。增加模版处理异常信息。  *   * @author 陈志平 chenzp  * @desc BaseDispatchDocumentAction.java  *   * @说明: web 应用基础平台  * @date 2005-03-02 11:18:01 AM  * @版权所有: All Right Reserved 2006-2008  */    BaseDispatchDocumentAction  Action {      Class clazz = .getClass();       Logger log = Logger.getLogger(BaseDispatchDocumentAction.);     /**      * 异常信息      */       ThreadLocalExceptionDisplayDTO 
expDisplayDetails =  ThreadLocalExceptionDisplayDTO();             Long defaultLong = ;       ApplicationContext ctx = ;     /**      * 注册转换的工具类 使得From中的string --      * Model中的对应的类型(Date,BigDecimal,Timestamp,Double...)      */      {     &

, Document., HttpServletRequest.,             HttpServletResponse. };     /**      * Process the specified HTTP request, and create the corresponding HTTP      * response (or forward to another web component that will create it).      * Return an codeActionForward/code instance describing where and how      * control should be forwarded, or codenull/code if the response has      * already been completed.      *       * @param mapping      *            The ActionMapping used to select this instance      * @param form      *            The optional ActionForm bean for this request (if any)      * @param request      *            The HTTP request we are processing      * @param response      *            The HTTP response we are creating      *       * @exception Exception      *                if an exception occurs      */      ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,             HttpServletResponse response)  Exception {         response.setContentType("text/html; charset=UTF-8");      &n

    /**      * 直接输出纯字符串      */       renderText(HttpServletResponse response, String text) {         PrintWriter out = ;          {             out = response.getWriter();             response.setContentType("text/plain;charset=UTF-8");             out.write(text);         }  (IOException e) {             log.error(e);         }  {              (out != ) {                 out.flush();                 out.close();                 out = ;             }         }     }          /**      * 直接输出纯HTML      */       renderHtml(HttpServletResponse response, String text) {         PrintWriter out = ;          {             out = response.getWriter();             response.setContentType("text/html;charset=UTF-8");             out.write(text);         }  (IOException e) {             log.error(e);         }

     ExceptionDisplayDTO handlerException(HttpServletRequest request,HttpServletResponse response, Exception ex) {         ExceptionDisplayDTO expDTO = (ExceptionDisplayDTO) expDisplayDetails.get();          ( == expDTO) {                         expDTO =  ExceptionDisplayDTO(,.getClass().getName());         }         IExceptionHandler expHandler = ExceptionHandlerFactory.getInstance().create();         ExceptionDTO exDto = expHandler.handleException(expDTO.getContext(), ex);         request.setAttribute("ExceptionDTO", exDto);         renderText(response,"[Error:" + (exDto ==  ? "ExceptionDTO is null,请检查expinfo.xml配置文件." : exDto.getMessageCode())                 + "]");          expDTO;     }       isValidMethod(String actionMethod)  MappingConfigException {          (actionMethod ==  || "execute".equals(actionMethod) || "perform".equals(actionMethod)) {             log.error("[BaseDispatchAction-error] parameter = " + actionMethod);             expDisplayDetails.set( ExceptionDisplayDTO(, "MappingConfigException"));               MappingConfigException("对不起,配置的方法名不能为 " + actionMethod);         }     }   /**      * 解析xml流      * @param request      * @return Document对象      */  &n

来源:https://www.tulaoshi.com/n/20160219/1609834.html

延伸阅读
最终效果图 原图 素材图 1 素材图 2 1、打开素材图1。 2、拖入原图和素材图2,把原图放在最上面一层,自己摆放到合适位置。 3、隐藏人物图眼睛,对花图调整色阶,使其鲜艳些,然后混合模式改为叠加。 4、打开人物图层的眼睛,用魔棒工具除去不用的东西,橡皮擦掉生硬地方,...
多数IT 组织都必须解决三个主要问题:1.帮助组织减少成本 2.增加并且保持客户 3.加快业务效率。完成这些问题一般都需要实现对多个业务系统的数据和业务逻辑的无缝访问,也就是说,要实施系统集成工程,以便联结业务流程、实现数据的访问与共享。 JpetStore 4.0是ibatis的最新示例程序,基于Struts MVC框架(注:非传统Struts开发模式...
标签: 电脑入门
在我们的日常生活中,大读书的人通常用的是RJ45的接口网络上网,当然,随着现在网络的飞速发展,Wifi的应用也越来越实用,只需要家中接个无线路由,有笔记本带Wifi的朋友就可以抱着我们的本本,随时随地上网,今天我和大家分享的是当我们插上RJ45的接口后,如何在接入Wifi的办法,这中间其实有个优先级的设置,现在把办法告诉大家。 首先我们...
异步 javascript 和 XML(Asynchronous JavaScript and XML,Ajax)无疑是最流行的新 Web 技术。本文中我们将完全使用 PHP 和 Simple Ajax Toolkit (Sajax) 创建一个简单的相册作为在线 Web 应用程序。我们首先用标准的 PHP 开发方法编写简单的相册,然后再用 Sajax 将其变成活动的 Web 应用程序。 创建一个简单的相册 本文...
标签: Web开发
1.此代码适合所有下拉列表取值 2.一个项目所有的下拉列表只需要这一个公用方法; 步骤一:创建实体bean ; 代码如下: public class DictionaryBean { private String value_Id;//下拉框option的id private String value;//下拉框option的值 private String flag;//对应下拉框的值的类型,如flag=1,下拉列表为省份信息,flag=2为市级...

经验教程

265

收藏

59
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部