build.gradle
buildscript {
ext {
springBootVersion = '1.5.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('org.postgresql:postgresql')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
GreetingController.java
package com.example.demo;
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 GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
ServletInitializer.java
package com.example.demo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
}
index.html
<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a href="/greeting">here</a></p>
</body>
</html>
greeting.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=org.postgresql.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/erp
spring.datasource.url=jdbc:postgresql://localhost:5432/erp
spring.datasource.username=postgres
spring.datasource.password=postgres
# Controller - View JSP.
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
# Logging. Not working.
logging.path=target/logs
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
# THYMELEAF (ThymeleafAutoConfiguration)
#spring.thymeleaf.cache=true # Enable template caching.
#spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
#spring.thymeleaf.check-template-location=true # Check that the templates location exists.
#spring.thymeleaf.content-type=text/html # Content-Type value.
#spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.
#spring.thymeleaf.encoding=UTF-8 # Template encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
#spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain.
spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
错误:
2017-05-22 19:29:24.484 INFO 9500 --- [ restartedMain] com.example.demo.DemoApplication : Starting DemoApplication on DESKTOP-TFU8BCE with PID 9500 (D:\erp\build\classes\main started by admin in D:\erp)
2017-05-22 19:29:24.486 INFO 9500 --- [ restartedMain] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2017-05-22 19:29:24.767 INFO 9500 --- [ restartedMain] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5747b69: startup date [Mon May 22 19:29:24 ICT 2017]; root of context hierarchy
2017-05-22 19:29:27.189 INFO 9500 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-05-22 19:29:27.740 INFO 9500 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-05-22 19:29:28.490 WARN 9500 --- [ restartedMain] .t.AbstractTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ # Prefix that gets prepended to view names when building a URL. (please add some templates or check your Thymeleaf configuration)
2017-05-22 19:29:28.613 INFO 9500 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2017-05-22 19:29:28.648 INFO 9500 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-05-22 19:29:28.657 INFO 9500 --- [ restartedMain] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2017-05-22 19:29:28.727 INFO 9500 --- [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 4.597 seconds (JVM running for 5.201)
2017-05-22 19:29:28.730 INFO 9500 --- [ Thread-9] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5747b69: startup date [Mon May 22 19:29:24 ICT 2017]; root of context hierarchy
2017-05-22 19:29:28.732 INFO 9500 --- [ Thread-9] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0
2017-05-22 19:29:28.734 INFO 9500 --- [ Thread-9] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2017-05-22 19:29:28.735 INFO 9500 --- [ Thread-9] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
Process finished with exit code 0
Closing JPA EntityManagerFactory for persistence unit 'default'
和Process finished with exit code 0
的平均值是什么?
我如何才能成功获取http://localhost:8080/greeting?