struts2中 返回string文本配置 出借

本文分析了一个关于Struts框架中出现IllegalStateException异常的问题,并详细解释了异常产生的原因及解决方法。通过修改Action方法的返回类型并调整代码逻辑,成功解决了异常问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出现下面的错误

 

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();
        }
    }

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值