Thymeleaf 模板和springMvc的整合以及如何在前段页面使用取值
来源:互联网 发布:understand mac 破解 编辑:程序博客网 时间:2024/05/29 18:53
1.建一个springMVC项目2.加jar包 - 1)gradle加jar包compile(“org.springframework.boot:spring-boot-starter-thymeleaf”)这个东西是个机遇maven的项目构成工具,不用的人不用鸟它 - 2)jar包下载地址 http://www.thymeleaf.org/download.html 官网3.在web.xml中配置servlet
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <paramname>contextConfigLocation</paramname> <paramvalue>/WEBINF/servletcontext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>4.在配置的servlet.xml文件夹里配置thymeleaf相<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- Scans the classpath of this application for @Components to deploy as beans --> <context:component-scan base-package="com.test.thymeleaf.controller" /> <!-- Configures the @Controller programming model --> <mvc:annotation-driven /> <!--Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> <!--springMVC+jsp的跳转页面配置--> <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">--> <!--<property name="prefix" value="/WEB-INF/views/" />--> <!--<property name="suffix" value=".jsp" />--> <!--</bean>--> <!--springMVC+thymeleaf的跳转页面配置--> <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML5" /> </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" /> </bean></beans>
5.在cotroller中定义入口package com.test.thymeleaf.controller;import com.test.thymeleaf.domain.User;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class HomeController { User user = new User(); //入口 @RequestMapping(value = “/home”) public String home(Model model) { model.addAttribute(“user”,user); return “aa”; } //提交表单后进行数据读取,并将数据传出 @RequestMapping(value = “/bb”,method = RequestMethod.POST) public String bb(User user,Model model) { model.addAttribute(“user”, user); model.addAttribute(“message”, “,welcome”); return “bb”; }}静态页面加入项目中,并添加thymeleaf标签 注意头文件 (用th:object定义表单数据提交对象,用th:field定义表单数据属性,用*{}锁定上级定义的对象,{}内填写对象属性,提交表单时自动降属性值封ad> Home .html(用${}读取后台传出的数据动态替换静态数据“vinphy,”和”welcome!”) Homevinphy,welcome!
7.部署访问 部署后访问http://localhost:8080/home进行访问,出现aa.html的内容> http://www.cnblogs.com/vinphy/p/4673918.html
阅读全文
0 0
- Thymeleaf 模板和springMvc的整合以及如何在前段页面使用取值
- SpringMVC整合Thymeleaf模板
- 【三】、整合SpringMVC(基本配置和thymeleaf模板选择)
- thymeleaf页面框架模板的使用
- thymeleaf模板引擎和shiro框架的整合
- Thymeleaf模板的使用
- Thymeleaf模板的使用
- Thymeleaf模板的使用
- Thymeleaf模板的使用
- Thymeleaf 模板的使用
- Thymeleaf模板的使用
- Thymeleaf 模板的使用
- thymeleaf模板的使用
- 前段页面标签的使用
- 在SpringMVC 中使用 Thymeleaf 模板引擎
- Thymeleaf赋值和取值
- springboot整合thymeleaf模板
- SpringBoot整合thymeleaf模板
- leetcode--Reverse Linked List II
- 圆括号
- 看到的redis简单例子,类似于抢票的非常简单例子
- 计算机视觉、机器学习相关领域论文和源代码大集合--持续更新……
- idea 快捷键
- Thymeleaf 模板和springMvc的整合以及如何在前段页面使用取值
- 传感器专题(1)——相关介绍
- Linux下安装jetty服务器
- 【Tesseract-OCR】Tesseract训练中文字体识别
- centos下linux运行asp网站搭建配置-mono+nginx
- c++知识总结(1)
- leetcode--Restore IP Addresses
- Profibus-Dp 工作过程介绍
- poj 2299(权值线段树)