1.修改配置文件servlet,添加thymleaf解析器
<!-- thymeleaf的视图解析器 -->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/html/" />
<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 id="viewResolverThymeleaf" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8"/>
<property name="order" value="0"/>
</bean>
2.jsp页面相关内容转为html可识别代码,如
<c:forEach items="${list}" var="user" varStatus="s">
<tr>
<td><a href="delete?userId=${user.userId}">${user.userId}</a></td>
<td>${user.userName}</td>
<td><a href="delete?userId=${user.userId}">Delete</a></td>
</tr>
</c:forEach>
转为
<div th:each="user,s:${list}">
<table>
<tr>
<td><a th:href="@{delete(userId=${user.userId})}"><span th:text="${user.userId}"></span></a></td>
<td><span th:text="${user.userName}"></span></td>
<td><span th:text="${user.userPassword}"></span></td>
<td><a th:href="@{delete(userId=${user.userId})}">delete</a></td>
</tr>
</table>
</div>
3.将thymleaf相关包导入(thymeleaf-2.1.4.RELEASE, thymeleaf-spring4-2.1.4.RELEASE. unbescape-1.1.0.RELEASE)
注意: html中的EL表达式无法识别,需要通过th:方法
<meta charset="UTF-8"/> <!-- 后面的结束符号 -->
4.通过网络连接他人的数据库
新建sql用户(name随便,Hostname-IP地址)
更改工程中数据库连接的相关代码
5.html页面的顶部为(第二句为thymleaf的相关代码)
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
6、关于thymleaf解析器的配置的补充
对于返回的字符串,我们会先对通过配置的thymeleaf来先判断是否可以解析为一个HTML页面,然后不可以的时候就抛出错误,但是在我们的项目中,我们并没有抛出异常,因为我们在第一次解析中加入我们自定义的一个类在cn.agriculture.common.component这个包中,这个类的代码如下:
public class ThymeleafViewResolverEx extends ThymeleafViewResolver {
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
ServletContextTemplateResolver servletContextTemplateResolver =
(ServletContextTemplateResolver)this.getWebApplicationContext().getBean("templateResolver");
servletContextTemplateResolver.initialize();
String prefix = servletContextTemplateResolver.getPrefix().substring(1);
String suffix = servletContextTemplateResolver.getSuffix();
//String str = getClass().getResource("/").toString().replace("file:/", "")
.replace("/WEB-INF/classes/", "");
//log.info("*****************************" + str);
log.info("-----------------------------" + this.getServletContext().getRealPath("/"));
File file = new File(this.getServletContext().getRealPath("/") + prefix + viewName + suffix);
if (!file.exists()){ return null; }
return super.resolveViewName(viewName, locale); }}
在这个类的作用下,我们可以先对controller返回的一个字符串做HTML页面的解析,在没有相应的HTML页面再进行Jsp的解析,即调用SpringMVC的内部视图解析器。若我们不需要这样的帮助时,我们可以将第一段代码中的最后一个bean的class属性值选择为ThymeleafViewResolver的包名。
当我们因为类找不到时,我们可以通过复制类,然后按住 ctrl+shfit+t 来快捷提示对应的类的位置,然后将对应的包找到复制到我们工程WebContent的WEB-INF文件夹下的lib中,这样我们可以实现HTML的解析。