【问题标题】:spring mvc with entire html template带有整个html模板的spring mvc
【发布时间】:2015-08-18 00:38:03
【问题描述】:

我有一个使用 spring-mvc 和 html 的 spring-boot 应用程序。没有 Thymeleaf,没有 JSP。我希望能够将主题应用于我的应用程序,就像 Joomla 和 Wordpress 等 CMS 一样。问题是每篇 Spring-MVC 模板文章/帖子都在谈论使用单个 css 文件或使用 Tiles 之类的东西。如果我有 15 个主题,每个主题都在它们自己的文件夹中(它们通常似乎有许多 css、js 和 html 文件),我不确定如何将主题动态应用于我的应用程序(例如通过下拉选择)。

有人做过这样的事吗?从概念上讲我没有看到问题,但是没有手动移动 /template 下的每个模板相关文件,我不知道如何最好地完成此操作。

【问题讨论】:

  • 我知道如何将 Velocity 用作视图解析器。要我发布它作为答案吗?
  • 如果我不使用 Velocity 来查看我的页面视图,这会起作用吗?如果是这样,我很乐意看到它。
  • 我不这么认为。我从来没有在没有任何视图解析器的情况下使用过 spring。但我会发布它,所以你可以有一个想法。

标签: css html spring-mvc spring-boot


【解决方案1】:

使用 Velocity 作为 viewResolver 是可能的。

我正在使用这个配置:

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/views/"/>
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
            </props>
        </property>
    </bean>
    <!-- #Velocity -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
        <property name="cache" value="true" />
        <property name="prefix" value="" />
        <property name="suffix" value=".vm" />
        <property name="layoutUrl" value="layout1.vm" />
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>

属性 layoutUrl 是您的默认模板,即 webapp/WEB-INF/views/ 文件夹中的 HTML 文件:

layout1.vm:

<html>
<body>
<h1>Hello world 1!</h1>

$screen_content

</body>
</html>

velocity View Resolver 会将 $screen_content 替换为控制器响应的视图内容:

MyController.java

...

@RequestMapping("/mycontroller")
public String myController() {
    return "myView1";
}
...

所以,如果 webapp/WEB-INF/views/ 中的视图 myView1.vm 类似于:

<h2> Foo Bar! </h2>

对 /myApp/mycontroller 的请求的结果如下:

<html>
    <body>
    <h1>Hello world 1!</h1>

    <h2> Foo Bar! </h2>

    </body>
    </html>

如果你想使用另一个模板,你可以在你的控制器上动态设置它,在你的模型变量上设置值:

...

@RequestMapping("/mycontrollerWithADifferentLayout")
public String myController2(Model m) {
    m.addAttribute("layout", "layout2");
    return "myView1";
}
...

在模型上设置“布局”属性时,Velocity 将使用提供的视图作为模板。

【讨论】:

  • 感谢您的帮助,我将看看是否可以使用我的 HTML 进行这项工作。希望这可以解决我的问题而无需大量重写
【解决方案2】:

在 Spring Reference 上找到了这个:

17.9.2 定义主题

要在您的 Web 应用程序中使用主题,您必须设置一个 org.springframework.ui.context.ThemeSource 的实现 界面。 WebApplicationContext 接口扩展了 ThemeSource 但 将其职责委托给专门的实现。经过 默认委托将是 org.springframework.ui.context.support.ResourceBundleThemeSource 从根目录加载属性文件的实现 类路径。使用自定义 ThemeSource 实现或配置 ResourceBundleThemeSource 的基本名称前缀,您可以 使用保留名称在应用程序上下文中注册一个 bean 主题来源。 Web 应用程序上下文自动检测 bean 使用该名称并使用它。

使用 ResourceBundleThemeSource 时,主题定义在 简单的属性文件。属性文件列出了 构成主题。这是一个例子:

styleSheet=/themes/cool/style.css background=/themes/cool/img/coolBg.jpg 属性的键是 从视图代码中引用主题元素的名称。对于 JSP, 您通常使用 spring:theme 自定义标签来执行此操作,这非常 类似于 spring:message 标签。以下 JSP 片段使用 上一个示例中定义的主题以自定义外观:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
 <html>
     <head>
         <link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/>
     </head>
     <body style="background=<spring:theme code='background'/>">
         ...
     </body> </html>

默认情况下,ResourceBundleThemeSource 使用空的基本名称前缀。 > 结果,加载了属性文件 从类路径的根目录。因此你可以把cool.properties 类路径根目录中的主题定义,例如 例如,在 /WEB-INF/classes 中。 ResourceBundleThemeSource 使用 标准的 Java 资源包加载机制,允许完整的 主题国际化。例如,我们可以有一个 /WEB-INF/classes/cool_nl.properties 引用了一个特殊的 带有荷兰文字的背景图片。

http://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/html/mvc.html#mvc-themeresolver-defining

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 2021-03-16
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多