Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XSLT+;XML未在web客户端应用程序中形成html,抛出;“无协议”;错误_Java_Xml_Spring_Xslt - Fatal编程技术网

Java XSLT+;XML未在web客户端应用程序中形成html,抛出;“无协议”;错误

Java XSLT+;XML未在web客户端应用程序中形成html,抛出;“无协议”;错误,java,xml,spring,xslt,Java,Xml,Spring,Xslt,继续提问 我的web服务包含此方法 @WebMethod public String sayHello() { logger.debug("entering"); String result = null; DateTime currTime = new DateTime(); // now result = "greetings from the web service! time is " + DATE_PATTERN.print( currTime)

继续提问

我的web服务包含此方法

@WebMethod public String sayHello()
{
    logger.debug("entering");

    String result = null;

    DateTime currTime = new DateTime();  // now

    result = "greetings from the web service! time is " + DATE_PATTERN.print( currTime);

    logger.debug("exiting ");
    return result;
}
当你打电话来的时候 localhost:8080/myWebService/sayHello返回

<soap:Envelope>
  <soap:Body>
    <ns2:sayHelloResponse>
      <return>greetings from the web service! time is 2015-09-10T22:25:05.281</return>
    </ns2:sayHelloResponse>
  </soap:Body>
</soap:Envelope>
我的servlet-context.xml包含

<bean id="xsltViewResolver"  class="org.springframework.web.servlet.view.xslt.XsltViewResolver">
  <property name="order" value="1"/>
  <property name="sourceKey" value="xmlSource"/>
  <property name="viewClass" value="org.springframework.web.servlet.view.xslt.XsltView"/>
  <property name="prefix" value="/WEB-INF/xsl/" />
  <property name="suffix" value=".xsl" />
</bean>
因此,服务上的方法正在被调用,但我的控制器方法中存在一些不正确的地方。我应该怎么做才能让xslt格式化来自服务方法的xml以生成html页面

蒂亚


在删除的答案中,wero仍在学习Steve,他写道您的问题可能在新的StreamSource(string)中,它需要一个URI。在你写的评论中:

不,xmlSource实际上包含整个xmlsoap消息,这可能就是您的建议失败的原因,并出现“prolog中不允许内容”errmsg

xmlSource
包含整个xmlsoap消息的事实表明,至少对
newstreamsource(xmlSource)
的构造函数调用是不正确的,因为包含XML的字符串与包含URI的字符串不同

你运用了他的建议:

这给了您一个prolog中不允许的
内容
错误。这是积极的,因为这意味着现在至少您的代码正在尝试将其解析为XML

通常,后面的错误是由于编码错误,这可能是由于
addObject
方法没有获得正确的方法。您似乎没有说明模型中的
xmlSource
类型。但是,如果您按照您发布的链接中的示例进行操作,您会发现类似的情况:

@RequestMapping(value="/viewXSLT")
public ModelAndView viewXSLT(HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    // builds absolute path of the XML file
    String xmlFile = "resources/citizens.xml";
    String contextPath = request.getServletContext().getRealPath("");
    String xmlFilePath = contextPath + File.separator + xmlFile;

    Source source = new StreamSource(new File(xmlFilePath));

    // adds the XML source file to the model so the XsltView can detect
    ModelAndView model = new ModelAndView("XSLTView");
    model.addObject("xmlSource", source);

    return model;
}
这反过来表明预期的对象必须是
StreamSource
类型。所以这部分是正确的。由于出现prolog错误,这可能意味着XML世界中的一些事情之一:

  • 字符串没有正确编码,例如,在系统的默认编码ISO-8859-1中可以看到它,而实际上它是带有BOM的UTF-8,或者相反

  • 在序言之前的第一个字符不是BOM(这是
    我最终实现了这一点,即让我的viewXSLT方法在浏览器中正确显示调用的本地xml文件。下一步是重新定义该方法以从web服务读取SOAP消息。这可能需要对我的web客户端进行一些重构。感谢Abel的回答。
    
    <bean id="xsltViewResolver"  class="org.springframework.web.servlet.view.xslt.XsltViewResolver">
      <property name="order" value="1"/>
      <property name="sourceKey" value="xmlSource"/>
      <property name="viewClass" value="org.springframework.web.servlet.view.xslt.XsltView"/>
      <property name="prefix" value="/WEB-INF/xsl/" />
      <property name="suffix" value=".xsl" />
    </bean>
    
    javax.xml.transform.TransformerException:
        com.sun.org.apache.xml.internal.utils.WrappedRuntimeException:
           no protocol: 
              greetings from the web service! time is 2015-09-10T22:49:24.500
    
    Source source = new StreamSource(new java.io.StringReader(xmlSource));
    
    @RequestMapping(value="/viewXSLT")
    public ModelAndView viewXSLT(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        // builds absolute path of the XML file
        String xmlFile = "resources/citizens.xml";
        String contextPath = request.getServletContext().getRealPath("");
        String xmlFilePath = contextPath + File.separator + xmlFile;
    
        Source source = new StreamSource(new File(xmlFilePath));
    
        // adds the XML source file to the model so the XsltView can detect
        ModelAndView model = new ModelAndView("XSLTView");
        model.addObject("xmlSource", source);
    
        return model;
    }