欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springBoot web开发环境搭建

程序员文章站 2024-02-25 14:34:09
...

基本环境

  1. 电脑:win10 64 位 8G内存
  2. maven的配置
Maven home: C:\software\apache-maven-3.5.2\bin\..
Java version: 1.7.0_67, vendor: Oracle Corporation
Java home: D:\Program Files\Java\jdk1.7.0_67\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "windows"
  1. jdk的配置
PS C:\Users\admin> java -version
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
PS C:\Users\admin> javac -version
javac 1.7.0_67

主要代码

  1. 首先新建maven web工程在pom.xml文件中配置需要的依赖
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ccqing</groupId>
    <artifactId>springboot_web</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_web Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <!-- 1.依赖于spring-boot-starter -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
    <!-- 2.完成全局的配置 覆盖默认的springboot配置 -->
    <properties>
        <java-version>1.7</java-version>
    </properties>
    <!-- 3.加载需要的插件(组件) -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>
</project>
  1. 控制器
package com.aaron;

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

@Controller
public class HelloWorld {

    @RequestMapping("/hello")
    public String printHello(Model model) {
        model.addAttribute("name1", "wangcheng");
        return "hello";

    }

}
  1. spring boot运行web应用
package com.aaron;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);

    }

}
  1. 属性文件
    属性文件名称必须是:application.properties
server.port=8080
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

注意事项

如果在 Window > Prefences > Server > Runtime Environment 下加载了外部tomcat
使用 @SpringBootApplication 运行时报错
错误是:Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;)V
解决办法如下

In Eclipse I went to Window > Prefences > Server > Runtime Environment and deleted the Tomcat 7 environment I previously added. After removing I was able to run a @SpringBootApplication from within another .jar file.

工程目录结构

springBoot web开发环境搭建