springmvc4+thymeleaf简易教程(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhouchenxuan/article/details/82779690

thymeleaf整合springmvc与整合springboot不太一样,这里只介绍springmvc+thymeleaf的搭建。
1、文档结构图
在这里插入图片描述
2、pom.xml中加入依赖

		<properties>
	        <thymeleaf.version>2.1.2.RELEASE</thymeleaf.version>
    	</properties>
    	
		<dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>${thymeleaf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>${thymeleaf.version}</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>

3、spring配置文件
springmvc.xml

	<!--处理静态资源-->
    <mvc:default-servlet-handler/>
    <!--自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter两个bean-->
    <mvc:annotation-driven/>
    <!--自动扫描,需要换成自己的包名-->
    <context:component-scan base-package="com.zcx.controller"/>

    <!-- thymeleaf模板解析器  -->
    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="LEGACYHTML5" />
        <property name="cacheable" value="false" />
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding"  value="UTF-8" />
    </bean>

4、java文件
User.java


import lombok.Data;
import lombok.ToString;


@Data
@ToString
public class User {
    private String name;
    private Integer age;
}

HelloController.java


import com.zcx.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String index(Model model) {
        User u = new User();
        u.setAge(10);
        u.setName("zhou");
        model.addAttribute("name", "zcx");
        model.addAttribute("user", u);
        return "hello";
    }

    @RequestMapping("/anotherHtml")
    public String anotherHtml() {
        return "anotherHtml";
    }

}

5、页面
hello.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>Title</title>
    <script type="text/javascript" th:src="@{/static/js/jquery.min.js}" >
    </script>

    <script th:inline="javascript">
        $(function(){

        });
    </script>
</head>
<body>
<p>简单表达式${}</p>
<p th:text="${name}" />
<p th:text="${user.age}" />
<p>选择变量表达式*{},*{age} 等价于 ${user.age}</p>
<div th:object="${user}">
    <p th:text="*{name}" />
</div>
<p>链接表达式: @{} </p>
<a th:href="@{/anotherHtml}">超链接</a><br />
<p>文本替换</p>
<span th:text="'Welcome to our application, ' + ${user.name} + '!'"></span>
</body>
</html>

anotherHtml.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>Title</title>
    <script type="text/javascript" th:src="@{/static/js/jquery.min.js}" >
    </script>

    <script th:inline="javascript">
        $(function(){

        });
    </script>
</head>
<body>
<p>anotherHtml</p>
</body>
</html>

自己启动一下看看效果吧,100%可以用。
最后,可以参考大牛……们写的更加详细的文档。
https://www.cnblogs.com/litblank/p/7988689.html
https://www.cnblogs.com/jiangbei/p/8462294.html

猜你喜欢

转载自blog.csdn.net/zhouchenxuan/article/details/82779690