Springboot+ thymeleaf+ easyui (不含数据库) demo

这段时间的一个工作任务是要实现一个本地的管理工具,之前的版本都是使用的javaGUI的界面;但自己对于GUI的使用非常不熟悉,所以就提出使用也买呢的方式实现。由于之前听过说过springboot的大名,所以就决定使用该框架,边学边用。

好在spring官方的文档还是比较多的,稍微看了一下,发现入门比较简单,另外工作任务的也只是需求本地使用,所以需求的功能也不会太苛刻。公司网络限制,只好在家里学好了,再把demo发到公司邮箱。

通过查看文档与比较各个技术难点,初步确定了标题的技术栈,下载了springboot  thymeleaf easyui jquery文档就开始搭建demo,目标是实现web项目的基本购价。

1、直接导出官方的文件上传的demo,稍加修改之后,实现了一个较为流畅的demo;

首先是项目结构图:


然后是具体的各个文件内容:

1.1pom.xml  由于thymeleaf 包含了boot-starter-web,所以不需要重复依赖,具体的配置解释可以看http://blog.csdn.net/chszs/article/details/50610474

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>org.xiehf.own</groupId>
    <artifactId>ToBePro</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

1.2application没什么可说的,直接全部默认的话就可以省下不少代码量,如需要特殊的配置@bean再补充

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}


1.3首页controller,预留model是为了初始化的时候返回数据

@Controller
public class BaseController {

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String home(Model model){

        return "index";
    }
}

1.4uploadcontroller,文件上传(Result是一个返回结果的对象,封装信息)


@Controller
@RequestMapping("/file")
public class FileUploadController {

    private static final Logger LOG = LoggerFactory.getLogger(FileUploadController.class);

    public static final String ROOT = "upload-dir";

    @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Result handleFileUpload(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            Result result = null;
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                String originalFilename = file.getOriginalFilename();
                Files.copy(inputStream, Paths.get(ROOT, originalFilename));
                System.out.println("OK");
                result = new Result(true, "", originalFilename);
            } catch (FileAlreadyExistsException e) {
                System.out.println("FileAlreadyExistsException");
                result =new Result(true, "FileAlreadyExistsException");
            } catch (DirectoryNotEmptyException e) {
                System.out.println("DirectoryNotEmptyException");
                result =new Result(true, "DirectoryNotEmptyException");
            } catch (UnsupportedOperationException e) {
                System.out.println("UnsupportedOperationException");
                result =new Result(true, "UnsupportedOperationException");
            } catch (SecurityException e) {
                System.out.println("SecurityException");
                result =new Result(true, "SecurityException");
            } catch (IOException e) {
                System.out.println("IOException");
                result =new Result(true, "IOException");
            } finally {
                if (null != inputStream) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        System.out.println("close failed");
                    }
                }
            }
            return result;
        }
        return null;
    }


1.5index首页easyUI的模版,thymeleaf所有的引用路径都要用th:前缀使用@{}包含,为了实际开发效率,可以同时设置静态引入:

比如:有href =""和 th:href="@{}" 这样就可以直接不启动spring也能浏览了。

<link rel="stylesheet" type="text/css" href="../static/easyui/themes/default/easyui.css" th:href="@{easyui/themes/default/easyui.css}"/>

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>home page</title>
    <link rel="stylesheet" type="text/css" href="../static/easyui/themes/default/easyui.css" th:href="@{easyui/themes/default/easyui.css}"/>
    <link rel="stylesheet" type="text/css" href="../static/easyui/themes/icon.css" th:href="@{easyui/themes/icon.css}"/>
    <link rel="stylesheet" type="text/css" href="../static/easyui/themes/color.css" th:href="@{easyui/themes/color.css}"/>
    <script type="text/javascript" src="../static/jquery/jquery.min.js" th:src="@{jquery/jquery.min.js}"></script>
    <script type="text/javascript" src="../static/jquery/ajaxfileupload.js" th:src="@{jquery/ajaxfileupload.js}"></script>
    <script type="text/javascript" src="../static/easyui/jquery.easyui.min.js" th:src="@{easyui/jquery.easyui.min.js}"></script>
    <script type="text/javascript" src="../static/js/upload.js" th:src="@{js/upload.js}"></script>
</head>
<body class="easyui-layout" >
    <div data-options="region:'north',title:'North Title',split:true" style="height:100px;"></div>
    <div data-options="region:'south',title:'South Title',split:true" style="height:100px;"></div>
    <div data-options="region:'east',iconCls:'icon-reload',title:'East',split:true" style="width:100px;"></div>
    <div data-options="region:'west',title:'West',split:true" style="width:100px;"></div>
    <div data-options="region:'center',title:'center title'" style="padding:5px;background:#eee;">
        <div id="panel" class="easyui-panel" title="My Panel"
             style="width:500px;height:150px;padding:10px;background:#fafafa;"
             data-options="iconCls:'icon-save',closable:false,fit:true,
                collapsible:true,minimizable:false,maximizable:false">
                <table>
                    <tr><td>File to upload:</td><td><input type="file" name="file" id="upfile" readonly="readonly"/></td></tr>
                    <a id="btn" class="easyui-linkbutton" data-options="iconCls:'icon-add'" οnclick="upload();">upload</a>
                </table>
            <div>
                <img id="loading" th:src="@{image/process.gif}" style="display: none;" height="200px" width="200px"/>
                <div id="file-list">
                </div>
                <div id="serverResponse"></div>
            </div>
        </div>
    </div>
