Struts2输出XML格式的Result收藏 扩展Struts2--自定义String和XML格式的Result

本文介绍如何在Struts2框架中通过扩展实现自定义String与XML格式的结果返回,包括使用PlainTextResult和扩展StrutsResultSupport两种方法。

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

扩展Struts2--自定义String和XML格式的Result

struts2虽然继承了webwork优秀的MVC分离,可是有很多地方让人百思不得其解!最让人离谱的是,返回的结果集中居然没有String,xml这两种非常常用的类型。还是自己动手,丰衣足食:

第一种方式:使用“PlainText Result”

先看官方文档对plain text结果的定义:“A result that send the content out as plain text. Usefull typically when needed to display the raw content of a JSP or Html file for example.”这是一个纯扯蛋的说法。。。貌似感觉只能返回jsp页面似的,最起码他误导了我。

其实使用“PlainText Result” ,返回的结果是未进行格式和编码定义的字符串 什么意思?就类似于“FreeMarker Result” ,返回一个*.ftl格式的模板,你完全可以在*.ftl写string,那么结果就是string;也可以在里面写xml,那么结果就是xml。

举例如下:

  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. <!DOCTYPEstrutsPUBLIC
  3. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. < struts >
  6. < package name = "example" namespace = "/example"
  7. extends = "struts-default" >
  8. < action name = "outputXml" method = "outxml" class = "example.OutputXml" >
  9. < result name = "xmlMessage" type = "plaintext" > </ result >
  10. </ action >
  11. </ package >
  12. </ struts >

这里定义了xmlMessage为plain text结果,至于它具体是什么,看下面的Action类:

public class OutputXml extends ActionSupport{ public void outxml() throws Exception{ HttpServletResponseresponse=ServletActionContext.getResponse(); response.setContentType( "text/xml" ); PrintWriterpw=response.getWriter(); pw.print( "<cc>cccccc</cc>" ); }

在代码中,我们显式的给response定义了ContentType。 那么返回去的内容"<cc>cccccc</cc>"就会被接收方按xml进行解析。

而如果需要返回的是String类型,那么contentType = "text/plain”。

如果进一步需要指明编码,那么contentType = "text/plain; charset=UTF-8";

到这里理解“ plain text的结果是未进行格式和编码定义的字符串”应该就不困难了。基于http的内容传输实际都是字符串型,类型的定义是放在response的contentType 中。

第二种方式: 直接扩展struts2的结果集StrutsResultSupport

代码如下:

应该很容易懂了。。嘿嘿

package commons.struts2; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.dispatcher.StrutsResultSupport; import com.opensymphony.xwork2.ActionInvocation; /** *resulttypeforoutputstringinaction * *@authorsongwei,yaolei<b>Example:</b> * *<pre> *<!--STARTSNIPPET:example--> *<resultname="success"type="string"> *<paramname="stringName">stringName</param> *</result> *<!--ENDSNIPPET:example--> *</pre> * */ public class StringResultType extends StrutsResultSupport{ private static final long serialVersionUID=1L; private StringcontentTypeName; private StringstringName= "" ; public StringResultType(){ super (); } public StringResultType(Stringlocation){ super (location); } protected void doExecute(StringfinalLocation,ActionInvocationinvocation) throws Exception{ HttpServletResponseresponse=(HttpServletResponse)invocation .getInvocationContext().get(HTTP_RESPONSE); //StringcontentType=(String) //invocation.getStack().findValue(conditionalParse(contentTypeName, //invocation)); StringcontentType=conditionalParse(contentTypeName,invocation); if (contentType== null ){ contentType= "text/plain;charset=UTF-8" ; } response.setContentType(contentType); PrintWriterout=response.getWriter(); //Stringresult=conditionalParse(stringName,invocation); Stringresult=(String)invocation.getStack().findValue(stringName); out.println(result); out.flush(); out.close(); } public StringgetContentTypeName(){ return contentTypeName; } public void setContentTypeName(StringcontentTypeName){ this .contentTypeName=contentTypeName; } public StringgetStringName(){ return stringName; } public void setStringName(StringstringName){ this .stringName=stringName; } }

使用的方法:

1.Action

  1. package test;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class MyAction extends ActionSupport{
  4. Stringresult= "abc" ;
  5. public Stringajax(){
  6. return "ajaxResponse" ;
  7. }
  8. //getter&&setter
  9. public StringgetResult(){
  10. return result;
  11. }
  12. public void setResult(Stringresult){
  13. this .result=result;
  14. }
  15. }

2.定义struts.xml

  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. <!DOCTYPEstrutsPUBLIC
  3. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. < struts >
  6. < package name = "test" extends = "struts-default" >
  7. < result-types >
  8. < result-type name = "string" class = "test.StringResultType" > </ result-type >
  9. </ result-types >
  10. < action name = "myAction" class = "test.MyAction" >
  11. < result name = "ajaxResponse" type = "string" >
  12. < param name = "stringName" > result </ param >
  13. </ result >
  14. </ action >
  15. </ package >
  16. </ struts >

无非也就是将string结果集进行申明,然后给返回“ajaxResponse”的结果绑定变量名。这里定义返回Action的Stringresult 变量,即“abc”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值