zoukankan      html  css  js  c++  java
  • StrutsResultSupport的使用

    在有特殊情况时;如果没有异常信息,但是有错误并且有错误信息等内容;此时也需要进行友好的错误处理的话,那么可以借助StrutsResultSupport 返回结果类型来实现特定处理。此种方式先需要继承StrutsResultSupport ,然后可以在子类中获取本次请求的相关信息,再根据相关信息进行结果处理:

    package commons.struts2;  
    import <a href="http://lib.csdn.net/base/java" class='replace_word' title="Java 知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.io.PrintWriter;  
    import javax.servlet.http.HttpServletResponse;  
    import org.apache.struts2.dispatcher.StrutsResultSupport;  
    import com.opensymphony.xwork2.ActionInvocation;  
    /** 
     * result type for output string in action 
     *  
     * @author songwei,yaolei <b>Example:</b> 
     *  
     * <pre> 
     * <!-- START SNIPPET: example --> 
     * <result name="success" type="string"> 
     *   <param name="stringName">stringName</param> 
     * </result> 
     * <!-- END SNIPPET: example --> 
     * </pre> 
     *  
     */  
    public class StringResultType extends StrutsResultSupport {  
        private static final long serialVersionUID = 1L;  
        private String contentTypeName;  
        private String stringName = "";  
        public StringResultType() {  
            super();  
        }  
        public StringResultType(String location) {  
            super(location);  
        }  
        protected void doExecute(String finalLocation, ActionInvocation invocation)  
                throws Exception {  
            HttpServletResponse response = (HttpServletResponse) invocation  
                    .getInvocationContext().get(HTTP_RESPONSE);  
            // String contentType = (String)  
            // invocation.getStack().findValue(conditionalParse(contentTypeName,  
            // invocation));  
            String contentType = conditionalParse(contentTypeName, invocation);  
            if (contentType == null) {  
                contentType = "text/plain; charset=UTF-8";  
            }  
            response.setContentType(contentType);  
            PrintWriter out = response.getWriter();  
            // String result = conditionalParse(stringName, invocation);  
            String result = (String) invocation.getStack().findValue(stringName);  
            out.println(result);  
            out.flush();  
            out.close();  
        }  
        public String getContentTypeName() {  
            return contentTypeName;  
        }  
        public void setContentTypeName(String contentTypeName) {  
            this.contentTypeName = contentTypeName;  
        }  
        public String getStringName() {  
            return stringName;  
        }  
        public void setStringName(String stringName) {  
            this.stringName = stringName;  
        }  
    } 
    package test;  
    import com.opensymphony.xwork2.ActionSupport;  
    public class MyAction extends ActionSupport{  
        String  result="abc";  
        public String ajax() {  
            return "ajaxResponse";  
        }  
        // getter && setter  
        public String getResult() {  
            return result;  
        }  
        public void setResult(String result) {  
            this.result = result;  
        }  
          
    } 
    <?xml version="1.0" encoding="UTF-8" ?>  
    <!DOCTYPE struts PUBLIC  
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
        "http://struts.apache.org/dtds/struts-2.0.dtd">  
    <struts>  
        <package name="test" extends="struts-default">  
            <result-types>  
                <result-type name="string" class="test.StringResultType"></result-type>  
            </result-types>  
              
            <action name="myAction" class="test.MyAction" >  
                <result name="ajaxResponse" type="string">  
                    <param name="stringName">result</param>  
                </result>  
            </action>  
        </package>  
    </struts> 

    这里定义返回Action的String  result变量,即“abc”。

  • 相关阅读:
    contes配置nginx教程
    jquery 图片放大镜,草稿版
    VUE学习第四次
    VUE学习 第三次
    ryu
    ovs & ryu & mininet
    centos安装mininet 和卸载
    端口镜像
    数据中心网络监控小结
    5、Kafka生产过程分析
  • 原文地址:https://www.cnblogs.com/lm970585581/p/7352820.html
Copyright © 2011-2022 走看看