必须加入的包有jsonplugin-0.30.jar,xsttream-1.3.jar,freemarker-2.3.8.jar
Xml代码
- ///struts.xml
- <struts>
- <include file="struts-default.xml"/>
- <package name="json" extends="json-default">
- <action name="extjs" class="book.action.ExtjsAction">
- <result type="json"/><!-- 这里生成Json -->
- </action>
- <action name="extjsXml" class="book.action.ExtjsAction">
- <result type="freemarker">
- <param name="location">template/gridXml.ftl</param>
- <param name="contentType">application/xml</param>
- </result>
- </action>
- </package>
- </struts>
///struts.xml <struts> <include file="struts-default.xml"/> <package name="json" extends="json-default"> <action name="extjs" class="book.action.ExtjsAction"> <result type="json"/><!-- 这里生成Json --> </action> <action name="extjsXml" class="book.action.ExtjsAction"> <result type="freemarker"> <param name="location">template/gridXml.ftl</param> <param name="contentType">application/xml</param> </result> </action> </package> </struts>
模板如下,
Xml代码
- //src/template/gridXml.ftl
- <?xml version="1.0" encoding="utf-8"?>
- <list>
- <Total>
- <results>${results}</results>
- </Total>
- <#list items as item>
- <#assign recordName=item.class.name.replaceAll(".*\\.","")>
- <${recordName}>
- <#assign methods=item.class.declaredMethods>
- <#list methods as method>
- <#assign isGetMethod=method.name.startsWith("get")>
- <#if isGetMethod = true>
- <#assign methodmethodName=method.name.replaceAll("get","").toLowerCase()>
- <${methodName}>
- ${method.invoke(item,null)}
- </${methodName}>
- </#if>
- </#list>
- </${recordName}>
- </#list>
- </list>