基于不重新发明轮子的原则,Struts 2并没有开发新的AJAX框架,而是使用时下Java EE平台中比较流行的AJAX框架——Dojo和DWR。
最近在Musachy Barroso等同志的无私奉献下,开发了Struts 2的JSON插件(Plugin),极大地方便了我们输出JSON结果(Result)。这些框架都是开发Ajax不错的选择,项目中可选择使用。
一、使用Ajax主题提交表单
1、JSP页面
- <s:head theme="ajax"/>
- <div id="show">原始内容</div>
- <s:form action="deal" method="post" theme="ajax">
- <s:datetimepicker name="chooseDate" label="日期选择"/>
- <s:submit targets="show" loadingText="请等待,提交中..."/>
- </s:form>
<s:head theme="ajax"/> <div id="show">原始内容<s:form action="deal" method="post" theme="ajax"> <s:datetimepicker name="chooseDate" label="日期选择"/> <s:submit targets="show" loadingText="请等待,提交中..."/> </s:form>
2、Action配置
- <action name="deal" class="com.xy.struts2.DealWithParams">
- <!-- 使用Ajax主题提交后必须返回一个视图 -->
- <result name="success">/AjaxResult.jsp</result>
- </action>
<action name="deal" class="com.xy.struts2.DealWithParams"> <!-- 使用Ajax主题提交后必须返回一个视图 --> <result name="success">/AjaxResult.jsp</result> </action>
3、AjaxResult
- <%@ page language="java" pageEncoding="GBK"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- //设置页面不缓存
- request.setAttribute("decorator","none");
- response.setHeader("Cache-Control","no-cache");
- response.setHeader("Pragma","no-cache");
- response.setDateHeader("Expires",0);
- %>
- <!-- 下面是Action返回页面的数据 -->
- <s:property value="chooseDate"/>
- <s:property value="msg"/>
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% //设置页面不缓存 request.setAttribute("decorator","none"); response.setHeader("Cache-Control","no-cache"); response.setHeader("Pragma","no-cache"); response.setDateHeader("Expires",0); %> <!-- 下面是Action返回页面的数据 --> <s:property value="chooseDate"/> <s:property value="msg"/>
4、Action
代码略。

评论加载中....