项目搭建
- 
选择创建项目类型
File -> New -> Project ->Maven
注:GroupId 包名路径
 - 
创建 application.yml
resources -> New -> File 输入:application.yml
注:配置路径需要 :加空格方式 
server :
  ip: 127.0.0.1
  port: 8877
  context-path: /
  tomcat:
     uri-encoding: UTF-8
     max-threads: 800
     access-log-enabled: true
     #access-log-pattern: '%h %l %u %t "%r" %s %b %D'
     access-log-pattern: '%{X-Real-IP}i %l %u %t "%r" %s %b'
     accesslog.pattern: combined
     basedir: logback_logs
spring:
  freemarker:
    settings:
      number_format: 0.##
    cache: false
    template-loader-path: ["classpath:/templates"]
- 创建启动入口类
注:需要在GroupId 包名的路径下创建 
package com.yellow;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableScheduling
public class Main {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder().sources(Main.class).web(true).run(args);
    }
}
- 创建index访问控制器
@RequestMapping 配置访问路径
@Controller 视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面
@ResetController = @Controller + @ResponseBody
当 访问 http://localhost:8877/默认请求 index方法,并调用 index.ftl 模版页面,通过 ModelMap进行数据传递
注:模版路径在application.yml 配置,即:resources/templates 
package com.yellow.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
@RequestMapping(value = "/")
public class IndexController {
    
    @RequestMapping(value = "/")
    public  String index(ModelMap modelMap) throws Exception {
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        modelMap.put("time", time);
        modelMap.put("text","hello wrold");
        return "index";
    }
}
- 创建index.ftl 模版
 
<!DOCTYPE html>
<html lang="en">
<style></style>
<head>
    <meta charset="utf-8" />
    <title>daily</title>
    <style type="text/css">
    </style>
</head>
<body>
<h1 style="text-align: center;color: green;">Index</h1>
<div style="padding:8px;text-align: left;font-size: 20px;border: 1px solid #999999;">
    <br>time:<strong style="color: royalblue;">${time!}</strong>
    <br>text:<strong style="color: royalblue;">${text!}</strong>
</div>
</body>
</html>
- 
创建好后的工程目录结构
 - 
配置启动项
选择 MainController 为启动入口控制
 - 
运行
说明运行成功,可以直接通过浏览器访问服务,就可以看到效果了
http://localhost:8877/ 
打包应用
- 双击 package,执行打包
 - target目录输出相应的 xxx.jar 包
 
上传服务器
具体步骤参考:https://www.jianshu.com/p/334eaf77b314
服务器运行部署
直接运行java -jar xxx.jar 即可运行相应的服务器,然后就可以在本地浏览器访问http://xxx.xxx.xxx.xxx:xxx/xxx/index
让服务在服务器一直处于运行状态
- java -jar xxx.jar
 - ctrl+z 退出到控制台,执行 bg
 - exit
 
- java -jar xxx.jar & 处于后台运行
 - 
nohup java -jar xxxx.jar &
推荐使用 nohup java -jar xxxx.jar & 











网友评论