Struts2输出XML格式的Result
扩展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。
举例如下:
<?
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
=
"example"
namespace
=
"/example"
extends
=
"struts-default"
>
<
action
name
=
"outputXml"
method
=
"outxml"
class
=
"example.OutputXml"
>
<
result
name
=
"xmlMessage"
type
=
"plaintext"
>
</
result
>
</
action
>
相关文档:
已知有一个XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
< ......
XSD文件 (XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)。 具体使用方法和定义请参看:
http://www.w3school.com.cn/schema/index.asp
java自jdk1.5以上新增了SchemaFactory类 可以实现对XSD验证的支持,使用起来也很方便。
以下代码可用在JDK1.5+ 验证xml
public class SimpleErrorHandler i ......
var xmlDom; //XML DOM object
var xmlFile="ff.xml"; //xml file name
loadXML = function(fileRoute){
xmlDoc=null;
if (window.ActiveXObject){
xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
xmlDoc.async=false;
xmlDoc.load(fileRoute);
}else if (document.implementation &a ......
好接“OpenCV训练分类器制作xml文档之一”
了解了创建函数的命令行参数
现在我们对正样本举个例子:
假设有5 个正样本图片文件img1.bmp,…img5.bmp;创建一个正样本的txt文本文档,名字为 info.txt
正样本描述文件info.txt 的内容如下
positive/image1.bmp 1 0 0 24 28
positive/image2.bmp 1 0 0 24 ......
首先写一个html userxmljquery.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用户校验ajax实例</title>
& ......