将jsp页面转为html页面

1.修改配置文件servlet,添加thymleaf解析器html

<!-- 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可识别代码,如java

<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>

转为spring

<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)sql

  • 注意:  html中的EL表达式没法识别,须要经过th:方法数据库

  • <meta charset="UTF-8"/>    <!-- 后面的结束符号 -->

4.经过网络链接他人的数据库网络

  • 新建sql用户(name随便,Hostname-IP地址)jsp

  • 更改工程中数据库链接的相关代码ide

5.html页面的顶部为(第二句为thymleaf的相关代码)this

<!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">

六、关于thymleaf解析器的配置的补充spa

对于返回的字符串,咱们会先对经过配置的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的解析。

相关文章
相关标签/搜索