• javaee ssm框架项目整合thymeleaf 项目结构图


    在这里插入图片描述

    搭建ssm框架项目

    参考这篇博客

    引入thymeleaf

    引入jar包

    
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0modelVersion>
    
      <groupId>org.examplegroupId>
      <artifactId>testSSM2artifactId>
      <version>1.0-SNAPSHOTversion>
      <packaging>warpackaging>
    
      <name>testSSM2 Maven Webappname>
      
      <url>http://www.example.comurl>
    
      <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
      properties>
    
      <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
    
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-contextartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-coreartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-beansartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-context-supportartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>5.1.37version>
        dependency>
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatisartifactId>
          <version>3.4.6version>
        dependency>
        <dependency>
          <groupId>com.mchangegroupId>
          <artifactId>c3p0artifactId>
          <version>0.9.5.2version>
        dependency>
    
        
        <dependency>
          <groupId>log4jgroupId>
          <artifactId>log4jartifactId>
          <version>1.2.17version>
        dependency>
    
        
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatis-springartifactId>
          <version>1.3.0version>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-jdbcartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webmvcartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>jstlartifactId>
          <version>1.2version>
        dependency>
    
        
        <dependency>
          <groupId>com.github.pagehelpergroupId>
          <artifactId>pagehelperartifactId>
          <version>5.1.2version>
        dependency>
    
        
        
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>javax.servlet-apiartifactId>
          <version>3.1.0version>
          <scope>providedscope>
        dependency>
    
        
        <dependency>
          <groupId>javax.servlet.jspgroupId>
          <artifactId>jsp-apiartifactId>
          <version>2.2version>
          <scope>providedscope>
        dependency>
    
        
        <dependency>
          <groupId>org.thymeleafgroupId>
          <artifactId>thymeleafartifactId>
          <version>3.0.9.RELEASEversion>
        dependency>
    
        <dependency>
          <groupId>org.thymeleafgroupId>
          <artifactId>thymeleaf-spring4artifactId>
          <version>3.0.9.RELEASEversion>
        dependency>
    
      dependencies>
    
    
    
      <build>
        <resources>
          <resource>
            
            <directory>src/main/javadirectory>
            <includes>
              <include>**/*.xmlinclude>
            includes>
            <filtering>truefiltering>
          resource>
        resources>
        <finalName>testSSM2finalName>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-clean-pluginartifactId>
              <version>3.1.0version>
            plugin>
            
            <plugin>
              <artifactId>maven-resources-pluginartifactId>
              <version>3.0.2version>
            plugin>
            <plugin>
              <artifactId>maven-compiler-pluginartifactId>
              <version>3.8.0version>
            plugin>
            <plugin>
              <artifactId>maven-surefire-pluginartifactId>
              <version>2.22.1version>
            plugin>
            <plugin>
              <artifactId>maven-war-pluginartifactId>
              <version>3.2.2version>
            plugin>
            <plugin>
              <artifactId>maven-install-pluginartifactId>
              <version>2.5.2version>
            plugin>
            <plugin>
              <artifactId>maven-deploy-pluginartifactId>
              <version>2.8.2version>
            plugin>
          plugins>
        pluginManagement>
      build>
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195

    修改springmvc配置文件

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           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">
        
        <context:component-scan base-package="com.test.controller">context:component-scan>
    
        
    
    
    
    
    
    
    
        
        
    
        <bean id="templateResolver"
    
              class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    
            <property name="prefix" value="/WEB-INF/views/" />
    
            <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>
    
    
    
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    修改web.xml

    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             id="WebApp_ID" version="3.0">
      <display-name>Archetype Created Web Applicationdisplay-name>
      
      
      <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
      context-param>
    
      
      
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
      listener>
    
    
    
      
      <servlet>
        <servlet-name>dispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
        <init-param>
          <param-name>contextConfigLocationparam-name>
          <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    
        <load-on-startup>1load-on-startup>
      servlet>
    
      <servlet-mapping>
        <servlet-name>dispatcherServletservlet-name>
        <url-pattern>/url-pattern>
      servlet-mapping>
    
      
    
      <servlet-mapping>
    
        <servlet-name>dispatcherServletservlet-name>
    
        <url-pattern>*.htmlurl-pattern>
    
      servlet-mapping>
    
    web-app>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    测试

    package com.test.controller;
    
    import com.github.pagehelper.PageInfo;
    import com.test.pojo.Items;
    import com.test.service.IItemsService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    import java.util.List;
    
    @Controller
    @RequestMapping("/items")
    public class ItemsController {
    
        @Autowired
        private IItemsService itemsService;
    
        public IItemsService getItemsService() {
            return itemsService;
        }
    
        public void setItemsService(IItemsService itemsService) {
            this.itemsService = itemsService;
        }
    
        @RequestMapping("/selectAllItems")
        public ModelAndView selectAllItems()
        {
    
            List<Items> itemsList= itemsService.selectItems();
    
            // System.out.println(itemsList);
    
            ModelAndView modelAndView=new ModelAndView();
    
            modelAndView.addObject("itemsList",itemsList);
    
            modelAndView.setViewName("showItems");
    
            return modelAndView;
        }
    
        @RequestMapping("/selectAllItemsByPage")
        public ModelAndView selectAllItemsByPage(@RequestParam(value ="pageindex",defaultValue = "1") int pageindex)
        {
            PageInfo<Items> pageInfo= itemsService.selectItemsByPage(pageindex,2);
    
            ModelAndView modelAndView=new ModelAndView();
    
            modelAndView.addObject("pageInfo",pageInfo);
    
            modelAndView.setViewName("showItemsByPage");
    
            return modelAndView;
        }
        @RequestMapping("/testThymeleaf")
        public  ModelAndView testThymeleaf()
        {
            ModelAndView modelAndView=new ModelAndView();
            modelAndView.addObject("uname","aaa");
            modelAndView.setViewName("test");// WEB-INF/views/test.html
            return modelAndView;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    DOCTYPE html>
    <html lang="en"  xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    
    head>
    <body>
    
    <div th:text="${uname}" style="background-color: pink">这里是用来显示用户名的div>
    
    
    
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    用 Pytorch 自己构建一个Transformer
    反转链表与拓展【灵神基础精讲】
    【WinForm详细教程五】WinForm中的MenuStrip 、ContextMenuStrip 、ToolStrip、StatusStrip控件
    【Bluetooth蓝牙开发】七、BLE协议之链路层
    FAIG滤波器
    React-hooks【四】父组件通过ref获取子组件实例
    2022年湖北省自然科学基金计划项目申请条件、要求和项目类型
    flex设置为1后为什么要设置width为0,和布局超出省略号为什么会超出容器,为什么会没有用
    systemctl start docker启动报错:Job for docker.service failed because the
    百度OCR识别图片文本字符串——物联网上位机软件
  • 原文地址:https://blog.csdn.net/Rockandrollman/article/details/133610702