ssm+maven搭建及扩展修改(二.架构页面除去jsp采用html5)

本文详细介绍如何在Spring+SpringMVC+MyBatis(Maven)项目中,用Thymeleaf替代JSP进行页面渲染,包括配置依赖、修改配置文件、调整页面写法等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

好了,现在开始正题:

引用thymeleaf包(在basicService的pom.xml里添加)

修改own里的web.xml

修改own里的spring-mvc.xml

修改页面写法,标签库采用thymeleaf的th


上一节文章中搭建好了基本的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测试一下,可以正常显示了

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值