2016-11-05 84 views
0

我有结构的项目:没有看到网页在http://本地主机:8080 /问候

enter image description here

build.gradle

buildscript { 
    ext { 
     springBootVersion = '1.4.1.RELEASE' 
    } 
    repositories { 
     mavenCentral() 
     mavenLocal() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 

apply plugin: 'war' 
apply plugin: 'spring-boot' 

jar { 
    baseName = 'accouting' 
    version = '0.0.1-SNAPSHOT' 
} 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

repositories { 
    mavenCentral() 
    mavenLocal() 
} 

dependencies { 
    compile('org.springframework.boot:spring-boot-starter-data-jpa') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile("org.springframework.boot:spring-boot-starter-thymeleaf") 
    compile("org.springframework.boot:spring-boot-devtools") 
    compile('com.oracle:ojdbc6:11.2.0.4') 
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') 
    testCompile('org.springframework.boot:spring-boot-starter-test') 
} 

Application.java

package com.example; 

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

@SpringBootApplication 
public class Application extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(Application.class, args); 
    } 

} 

GreetingController.java

package hello; 

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"; 
    } 

} 

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> 

application.properties

#Basic Spring Boot Config for Oracle 
spring.datasource.url=jdbc:oracle:thin:@//192.168.1.42:1521/xe 
spring.datasource.username=system 
spring.datasource.password=123456 
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver 
datasource.mine.poolSize=30 

# Hibernate config 
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect 
spring.mvc.view.prefix=/ 
spring.mvc.view.suffix=.jsp 

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> 

我没有看到网页在http://localhost:8080/greeting,如何解决呢?

+0

题外话。但是,我可以知道这是什么sts .... <3 – Jay

+0

@Jay Spring Tool Suite是一个为Spring应用程序开发人员设计的Eclipse定制版本。 –

+0

@DoNhuVy看来你建立一个战争文件。你是否将其部署到Tomcat?日志是否表明应用程序已成功启动?如果可以的话,分享一个片段。 –

回答

2

Your GreetingController位于另一个包中。将其移动到com.example,这应该可以解决您的问题。

如果您仍想保留的封装结构,使用@ComponentScan(basePackages = "hello")我们有:

package com.example; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.annotation.ComponentScan; 

@SpringBootApplication 
@ComponentScan(basePackages = "hello") 
public class Application extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(Application.class, args); 
    } 

} 
相关问题