2016-11-05 78 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); 
    } 

} 
相關問題