XSLServlet.javapackage com.jcc.jia;/**
* This is the meat of the Model 2X. This servlet loads
* the XSLT styesheet after initialization. On each request,
* the form beans are serialized in XML, along with other
* information (see below). Finally, the XSLT processor is
* invoked.
*
* If the XML_DEBUG_PARAM parameter is found on the Request,
* the raw XML is sent to the client.
* This is useful for debugging.
*/
import org.apache.struts.action.ActionForm;
import org.w3c.dom.Document;
import org.w3c.dom.Element;import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;public class XSLServlet extends HttpServlet {
private static final String XSL_FILE_PARAM = "xsl-file";
private static final String XML_DEBUG_PARAM = "xml"; private DocumentBuilder documentBuilder;
private TransformerFactory tFactory = null ; private Templates xsl;
private Date xslLastRead = new Date();
private ServletConfig servletConfig;
private String xslFile; public void init(ServletConfig config)
throws ServletException {
String xslFile = config.getInitParameter(XSL_FILE_PARAM);
if (xslFile == null || xslFile.equals(""))
throw new ServletException(XSL_FILE_PARAM + " parameter must be present !"); try
{
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
throw new ServletException(e.toString());
} //this.tFactory = TransformerFactory.newInstance() ;
this.servletConfig = config;
this.xslFile = xslFile;
System.out.println("MVC 2X MODEL ===> XSLServlet1 getInitParameter(XSL_FILE_PARAM) = " + xslFile);
System.out.println("MVC 2X MODEL ===> XSLServlet1 init()"); } /**
* This method reads and parses the XSL stylesheet. For performance
* reasons, the file is read only if it changes on disk since
* the last read.
* 固定部分
*/
private void readXSL(ServletConfig config, String xslFile) throws ServletException {
try {
URLConnection urlConn = config.getServletContext().getResource(this.xslFile).openConnection();
urlConn.connect();
Date d = new Date(urlConn.getLastModified());
if (this.xslLastRead.after(d)) {
System.out.println("MVC 2X MODEL ===> XSLServlet XSL File has changed: reparsing...");
StreamSource ss = new StreamSource(urlConn.getInputStream());
// this.xsl = tFactory.newTemplates(ss);
this.xslLastRead = new Date();
}
System.out.println("MVC 2X MODEL ===> XSLServlet readXSL()==>" + xslFile); } catch (Exception e) {
e.printStackTrace();
throw new ServletException(e.toString());
} } /**
* Main method. We introspect the beans, serialize them and call the XSLT processor
* 主要的方法,对BEAN进行序列化以后,调用XSLT处理器
*/
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
System.out.println("MVC 2X MODEL ===> service1 begin");
System.out.println("MVC 2X MODEL ===> service1 before get tFactory ");
this.tFactory = javax.xml.transform.TransformerFactory.newInstance() ;
System.out.println("MVC 2X MODEL ===> service1 begin tFactory = " + tFactory);
}}
* This is the meat of the Model 2X. This servlet loads
* the XSLT styesheet after initialization. On each request,
* the form beans are serialized in XML, along with other
* information (see below). Finally, the XSLT processor is
* invoked.
*
* If the XML_DEBUG_PARAM parameter is found on the Request,
* the raw XML is sent to the client.
* This is useful for debugging.
*/
import org.apache.struts.action.ActionForm;
import org.w3c.dom.Document;
import org.w3c.dom.Element;import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;public class XSLServlet extends HttpServlet {
private static final String XSL_FILE_PARAM = "xsl-file";
private static final String XML_DEBUG_PARAM = "xml"; private DocumentBuilder documentBuilder;
private TransformerFactory tFactory = null ; private Templates xsl;
private Date xslLastRead = new Date();
private ServletConfig servletConfig;
private String xslFile; public void init(ServletConfig config)
throws ServletException {
String xslFile = config.getInitParameter(XSL_FILE_PARAM);
if (xslFile == null || xslFile.equals(""))
throw new ServletException(XSL_FILE_PARAM + " parameter must be present !"); try
{
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
throw new ServletException(e.toString());
} //this.tFactory = TransformerFactory.newInstance() ;
this.servletConfig = config;
this.xslFile = xslFile;
System.out.println("MVC 2X MODEL ===> XSLServlet1 getInitParameter(XSL_FILE_PARAM) = " + xslFile);
System.out.println("MVC 2X MODEL ===> XSLServlet1 init()"); } /**
* This method reads and parses the XSL stylesheet. For performance
* reasons, the file is read only if it changes on disk since
* the last read.
* 固定部分
*/
private void readXSL(ServletConfig config, String xslFile) throws ServletException {
try {
URLConnection urlConn = config.getServletContext().getResource(this.xslFile).openConnection();
urlConn.connect();
Date d = new Date(urlConn.getLastModified());
if (this.xslLastRead.after(d)) {
System.out.println("MVC 2X MODEL ===> XSLServlet XSL File has changed: reparsing...");
StreamSource ss = new StreamSource(urlConn.getInputStream());
// this.xsl = tFactory.newTemplates(ss);
this.xslLastRead = new Date();
}
System.out.println("MVC 2X MODEL ===> XSLServlet readXSL()==>" + xslFile); } catch (Exception e) {
e.printStackTrace();
throw new ServletException(e.toString());
} } /**
* Main method. We introspect the beans, serialize them and call the XSLT processor
* 主要的方法,对BEAN进行序列化以后,调用XSLT处理器
*/
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
System.out.println("MVC 2X MODEL ===> service1 begin");
System.out.println("MVC 2X MODEL ===> service1 before get tFactory ");
this.tFactory = javax.xml.transform.TransformerFactory.newInstance() ;
System.out.println("MVC 2X MODEL ===> service1 begin tFactory = " + tFactory);
}}
package com.jcc.jia.login.control;/**
* This action class populates the list.
* We also need to set the current form name
* that will be used in the XSLServlet.
*/import com.jcc.jia.login.beans.LoginForm;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import java.util.ArrayList;public class LoginAction extends Action {
public ActionForward perform(ActionMapping mapping, ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws java.io.IOException,
javax.servlet.ServletException {
String flag = request.getParameter("flag");
LoginForm loginForm = (LoginForm) form;
System.out.println("MVC 2X MODEL ===> LoginAction perform()");
//可以在此处添加该动作要求处理的业务逻辑
request.setAttribute("currentForm", form);
if(flag==null || flag.trim().equals("") ){
return mapping.findForward("XSLServlet");
}else {
return mapping.findForward("XSLServlet2");
} }
}
ResultAction.java 启动 Tomcat4.03 (我发布我的 mvc2x.war ), tomcat 出现如下信息:Starting service Tomcat-Standalone
Apache Tomcat/4.0.3
MVC 2X MODEL ===> XSLServlet1 getInitParameter(XSL_FILE_PARAM) = /WEB-INF/test.x
sl
MVC 2X MODEL ===> XSLServlet1 init()
register('-//Apache Software Foundation//DTD Struts Configuration 1.0//EN', 'jar
:file:E:/tomcat403/webapps/mvc2x/WEB-INF/lib/struts.jar!/org/apache/struts/resou
rces/struts-config_1_0.dtd'
register('-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN', 'jar:file:E:/
tomcat403/webapps/mvc2x/WEB-INF/lib/struts.jar!/org/apache/struts/resources/web-
app_2_2.dtd'
register('-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN', 'jar:file:E:/
tomcat403/webapps/mvc2x/WEB-INF/lib/struts.jar!/org/apache/struts/resources/web-
app_2_3.dtd'
resolveEntity('-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN', 'http://
java.sun.com/j2ee/dtds/web-app_2_2.dtd')
Resolving to alternate DTD 'jar:file:E:/tomcat403/webapps/mvc2x/WEB-INF/lib/str
uts.jar!/org/apache/struts/resources/web-app_2_2.dtd'
Call org.apache.struts.action.ActionServlet.addServletMapping(XSLServlet/java.la
ng.String,/XSLServlet/java.lang.String)
Call org.apache.struts.action.ActionServlet.addServletMapping(XSLServlet2/java.l
ang.String,/XSLServlet2/java.lang.String)
Call org.apache.struts.action.ActionServlet.addServletMapping(action/java.lang.S
tring,/*/java.lang.String)
Starting service Tomcat-Apache
Apache Tomcat/4.0.3
可能还需要将xalan.jar包放到tomcat\common\lib下
但是, 只需要将 xml-apis.jar 拷贝进 common\lib 下就行了
也就是说tomcat只认这个目录