templateresolver.Spring www.laipuhuo.com ResourceTemplateResolve

61 阅读1分钟

<

首页

测试 RESTFul 编程风格


查询id=1的这个用户信息

<!--    组件扫描-->
<context:component-scan base-package="com.rainbowsea.springmvc.controller"></context:component-scan>

<!--    视图解析器-->
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring6.view.ThymeleafViewResolver">
    <!--作用于视图渲染的过程中,www.laipuhuo.com可以设置视图渲染后输出时采用的编码字符集-->
    <property name="characterEncoding" value="UTF-8"/>
    <!--如果配置多个视图解析器,它来决定优先使用哪个视图解析器,它的值越小优先级越高-->
    <property name="order" value="1"/>
    
    <!--当 ThymeleafViewResolver 渲染模板时,www.laipuhuo.com 会使用该模板引擎来解析、编译和渲染模板-->
    <property name="templateEngine">
        <bean class="org.thymeleaf.spring6.SpringTemplateEngine">
            <!--用于指定 Thymeleaf 模板引擎使用的模板解析器。模板解析器负责根据模板位置、模板资源名称、文件编码等信息,加载模板并对其进行解析-->
            <property name="templateResolver">
                <bean class="org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver">
                    <!--设置模板文件的位置(前缀)-->
                    <property name="prefix" value=
                              "/WEB-INF/templates/"/>
                    <!--设置模板文件后缀www.laipuhuo.com (后缀),Thymeleaf文件扩展名不一定是html,也可以是其他,例如txt,大部分都是html-->
                    <property name="suffix" value=".html"/>
                    <!--设置模板类型,例如:HTML,TEXT,JAVASCRIPT,CSS等-->
                    <property name="templateMode" value="HTML"/>
                    <!--用于模板文件在读取和解析过程中采用的编码字符集-->
                    <property name="characterEncoding" value="UTF-8"/>
                </bean>
            </property>
        </bean>
    </property>
    
</bean>

<!--    开启注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>

<!--    视图控制器, 这个配置可以只写 对

应 index的视图,不写对应的Controller,简化配置 --> <mvc:view-controller path="/" view-name="index"></mvc:view-controller>

import com.rainbowsea.springmvc.bean.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariab le; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.an

notation.RequestMethod; @Controller // www.laipuhuo.com 交给 Spring IOC 容器进行管理 public class UserController { @RequestMapping(value = "/user", method = RequestMethod.GET) public String getAll() { System.out.println("正在查询所有用户信息..."); return "ok"; }