⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javaeye技术网站.htm

📁 几个运用了DWR框架编写的AJAX代码,
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<BR>import org.springframework.beans.BeansException; <BR>import 
org.springframework.context.ApplicationContext; <BR>import 
org.springframework.dao.DataAccessException; <BR>import 
org.springframework.web.context.support.WebApplicationContextUtils; 
<BR><BR>import com.ufida.haisheng.constants.Globals; <BR>import 
com.ufida.haisheng.converter.DateConverter; <BR>import 
com.ufida.haisheng.converter.TimestampConverter; <BR>import 
com.ufida.haisheng.exp.ExceptionDTO; <BR>import 
com.ufida.haisheng.exp.ExceptionDisplayDTO; <BR>import 
com.ufida.haisheng.exp.exceptionhandler.ExceptionHandlerFactory; <BR>import 
com.ufida.haisheng.exp.exceptionhandler.ExceptionUtil; <BR>import 
com.ufida.haisheng.exp.exceptionhandler.IExceptionHandler; <BR>import 
com.ufida.haisheng.exp.exceptions.BaseAppException; <BR>import 
com.ufida.haisheng.exp.exceptions.MappingConfigException; <BR>import 
com.ufida.haisheng.exp.exceptions.NoSuchBeanConfigException; <BR><BR>/** <BR>* 
系统的Ajax转发基类。增加模版处理异常信息。 <BR>* <BR>* @author <BR>* @desc 
BaseDispatchDocumentAction.java <BR>* <BR>* @说明: web 应用基础平台 <BR>* @date 
2005-03-02 11:18:01 AM <BR>* @版权所有: All Right Reserved 2006-2008 <BR>*/ 
<BR>public abstract class BaseDispatchDocumentAction extends Action { 
<BR><BR>protected Class clazz = this.getClass(); <BR><BR>protected static Logger 
log = Logger.getLogger(BaseDispatchDocumentAction.class); <BR><BR>/** <BR>* 异常信息 
<BR>*/ <BR>protected static ThreadLocal&lt;ExceptionDisplayDTO&gt; 
expDisplayDetails = new ThreadLocal&lt;ExceptionDisplayDTO&gt;(); 
<BR><BR>private static final Long defaultLong = null; <BR><BR>private static 
ApplicationContext ctx = null; <BR>/** <BR>* 注册转换的工具类 使得From中的string -- <BR>* 
Model中的对应的类型(Date,BigDecimal,Timestamp,Double...) <BR>*/ <BR>static { 
<BR>ConvertUtils.register(new ClassConverter(), Double.class); 
<BR>ConvertUtils.register(new DateConverter(), Date.class); 
<BR>ConvertUtils.register(new DateConverter(), String.class); 
<BR>ConvertUtils.register(new LongConverter(defaultLong), Long.class); 
<BR>ConvertUtils.register(new IntegerConverter(defaultLong), Integer.class); 
<BR>ConvertUtils.register(new TimestampConverter(), Timestamp.class); 
<BR>ConvertUtils.register(new BigDecimalConverter(defaultLong), 
BigDecimal.class); <BR>} <BR><BR>/** <BR>* The message resources for this 
package. <BR>*/ <BR>protected static MessageResources messages = 
MessageResources.getMessageResources("org.apache.struts.actions.LocalStrings"); 
<BR><BR>/** <BR>* The set of Method objects we have introspected for this class, 
keyed by <BR>* method name. This collection is populated as different methods 
are <BR>* called, so that introspection needs to occur only once per method 
name. <BR>*/ <BR>protected HashMap&lt;String, Method&gt; methods = new 
HashMap&lt;String, Method&gt;(); <BR><BR>/** <BR>* The set of argument type 
classes for the reflected method call. These are <BR>* the same for all calls, 
so calculate them only once. <BR>*/ <BR>protected Class[] types = { 
ActionMapping.class, ActionForm.class, Document.class, HttpServletRequest.class, 
<BR>HttpServletResponse.class }; <BR><BR>/** <BR>* Process the specified HTTP 
request, and create the corresponding HTTP <BR>* response (or forward to another 
web component that will create it). <BR>* Return an 
&lt;code&gt;ActionForward&lt;/code&gt; instance describing where and how <BR>* 
control should be forwarded, or &lt;code&gt;null&lt;/code&gt; if the response 
has <BR>* already been completed. <BR>* <BR>* @param mapping <BR>* The 
ActionMapping used to select this instance <BR>* @param form <BR>* The optional 
ActionForm bean for this request (if any) <BR>* @param request <BR>* The HTTP 
request we are processing <BR>* @param response <BR>* The HTTP response we are 
creating <BR>* <BR>* @exception Exception <BR>* if an exception occurs <BR>*/ 
<BR>public ActionForward execute(ActionMapping mapping, ActionForm form, 
HttpServletRequest request, <BR>HttpServletResponse response) throws Exception { 
<BR>response.setContentType("text/html; charset=UTF-8"); <BR>ExceptionDisplayDTO 
expDTO = null; <BR>try { <BR>Document doc = createDocumentFromRequest(request); 
<BR>/* <BR>* 这里直接调用mapping的parameter配置 <BR>*/ <BR>String actionMethod = 
mapping.getParameter(); <BR>/* <BR>* 校验配置的方法是否正确、有效 <BR>*/ 
<BR>isValidMethod(actionMethod); <BR><BR>return dispatchMethod(mapping, form, 
doc, request, response, actionMethod); <BR>} catch (BaseAppException ex) { 
<BR>expDTO = handlerException(request, response, ex); <BR>} catch (Exception ex) 
{ <BR>ExceptionUtil.logException(this.getClass(), ex); 
<BR>renderText(response,"[Error :对不起,系统出现错误了,请向管理员报告以下异常信息.\n" + ex.getMessage() 
+ "]"); <BR>request.setAttribute(Globals.ERRORMSG, 
"对不起,系统出现错误了,请向管理员报告以下异常信息.\n" + ex.getMessage()); <BR>expDTO = 
handlerException(request,response, ex); <BR>} finally { 
<BR>expDisplayDetails.set(null); <BR>} <BR>return null == expDTO ? null : 
(expDTO.getActionForwardName() == null ? null : 
mapping.findForward(expDTO.getActionForwardName())); <BR>} <BR><BR>/** <BR>* 
直接输出纯字符串 <BR>*/ <BR>public void renderText(HttpServletResponse response, String 
text) { <BR>PrintWriter out = null; <BR>try { <BR>out = response.getWriter(); 
<BR>response.setContentType("text/plain;charset=UTF-8"); <BR>out.write(text); 
<BR>} catch (IOException e) { <BR>log.error(e); <BR>} finally { <BR>if (out != 
null) { <BR>out.flush(); <BR>out.close(); <BR>out = null; <BR>} <BR>} <BR>} 
<BR><BR>/** <BR>* 直接输出纯HTML <BR>*/ <BR>public void 
renderHtml(HttpServletResponse response, String text) { <BR>PrintWriter out = 
null; <BR>try { <BR>out = response.getWriter(); 
<BR>response.setContentType("text/html;charset=UTF-8"); <BR>out.write(text); 
<BR>} catch (IOException e) { <BR>log.error(e); <BR>} finally { <BR>if (out != 
null) { <BR>out.flush(); <BR>out.close(); <BR>out = null; <BR>} <BR>} <BR>} 
<BR><BR>/** <BR>* 直接输出纯XML <BR>*/ <BR>public void renderXML(HttpServletResponse 
response, String text) { <BR>PrintWriter out = null; <BR>try { <BR>out = 
response.getWriter(); <BR>response.setContentType("text/xml;charset=UTF-8"); 
<BR>out.write(text); <BR>} catch (IOException e) { <BR>log.error(e); <BR>} 
finally { <BR>if (out != null) { <BR>out.flush(); <BR>out.close(); <BR>out = 
null; <BR>} <BR>} <BR>} <BR><BR>/** <BR>* 异常处理 <BR>* @param request <BR>* @param 
out <BR>* @param ex <BR>* @return ExceptionDisplayDTO异常描述对象 <BR>*/ <BR>private 
ExceptionDisplayDTO handlerException(HttpServletRequest 
request,HttpServletResponse response, Exception ex) { <BR>ExceptionDisplayDTO 
expDTO = (ExceptionDisplayDTO) expDisplayDetails.get(); <BR>if (null == expDTO) 
{ <BR>expDTO = new ExceptionDisplayDTO(null,this.getClass().getName()); <BR>} 
<BR>IExceptionHandler expHandler = 
ExceptionHandlerFactory.getInstance().create(); <BR>ExceptionDTO exDto = 
expHandler.handleException(expDTO.getContext(), ex); 
<BR>request.setAttribute("ExceptionDTO", exDto); 
<BR>renderText(response,"[Error:" + (exDto == null ? "ExceptionDTO is 
null,请检查expinfo.xml配置文件." : exDto.getMessageCode()) <BR>+ "]"); <BR>return 
expDTO; <BR>} <BR><BR>private void isValidMethod(String actionMethod) throws 
MappingConfigException { <BR>if (actionMethod == null || 
"execute".equals(actionMethod) || "perform".equals(actionMethod)) { 
<BR>log.error("[BaseDispatchAction-&gt;error] parameter = " + actionMethod); 
<BR>expDisplayDetails.set(new ExceptionDisplayDTO(null, 
"MappingConfigException")); <BR>throw new MappingConfigException("对不起,配置的方法名不能为 
" + actionMethod); <BR>} <BR>} <BR><BR>/** <BR>* 解析xml流 <BR>* @param request 
<BR>* @return Document对象 <BR>*/ <BR>protected static Document 
createDocumentFromRequest(HttpServletRequest request) throws Exception { <BR>try 
{ <BR>request.setCharacterEncoding("UTF-8"); <BR>Document document = null; 
<BR>SAXReader reader = new SAXReader(); <BR>document = 
reader.read(request.getInputStream()); <BR>return document; <BR>} catch 
(Exception ex) { <BR>log.warn("TIPS:没有提交获取XML格式数据流! "); <BR>return null; <BR>} 
<BR>} <BR><BR>/** <BR>* Dispatch to the specified method. <BR>* <BR>* @since 
Struts 1.1 <BR>*/ <BR>protected ActionForward dispatchMethod(ActionMapping 
mapping, ActionForm form, Document doc,HttpServletRequest request, 
HttpServletResponse response, String name) throws Exception { <BR>Method method 
= null; <BR>try { <BR>method = getMethod(name); <BR>} catch 
(NoSuchMethodException e) { <BR>String message = 
messages.getMessage("dispatch.method", mapping.getPath(), name); 
<BR>log.error(message, e); <BR>expDisplayDetails.set(new 
ExceptionDisplayDTO(null, "MappingConfigException")); <BR>throw new 
MappingConfigException(message, e); <BR>} <BR><BR>ActionForward forward = null; 
<BR>try { <BR>Object args[] = { mapping, form, doc, request, response }; 
<BR>log.debug("[execute-begin] -&gt; " + mapping.getPath() + "-&gt;[" + 
clazz.getName() + "-&gt;" + name + "]"); <BR>forward = (ActionForward) 
method.invoke(this, args); <BR>log.debug(" [execute-end] -&gt; " + (null == 
forward ? "use ajax send to html/htm" : forward.getPath())); <BR>} catch 
(ClassCastException e) { <BR>String message = 
messages.getMessage("dispatch.return", mapping.getPath(), name); 
<BR>log.error(message, e); <BR>throw new BaseAppException(message, e); <BR><BR>} 
catch (IllegalAccessException e) { <BR>String message = 
messages.getMessage("dispatch.error", mapping.getPath(), name); 
<BR>log.error(message, e); <BR>throw new BaseAppException(message, e); <BR><BR>} 
catch (InvocationTargetException e) { <BR>Throwable t = e.getTargetException(); 
<BR>String message = messages.getMessage("dispatch.error", mapping.getPath(), 
name); <BR>throw new BaseAppException(message, t); <BR>} <BR><BR>return 
(forward); <BR>} <BR><BR>/** <BR>* Introspect the current class to identify a 
method of the specified name <BR>* that accepts the same parameter types as the 
&lt;code&gt;execute&lt;/code&gt; <BR>* method does. <BR>* <BR>* @param name 
<BR>* Name of the method to be introspected <BR>* <BR>* @exception 
NoSuchMethodException <BR>* if no such method can be found <BR>*/ <BR>protected 
Method getMethod(String name) throws NoSuchMethodException { <BR>synchronized 
(methods) { <BR>Method method = (Method) methods.get(name); <BR>if (method == 
null) { <BR>method = clazz.getMethod(name, types); <BR>methods.put(name, 
method); <BR>} <BR>return (method); <BR>} <BR>} <BR><BR>/** <BR>* 返回spring 
bean对象 <BR>* @param name Spring Bean的名称 <BR>* @exception BaseAppException <BR>*/ 
<BR>protected Object getSpringBean(String name) throws BaseAppException { <BR>if 
(ctx == null) { <BR>ctx = 
WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext()); 
<BR>} <BR>Object bean = null; <BR>try { <BR>bean = ctx.getBean(name); <BR>} 
catch (BeansException ex) { <BR>throw new 
NoSuchBeanConfigException("对不起,您没有配置名为:" + name + "的bean。请检查配置文件!", 
ex.getRootCause()); <BR>} <BR>if (null == bean) { <BR>throw new 
NoSuchBeanConfigException("对不起,您没有配置名为:" + name + "的bean。请检查配置文件!"); <BR>} 
<BR>return bean; <BR>} <BR>} <BR><BR><BR>开发人员只需要继承它就可以了,我们写个简单的示例action,如下: 
<BR><BR>代码 <BR>/** <BR>* 带Ajax提交xml数据的action类模版 <BR>* <BR>* @author 陈志平 chenzp 
<BR>* <BR>* @说明: web 应用基础平台 <BR>* @date Aug 1, 2006 10:52:13 AM <BR>* @版权所有: All 
Right Reserved 2006-2008 <BR>*/ <BR>public class UserAction extends 
BaseDispatchDocumentAction { <BR>/** <BR>* 这里 actionForm 和 doc 
参数必有一个为空,请聪明的你分析一下 <BR>* @param mapping --转发的映射对象 <BR>&lt;span 
style="color:blue;"&gt;* @param actionForm --仍然支持表单提交,此时doc == null <BR>* @param 
doc document对象,解析xml后的文档对象&lt;/span&gt; <BR>* @param request --请求 <BR>* @param 
response --响应 <BR>*/ <BR>public ActionForward list(ActionMapping mapping, 
ActionForm actionForm, &lt;span style="color:red;"&gt;Document 
doc&lt;/span&gt;,HttpServletRequest request, HttpServletResponse response) 
throws BaseAppException { <BR>/** <BR>* 转发的名称 userAction.search: 系统上下文 用于异常处理 
<BR>*/ <BR>expDisplayDetails.set(new ExceptionDisplayDTO(null, 
"userAction.search")); <BR>/** <BR>* 处理业务逻辑部分: <BR>* <BR>* 获取各种类型的参数 
RequestUtil.getStrParameter(request,"ParameterName"); <BR>* <BR>* 调用父类的 
getSpringBean("serviceID")方法获取spring的配置bean <BR>* <BR>*/ <BR>UserManager 
userManager = (LogManager) getSpringBean("userManager"); <BR>//返回xml对象到前台 
<BR>renderXML(response, userManager.findUsersByDoc(doc)); return null; <BR>} 
<BR><BR><BR>至此,我们成功实现了ajax--struts--spring的无缝结合。欢迎大家拍砖! </DIV>
<DIV class=blog_bottom>
<UL>
  <LI>20:23 </LI>
  <LI>浏览 (267) </LI>
  <LI><A href="http://zhangljerry.javaeye.com/blog/83176#comments">评论</A> (0) 
  </LI>
  <LI>分类: <A href="http://zhangljerry.javaeye.com/category/9989">web2.0</A> 
</LI></UL></DIV></DIV>
<DIV class=blog_main>
<DIV class=blog_title>
<H5>2007-05-24</H5>
<DIV class=show_full_flag><A 
href="http://zhangljerry.javaeye.com/category/9989?show_full=false">缩略显示</A></DIV>
<H3><A 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -