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



    4.0.0

    org.xiehf.own
    ToBePro
    1.0-SNAPSHOT
    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.6.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-hateoas
        
        
            org.springframework.boot
            spring-boot-devtools
            true
        

        
            org.projectlombok
            lombok
            1.16.6
        
    

    
        1.8
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



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也能浏览了。





    
    home page
    
    
    
    
    
    
    


    
upload
File to upload:

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



    
    
        
            %-5level %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n
        
    

    
    
        logs/logFile.log
        
            
            logs/logFile.%d{yyyy-MM-dd}.log
            
            30
            
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        
    

    
        
        
    



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


server.port=8088


spring.thymeleaf.prefix=classpath:/templates/


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

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

你可能感兴趣的:(thymeleaf)