【问题标题】:How can I configure spring mvc/jsp to output xhtml instead of html?如何配置 spring mvc/jsp 以输出 xhtml 而不是 html?
【发布时间】:2011-04-25 17:36:47
【问题描述】:

我开始尝试使用 Spring MVC,并注意到我的 jsps 以 html 文件的形式提供。例如。

<html>
<head>
...

</html>

如何配置 Spring MVC 来提供 xhtml 文件?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
...
</html>

注意 - SpringMVC 在我的 jsp 前面加上 &lt;html&gt; 标记,所以在此之前我没有任何空间来添加 doctype。

【问题讨论】:

    标签: java html xhtml spring-mvc


    【解决方案1】:

    更改您的 JSP。对于服务器来说,所有的 HTML 都只是文本。 但请注意,您需要更改的不仅仅是文档类型。您还必须检查 JSP(以及包含的文件等)是否符合新标准。例如结束标记、小写标记和属性名称。

    【讨论】:

    • 不正确..这会破坏..因为生成的代码不符合 xhtml。
    • 对不起,哪一部分不是真的?服务器(不是客户端)不关心 HTML?这个问题没有提到任何关于生成代码的内容。
    • 对不起,如果评论不是很清楚......我应该提到的是“不够”。将 jsp 更改为 xhtml 是如何使 spring mvc 与 xhtml 一起工作的第 1 步。由于某些代码宏现在将生成不兼容的 xhtml 标记,因此由于验证问题,代码将开始中断。
    • 通过“更改 JSP”,我并不是说简单地更改文档类型就足够了。我已经更新了我的答案以明确这一点。
    • 但是我的 JSP 不包含 HTML 标记本身和文档类型。这些是由 Spring MVC 自动添加的,我无法控制它们。 (嗯,Spring 可能不会添加 doctype,但它会添加 html 标签,并且 doctype 应该在它之前)。
    【解决方案2】:

    这个没有快速修复,基本上你必须重写你的 jsps 使其符合 html 并添加适当的 DOCTYPE。

    您可以使用 JSP 生成几乎任何类型的文本文件。 JSP 本身并不关心您是在制作 CSV、XHTML、Quirks 模式 HTML 还是其他任何东西。

    现在,如果您使用 JSPX,您会受到更多限制,因为这些文件必须是有效的 xml。

    【讨论】:

    • 请查看我对@nfechner 的回答的评论——我不明白它是如何工作的。
    • Spring 没有添加它,在您的一个 jsps 中的某些地方,您会同时找到 doc 类型和 html 标记。
    【解决方案3】:

    以下是 web.xml 中用于 web flow 、 jsf 和 xhtml 显示的条目:

        <bean id="jpaFlowExecutionListener" class="org.springframework.webflow.persistence.JpaFlowExecutionListener">
                <constructor-arg ref="entityManagerFactory" />
                <constructor-arg ref="transactionManager" />
        </bean>
    
    <bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener" />
    
    <webflow:flow-executor id="flowExecutor">
        <webflow:flow-execution-listeners>
            <webflow:listener ref="jpaFlowExecutionListener"/>
            <webflow:listener ref="facesContextListener"/>
        </webflow:flow-execution-listeners>
    </webflow:flow-executor>
    
    <webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices" base-path="/WEB-INF/flows">
        <webflow:flow-location-pattern value="/**/*-flow.xml"/> 
    </webflow:flow-registry>
    
    <faces:flow-builder-services id="facesFlowBuilderServices" development="true" />
    
    <faces:resources/>
    
    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
        <property name="order" value="1"/>
        <property name="flowRegistry" ref="flowRegistry"/>
        <property name="defaultHandler">
             <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
        </property> 
    </bean>
    
    <bean class="org.springframework.faces.webflow.JsfFlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor" />
    </bean>
    
    <bean id="faceletsViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.faces.mvc.JsfView"/>
        <property name ="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".xhtml" />
    </bean>
    
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    

    【讨论】:

      猜你喜欢
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多