SpringMVC+Thymeleaf +HTML的简单框架
一、问题
项目中需要公众号开发,移动端使用的是H5,但是如果不用前端框架的话,只能考虑JS前端用ajax解析JSON字符串了。今天我们就简单的说下前端框架Thymeleaf如何解决这个问题的;
二、开始实战
2.1:配置pom.xml
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0..RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0..RELEASE</version>
</dependency>
2.2:配置Spring mvc的主配置文件(spring-mvc.xml)
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="com.king.weixin"/>
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!--
配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,.04新增功能,需要重新设置spring-mvc-3.0.xsd
-->
<mvc:resources mapping="/img/**" location="/img/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/tinymce/**" location="/tinymce/" />
<mvc:resources mapping="/upload/**" location="/upload/" />
<mvc:resources mapping="/assset/**" location="/assset/" />
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<!--采用前端模板工具thymeleaf-->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/html/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<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>
<!--采用前端模板工具thymeleaf-->
<!-- redis配置 -->
<import resource="spring-redis.xml"/>
</beans>
需要注意的是:下面的是关键配置
<mvc:resources mapping="/img/**" location="/img/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/tinymce/**" location="/tinymce/" />
<mvc:resources mapping="/upload/**" location="/upload/" />
<mvc:resources mapping="/assset/**" location="/assset/" />
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<!--采用前端模板工具thymeleaf-->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/html/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8"/>
</bean>
2.3:Controller配置
@RequestMapping(value = "/QueryUserbyOpenId", method = RequestMethod.GET)
public String QueryUserbyOpenId(String openid,HttpServletRequest res,ModelMap model)
{
System.out.println("来了");
User user=new User();
user=userService.findByOpenId(openid);
if(user!=null)
{
model.addAttribute("user_name", user.getUser_name());
model.addAttribute("user_sex",user.getUser_sex());
model.addAttribute("user_age", user.getUser_age());
model.addAttribute("user_address", user.getUser_address());
model.addAttribute("user_mobile", user.getUser_mobile());
}
System.out.println("------------------:"+user.getUser_id());
return "userInfo";
return 返回的就是html视图的名称,具体是html还是jsp是在spring mvc中配置,需要在方法中生命ModelMap 来存放对象
2.4:HTML取值
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="UTF-8">
<title>个人中心</title>
<!-- <link rel="stylesheet" th:href="@{/css/userInfo.css}"> -->
<link rel="stylesheet" href="../css/userInfo.css">
<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="home">
<div class="doctor">
<div class="doctorName" th:text="${user_name}"></div>
<img class="doctorImg" src="../assset/icon/mydoctor_man.png">
<div class="changeUserInfo" onclick="window.location.href='updateUserInfoHTML'"><img src="../assset/icon/icon_bianjiziliao@2x.png">修改个人信息</div>
<div class="count">
<div>
<span class="moneyCount">20元</span>
<span>金额</span>
</div>
<div class="borderLeft"></div>
<div>
<span class="Upoint">0个</span>
<span>U点</span>
</div>
<div class="borderRight"></div>
<div>
<span class="discount">0张</span>
<span>优惠券</span>
</div>
</div>
</div>
<div class="msg">
<div>| 基本信息</div>
<div class="msgDiv">
<span style="color: rgba(204,204,204,1)" >性别:</span><span th:text="${user_sex}"></span>
</div>
<div class="msgDiv">
<span style="color: rgba(204,204,204,1)">年龄:</span><span th:text=" ${user_age}"></span>
</div>
<div class="msgDiv">
<span style="color: rgba(204,204,204,1)">地址:</span><span th:text="${user_address}"></span>
</div>
<div class="msgDiv">
<span style="color: rgba(204,204,204,1)">手机:</span><span th:text="${user_mobile}"></span>
</div>
<!-- <div class="msgDiv"> -->
<!-- <span style="color: rgba(204,204,204,1)">个人签名:</span><span th:text="${mobile}">无就是我</span> -->
<!-- </div> -->
<!-- <div class="msgDiv"> -->
<!-- <span style="color: rgba(204,204,204,1)">手机号:</span><span th:text="${mobile}"></span> -->
<!-- </div> --> <!-- <div class="changePhoneNum" onclick="window.location.href='updateUserMobileHTML'">修改手机号</div> -->
</div>
<!-- <div class="goToDownload" onclick="javascript:test()">马上下载APP,体验更多服务</div> -->
</div>
</body>
<script type="text/javascript">
function test(){
window.location.href="download"
}
</script>
</html>
导入命名空间 <html lang="en" xmlns:th="http://www.thymeleaf.org">,用th:text标签取值
SpringMVC+Thymeleaf +HTML的简单框架的更多相关文章
- SpringMVC+Thymeleaf 简单使用
一.简介 1.Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启动WEB应用,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果.浏览器解释 h ...
- 第6章—渲染web视图—SpringMVC+Thymeleaf 处理表单提交
SpringMVC+Thymeleaf 处理表单提交 thymleaf处理表单提交的方式和jsp有些类似,也有点不同之处,这里操作一个小Demo,并说明: 1.demo的结构图如下所示: pom.xm ...
- 基于SpringMVC下的Rest服务框架搭建【1、集成Swagger】
基于SpringMVC下的Rest服务框架搭建[1.集成Swagger] 1.需求背景 SpringMVC本身就可以开发出基于rest风格的服务,通过简单的配置,即可快速开发出一个可供客户端调用的re ...
- NHIBERNATE的简单框架的设计
NHIBERNATE的简单框架的设计 上次的 NHibernate的Session管理策略和NHibernateHelper 发布并提供下载,给NHibernate刚入门的同学们带来很多便利. 最近有 ...
- SpringMvc+thymeleaf+HTML5中文乱码问题
SpringMvc+thymeleaf+HTML5环境下遇到中文乱码...... 按照以往经验逐个排查,开发环境统一为utf-8编码,服务器也配置了编码过滤器,tomcat也是utf-8编码.前台页面 ...
- springmvc拦截器的简单了解
1.定义一个拦截器 2.在springmvc.xml中配置拦截器. (1)拦截器拦截的请求是建立在前端控制器配置之下的,若DispatcherServlet拦截的是*.action,则拦截器即使配置 ...
- 使用IntelliJ IDEA开发SpringMVC网站(二)框架配置
原文:使用IntelliJ IDEA开发SpringMVC网站(二)框架配置 摘要 讲解如何配置SpringMVC框架xml,以及如何在Tomcat中运行 目录[-] 文章已针对IDEA 15做了一定 ...
- 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建
概述 从0到1教你搭建spring+springMVC+hibernate整合框架,基于注解. 本教程框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3,开发工具为my ...
- spring的了解以及简单框架的搭建
了解spring: Spring是一个开源的控制反转(Inversion of Controller)和面向切面(AOP)的框架,目的是为了简化开发. IOC(控制反转): public class ...
随机推荐
- html 页内跳转
第一种 <a href="#div1">to div1</a> //跳转链接<div id="div1">div1</ ...
- python 全栈开发,Day24(复习,__str__和__repr__,__format__,__call__,__eq__,__del__,__new__,item系列)
反射: 使用字符串数据类型的变量名来使用变量 wwwh即what,where,why,how 这4点是一种学习方法 反射 :使用字符串数据类型的变量名来使用变量 1.文件中存储的都是字符串 2.网络 ...
- Linux学习笔记(Ubuntu操作系统)之hadoop学习之路
1:检查虚拟机的ip命令:ifconfig 2:普通用户切换root用户命令:su 3:root用户切换普通用户命令:su 用户名 4:普通用户执行系统执行前面加命令:sudo 5:查询主机名命令:h ...
- 【AtCoder】ARC078
C - Splitting Pile 枚举从哪里开始分的即可 #include <bits/stdc++.h> #define fi first #define se second #de ...
- Kubernetes核心概念总结
目录贴:Kubernetes学习系列 1.基础架构 1.1 Master Master节点上面主要由四个模块组成:APIServer.scheduler.controller manager.etcd ...
- Color the ball HDU1556
这题整整debug了两个小时 不同组居然要初始化 本以为built函数里面已经初始化好了!!!!! 其他无需注意 #include<cstdio> #include<cstring ...
- Docker 配置固定IP及桥接的实现方法(转载)
这篇文章主要介绍了Docker 配置固定IP和桥接的实现方法的相关资料,这里详细介绍了Docker 的四种网络模式及如何实现桥接的案例,需要的朋友可以参考下 docker默认使用bridge模式,通过 ...
- Linux学习之分区自动挂载与fstab文件修复(九)
linux分区自动挂载与fstab文件修复 在前面我们实现新添加硬盘,进行分区与格式化,然后手动挂载,这样做,在重启后,需要重新挂载才能使用. https://www.cnblogs.com/-wen ...
- python套接字编程实现ntp服务和远程命令执行
python套接字编程实现ntp服务和远程命令执行 目录 基于udp实现ntp服务 基于tcp实现远程命令执行 基于udp实现远程命令执行 tcp与udp的比较 前面关于套接字基础请查阅 https: ...
- 算法进阶面试题05——树形dp解决步骤、返回最大搜索二叉子树的大小、二叉树最远两节点的距离、晚会最大活跃度、手撕缓存结构LRU
接着第四课的内容,加入部分第五课的内容,主要介绍树形dp和LRU 第一题: 给定一棵二叉树的头节点head,请返回最大搜索二叉子树的大小 二叉树的套路 统一处理逻辑:假设以每个节点为头的这棵树,他的最 ...