[bigdata-046] eclipse+spring开发web然后部署到tomcat

1. 用eclipse开发spring,需要使用相关的ide

2. spring官网的ide
https://spring.io/tools/eclipse


3. spring的tool suite
https://spring.io/tools/sts
这个是专用开发spring的。
选择 https://spring.io/tools/sts/all 这里的linux64版本。

4. 在eclipse ee里创建spring测试项目
https://start.spring.io/
这个地方,可以用来生成一些spring的初始项目,不用自己手动写。然后导入到eclipse。

4.1 生成一个springdemo20170219,web项目,然后将它导入到eclipse ee里。
项目的目录结构
├── mvnw
├── mvnw.cmd
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── tanzhishuju
│   │   │           ├── SampleController.java
│   │   │           └── Springdemo20170219Application.java
│   │   ├── resources
│   │   │   ├── application.properties
│   │   │   ├── static
│   │   │   └── templates
│   │   │       └── greeting.html
│   │   └── webapp


4.2 在项目上点击右键,选择run as java application。
4.3 在浏览器输入localhost:8080,能看到一个url没有地址的报错页面,这个不要紧,正常的。至此,表明,可以正常运行。

5. 增加新功能
5.1 在包com.tanzhishuju上右键,选择增加一个新类SampleController。

其内容如下:

package com.tanzhishuju;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SampleController {
    
    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name",required=false,defaultValue="world") String name,Model model){
            model.addAttribute("name",name);
            return "greeting";
    }
}




5.2 在src/main/resources/templates下更加greeting.html文件
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Greeting started</title>
    <meta http-equiv="Content-Type" content="text/html; chareset=UTF-8"/>
  </head>

  <body>
    <p th:text="'hello,'+${name}+'!'"/>
  </body>
</html>


5.3 增加多个依赖jar包
spring-boot-starter-web
spring-boot-starter-test
spring-boot-devtools
spring-boot-starter-thymeleaf
javax.servlet-api


5.4 在项目上点击右键,选择run as -> java application。

5.5 在浏览器输入localhost:8080/greeting,能看到helloworld,表明运行成功。


6. 将5.项目做成war包,然后部署到tomcat
6.1 修改application.java成内容如下


package com.tanzhishuju;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Springdemo20170219Application extends SpringBootServletInitializer {

    /*
    public static void main(String[] args) {
        SpringApplication.run(Springdemo20170219Application.class,args);
    }
    */
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(Springdemo20170219Application.class);
    }
    
}
6.2 将项目export成war包 6.2.1 修改pom.xml,将打包格式修改成war,然后在项目上点击右键,选择maven-update,更新整个项目。 6.2.2 在项目上点击右键选择export成war文件,叫springdemo20170219.war 6.2.3 将springdemo20170219.war放到tomcat的webapps里,然后启动tomcat,也就是bin/startup.sh。 6.2.4 在浏览器地址栏输入localhost:8080/springdemo20170219/greeting,能看到hellowrold,表明成功。 7.至此,eclipse+springt+tomcat的一个完成开发流程就做完了。这些是核心精简流程,其他都是在这个核心流程上做增量。

相关文章

文章浏览阅读5.3k次,点赞10次,收藏39次。本章详细写了mysq...
文章浏览阅读1.8k次,点赞50次,收藏31次。本篇文章讲解Spar...
文章浏览阅读7.8k次,点赞9次,收藏34次。ES查询常用语法目录...
文章浏览阅读928次,点赞27次,收藏18次。
文章浏览阅读1.1k次,点赞24次,收藏24次。作用描述分布式协...
文章浏览阅读1.5k次,点赞26次,收藏29次。为贯彻执行集团数...