现在的位置: 首页 > 综合 > 正文

struts2中 返回string文本配置 出借

2013年08月23日 ⁄ 综合 ⁄ 共 4174字 ⁄ 字号 评论关闭

出现下面的错误

 

2009-12-5 21:21:17 org.apache.catalina.core.StandardWrapperValve invoke
严重:
Servlet.service() for servlet default threw exception

java.lang.IllegalStateException
at
org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)

at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:707)

at
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)

at
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:467)

at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:97)

at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)

at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)

at
org.apache.catalina.valves.RequestFilterValve.process(RequestFilterValve.java:269)

at
org.apache.catalina.valves.RemoteAddrValve.invoke(RemoteAddrValve.java:81)

at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)

at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)

at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)

at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)

at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)

at java.lang.Thread.run(Unknown Source)

 

struts里的配置

 

<action name="getProductTypeTree" class="TreeAction" method="getProductTypeTree">
            <result name="success" type="plainText">
            </result>
 </action>   

 

Action里面的方法

 

public String getProductTypeTree() throws Exception{       
        List<ProductType> ProductTypes=this.pts.getProductTypeByParentId(parentId);
        String resutl=TreeUtil.getProudctTypeTreeString(ProductTypes, "getProductTypeTree.action");
        try{           
            HttpServletResponse response=ServletActionContext.getResponse();
            response.setContentType("text/plain;charset=utf-8");
            PrintWriter pw= response.getWriter();
            pw.print(resutl);
        }
        catch(Exception e){
            e.printStackTrace();
        }

        return SUCCESS;
    }

 

查jdk5的文档发现

 



出现IllegalStateException异常的可能情况:

1)同一个页面中再次调用response.sendRedirect()方法。

2)提交的URL错误,即不是个有效的URL。

sendRedirect
void
sendRedirect(java.lang.String location)
throws java.io.IOException
Sends
a temporary redirect response to the client using the specified redirect
location URL. This method can accept relative URLs; the servlet container must
convert the relative URL to an absolute URL before sending the response to the
client. If the location is relative without a leading &apos;/&apos; the
container interprets it as relative to the current request URI. If the location
is relative with a leading &apos;/&apos; the container interprets it as
relative to the servlet container root.
If the response has already been
committed, this method throws an IllegalStateException. After using this method,
the response should be considered to be committed and should not be written to.

Parameters:
location - the redirect location URL
Throws:

java.io.IOException - If an input or output exception occurs

java.lang.IllegalStateException - If the response was committed or if a
partial URL is given and cannot be converted into a valid URL

 

可能出现两次的 response.sendRedirect()方法


把Action 的方法改为无返回值的方法  就没有错误产生了

 

改成如下

 

       public void getProductTypeTree() throws Exception{       
        List<ProductType> ProductTypes=this.pts.getProductTypeByParentId(parentId);
        String resutl=TreeUtil.getProudctTypeTreeString(ProductTypes, "getProductTypeTree.action");
        try{           
            HttpServletResponse response=ServletActionContext.getResponse();
            response.setContentType("text/plain;charset=utf-8");
            PrintWriter pw= response.getWriter();
            pw.print(resutl);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

抱歉!评论已关闭.