编组是在内存中生成对象的 XML 表示的过程。与 Java 序列化一样,这种表示需要包括所有依赖的对象:主对象引用的对象、这些对象引用的其他对象、等等。
解组是上述过程的逆过程,在内存中从 XML 表示创建对象(以及依赖的对象)。
在javabean和xml之间我们自由的往来,不仅用于通过javabean编组xml,作为页面展示,也是Flex的数据源;另一方面,将xml解组成javabean,作为测试之用。
Java代码如下:
package mvc.login;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.exolab.castor.xml.Marshaller;
import basic.Dealer;
import basic.JspVAR;
import basic.Util;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport implements ServletRequestAware {
/**
*
*/
private static final long serialVersionUID = -9016618480581879359L;
private HttpServletRequest request;
private LoginUtil loginUtil;
public LoginUtil getLoginUtil() {
return loginUtil;
}
public void setLoginUtil(LoginUtil loginUtil) {
this.loginUtil = loginUtil;
}
public String login() {
return Action.LOGIN;
}
public String in() {
String[] members = { "username'String", "password'String",
"format'String" };
Object[] elements = Dealer.getElemByName(request, members);
LoginForm searchPara = new LoginForm(elements);
/* 返回值:0. 全对. 1. 密码错. 2. 用户名错. */
int flag = loginUtil.judgeUserName_Password(searchPara);
LoginBean loginBean = new LoginBean();
switch (flag) {
case 0:
loginBean.setUsername("0");
loginBean.setPassword("0");
request.getSession().setAttribute(JspVAR.session_user,
searchPara.getUsername());
request.getSession().setAttribute(JspVAR.session_date,
Util.timeTransform(Util.getCurrentTime()));
break;
case 1:
loginBean.setUsername("0");
loginBean.setPassword("1");
break;
case 2:
loginBean.setUsername("1");
loginBean.setPassword("1");
break;
default:
loginBean.setUsername("1");
loginBean.setPassword("1");
break;
}
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
Writer writer = new StringWriter();
Marshaller.marshal(loginBean, writer);
out.print(writer.toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void in1() {
int flag = 0;
LoginBean loginBean = new LoginBean();
switch (flag) {
case 0:
loginBean.setUsername("0你");
loginBean.setPassword("0我");
break;
case 1:
loginBean.setUsername("0");
loginBean.setPassword("1");
break;
case 2:
loginBean.setUsername("1");
loginBean.setPassword("1");
break;
default:
loginBean.setUsername("1");
loginBean.setPassword("1");
break;
}
outputxml(loginBean);
}
public void outputxml(Object object) {
PrintWriter out = null;
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
out = response.getWriter();
Writer writer = new StringWriter();
Marshaller.marshal(object, writer);
out.print(writer.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
out.close();
}
}
public String out() {
return "LOGOUT-SUCCESS";
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
}
访问
http://127.0.0.1:8080/s3s/login_in1.action?username=admin&password=12345&format=xml
得到的xml如下:
<?xml version="1.0" encoding="UTF-8"?> <login-bean> <password>0我</password> <username>0你</username> </login-bean>
测试代码如下:
package mvc.login.test;
import java.io.*;
import java.net.*;
import mvc.login.LoginBean;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
public class GetURL {
public static void main(String[] args) throws IOException,
MarshalException, ValidationException {
String re = GetURLstr("http://127.0.0.1:8080/s3s/login_in1.action?username=admin&password=12345&format=xml");
// OutputStream out = System.out;
// out.write(re.getBytes());
Reader reader = new StringReader(re);
LoginBean loginBean = (LoginBean) Unmarshaller.unmarshal(
LoginBean.class, reader);
System.out.println(loginBean.getUsername());
System.out.println(loginBean.getPassword());
}
public static String GetURLstr(String strUrl) {
InputStream in = null;
// OutputStream out = null;
String strdata = "";
try {
URL url = new URL(strUrl); // 创建 URL
in = url.openStream(); // 打开到这个URL的流
// out = System.out;
// 复制字节到输出流
byte[] buffer = new byte[4096];
int bytes_read;
while ((bytes_read = in.read(buffer)) != -1) {
String reads = new String(buffer, 0, bytes_read, "UTF-8");
// System.out.print(reads);
strdata = strdata + reads;
// out.write(buffer, 0, bytes_read);
}
in.close();
// out.close();
return strdata;
}
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java GetURL <URL> [<filename>]");
return strdata;
}
}
}