</body>
</html>

2、启动,直接main方法就可以看到控制台的启动了,spring自己封装了很多日志,我使用的是logback,配置文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <!-- ch.qos.logback.core.ConsoleAppender 控制台输出 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- ch.qos.logback.core.rolling.TimeBasedRollingPolicy 文件输出 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/logFile.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>logs/logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!-- keep 30 days' worth of history capped at 3GB total size -->
            <maxHistory>30</maxHistory>
            <!--<totalSizeCap>3GB</totalSizeCap>-->
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE" />
        <appender-ref ref="CONSOLE" />
    </root>

</configuration>

另外 application.properties的配置项很多,可以自行按照需求配置。


server.port=8088


spring.thymeleaf.prefix=classpath:/templates/


multipart.maxFileSize=50Mb
multipart.maxRequestSize=50Mb

	demo建立了,实际就可以按照需求进行扩展了。另外单元测试与SSM框架很类似,只不过注解加载的是application类,而不是配置文件。

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: springboot+thymeleaf项目是一种基于Java语言开发的Web应用程序。它采用了Spring Boot框架和Thymeleaf模板引擎,可以快速地搭建一个高效、可靠、易于维护的Web应用程序。该项目具有以下特点: 1. 简单易用:Spring Boot框架提供了一系列的自动化配置,使得开发者可以快速地搭建一个Web应用程序,而不需要过多的配置。 2. 高效可靠:Spring Boot框架采用了一系列的优化措施,使得应用程序具有高效、可靠的性能。 3. 易于维护:Thymeleaf模板引擎提供了一种简单、易于维护的模板语言,使得开发者可以快速地开发出具有良好可读性的Web应用程序。 总之,springboot+thymeleaf项目是一种非常优秀的Web应用程序开发框架,可以帮助开发者快速地开发出高效、可靠、易于维护的Web应用程序。 ### 回答2: Spring Boot是一个基于Spring框架的快速开发框架,这个框架的优点在于其简单易用,能够快速搭建一个Java Web应用程序,无需进行复杂的配置和繁琐的XML文件编写。而Thymeleaf则是一种Web和HTML的模板引擎,可以方便地处理文本、链接和表单等元素,支持多重继承和页面片段的复用等特性。 Spring Boot和Thymeleaf的结合,可以帮助开发人员更加简便地搭建Web应用程序。在使用Spring Boot进行项目开发时,可以使用Thymeleaf来完成Web开发的视图层,进行模版板的渲染和数据绑定。这样就可以很直接地将数据通过模板引擎展现出来,且更加方便。 在一个Spring Boot Thymeleaf项目的构建中,需要进行如下步骤: 1. 首先,引入Spring Boot和Thymeleaf的依赖以及其他必要的依赖,例如web和mybatis等相关组件。 2. 创建一个Controller类,并使用@Controller注解将类标记为Controller,编写具体的Action方法,这些方法可以用@RequestMapping或@GetMapping等注解来定义处理请求的URL路径和请求类型等相关信息。 3. 创建一个Model类,用于封装需要传输到前端的数据和相关操作等。 4. 在Controller内部设置Model变量并将相关数据注入Model,然后将需要展现的数据作为参数传递给Thymeleaf进行渲染,最后将渲染完成后的结果返回给前端页面展现。 5. 编写HTML页面,使用Thymeleaf标签来渲染动态数据。 需要注意的是,在进行Thymeleaf模板的渲染时,需要遵守一定的规范,例如页面中的数据变量名称需与Model中的属性名称一致,引入Thymeleaf命名空间等等。 总之,Spring Boot与Thymeleaf结合使用可以帮助开发人员快速地完成Web开发,整个过程简单而且高效。使用Thymeleaf能够降低模版制作的门槛,进一步提高开发效率,并且能够提供丰富的模版处理标签,使得页面制作更加灵活。 ### 回答3: 近年来,使用SpringBootThymeleaf进行Web开发已经成为越来越多的开发者选择的方案。SpringBoot是一个基于Spring框架的快速Web应用开发框架,而Thymeleaf是一种基于HTML的模板引擎,其中需要了解的内容包括以下几点: 首先,SpringBoot框架的优点是非常明显的。它提供了很多便于使用的方法,例如自动装配,以及基于配置的许多默认值。这使得开发者可以花更少的时间和精力来开发项目,将重点放在业务逻辑和功能实现上。 其次,Thymeleaf是一种非常强大和灵活的模板引擎,其语法简单易懂,而且支持HTML5标准。它还提供了一些样式和布局的工具,以及易于使用的表达式和标签,使得Web页面开发更加容易。 当然,SpringBoot集成Thymeleaf的过程也并不复杂。只需添加thymeleaf-starter包依赖,SpringBoot将自动将Thymeleaf注册为默认的模板引擎。然后,您只需要编写Thymeleaf模板文件即可。 最后,值得注意的是,使用SpringBootThymeleaf进行Web开发的好处在于它们之间的紧密集成。这种紧密集成可以更轻松地创建动态和交互性的Web应用程序,这是传统的HTML和JavaScript不能提供的。 总的来说,SpringBootThymeleaf是一对非常强大且易于使用的Web开发工具组合,它们的出现大大提高了Web开发的效率和质量,同时也为开发人员提供了更好的开发体验。我们相信,这对于Web开发者来说是非常有价值的组合。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值