WebWork 自定义resultType

大笑微笑生气吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头生气微笑大笑


偷笑我爱北京天安门偷笑


偷笑天安门上太阳升偷笑


偷笑伟大领袖毛主席偷笑


偷笑指引我们向前进偷笑

偷笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑偷笑

这里自定义一个dojoresult type  ,必须implements  Result 接口,重够Execute 方法

public class DojoMessageResult implements Result {  // 注意点1

private static final String OUTPUT_TYPE_NONE = "none";
 
private static final String OUTPUT_TYPE_LIST = "list";

 private String outputStyle = OUTPUT_TYPE_NONE;

 private String contentType = "text/html";

 public void execute(ActionInvocation invocation) throws Exception {   //注意点2
  HttpServletResponse resp = ServletActionContext.getResponse();
  if(StringUtil.isNotEmpty(contentType)){
   resp.setContentType(contentType);
  }
  List messages = DojoHelper.getResponseDojoMessages();
  for(Iterator iter = messages.iterator();iter.hasNext();){
   DojoHelper.DojoMessage aMsg = (DojoHelper.DojoMessage)iter.next();
   //now we just handle normal messages
   if(!aMsg.isMsgKey){
    /*
     * Encode all response messages into UTF-8 format.
     * And you should decode them using javascripts like this:
     * <pre>
     *  var rtnMsg = unescape(decodeURI(respData));
     * </pre>
     * the decodeURI() help you to decode UTF-8 chars back to your
     * original messages, but some special chars will still encoded
     * ,like '/'(the uri path charactor), so you should use the
     * unescape() function to handle these special charactors
     */
    String content = URLEncoder.encode(aMsg.content,"UTF-8");
    //decode the '+' back to space
    content = content.replace("+"," ");
    if(outputStyle.equals(OUTPUT_TYPE_LIST)){
     content = "<li>"+content+"</li>";
    }
    resp.getWriter().append(content).append('/n');
   }
  }
  resp.flushBuffer();  // 注意点3
  DojoHelper.cleanUp();
  return;
 }


}

你可能感兴趣的:(exception,String,list,Webwork,iterator,output)