Appfuse框架中的Ajax

本文介绍在Appfuse框架中实现Ajax请求的方法,包括如何避免Sitemesh的默认封装以确保响应文本为简单的文本或XML格式,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Appfuse中使用了Sitemesh模块对请求进行封装,默认配置一般为:
<sitemesh>
    <property name="decorators-file" value="/WEB-INF/decorators.xml"/>
    <excludes file="${decorators-file}"/>
    <page-parsers>
        <parser default="true" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
        <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
        <parser content-type="text/html;charset=UTF-8" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
    </page-parsers>
    <decorator-mappers>
        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}"/>
        </mapper>
    </decorator-mappers>
</sitemesh>

Ajax请求一般要求responseText为简单文本或者XML文本。数据量较少时采用无格式简单文本;如果数据量比较大,通常采用XML数据。
后一种情况下,需要在处理Ajax请求的Action中,设置
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
然后创建输出流对象并response数据:
PrintWriter out = response.getWriter();
out.println("<?xml version='1.0' encoding='UTF-8'?>");
out.println("<root>");
out.println("<result>");
......
out.close();
如果是在Appfuse框架下使用的Ajax,就要避免response被Sitemesh封装起来,变成了HTML文本(表现在浏览器端,应该会得到Sitemesh的修饰文件,内嵌了response文本)
解决方法是修改decorators.xml中的配置将处理Ajax的Action排除到默认修饰以外。
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值