目录
引用thymeleaf包(在basicService的pom.xml里添加)
在上一节文章中搭建好了基本的ssm+maven项目,但是页面用的是老技术jsp,这张将修改成最新的html5。
在此之前,我对项目利用maven进行了jar包拆分,父子工程,父工程现仅作为通用jar包引用(比如spring,mybatis)。另外修改了项目名称及包名,当然可以不模仿我的继续修改,只需看jsp转html5就行。
如图:
目前,基本架包都放在basicService里的pom.xml,在own工程pom.xml只留了测试junit。
另外:own的spring.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自动注入 -->
<context:component-scan base-package="org.test.service.impl" />
<!-- 加载properties文件 -->
<!-- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:mysqldb.properties</value>
</list>
</property>
</bean> -->
</beans>
在mybatis-spring.xml里添加一段代码mysqldb.properties我个人改为了db.properties
<!-- 在注释 配置数据源 前一段引入配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
好了,现在开始正题:
修改的根本是采用(thymeleaf)解析
引用thymeleaf包(在basicService的pom.xml里添加)
<!-- 引入thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.0.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>javassist</artifactId>
<groupId>org.javassist</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
修改own里的web.xml
去除原本的
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这一段非常重要,一定不要忘了加
<!--配置thymeleaf一定要加这个啊啊啊啊 不然404传不到给Controller !!! -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
*.action这样写,是解决访问拦截问题导致不能访问*.html,比如首页。js。css文件也会被拦截,走后台controller的requestmapping路劲去请求,而不是去找静态资源,当然会找不到。
修改own里的spring-mvc.xml
注释之前的jsp解析段落,下面的代码我已加了注释。
<!-- 视图解析器1:html视图解析器 必须先配置freemarkerConfig,注意html是没有prefix前缀属性的 -->
<!--<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="freemarkerSettings">
<bean
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="default_encoding">utf-8</prop>
<prop key="output_encoding">utf-8</prop>
<prop key="classic_compatible">true</prop>
</props>
</property>
</bean>
</property>
<property name="templateLoaderPath">
<value>/html/</value>
</property>
</bean>
<bean id="htmlviewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
p:suffix=".html" p:order="0">
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>-->
<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/html/" p:suffix=".jsp" p:order="1"/>-->
<!-- 使用thymeleaf解析 -->
<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/html/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML" />
<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的th
修改index.jsp为index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>person list</title>
</head>
<body>
<table>
<tr>
<th>白菜</th>
<th>大神</th>
</tr>
<tr th:each="person : ${persons}">
<td th:text="${person.name}"></td>
<td th:text="${person.age}"></td>
</tr>
</table>
</body>
</html>
好了,访问http://localhost:8099/own/personController/showPerson.action测试一下,可以正常显示了