2017-10-13 11 views
1

我在設置一個非常簡單的Spring啓動應用程序時遇到了問題。我嘗試了多個教程,Spring MVC,但沒有人會打開我的jsp。需要幫助在IntelliJ IDEA中使用Maven設置簡單的Spring Boot jsp應用程序

我目前能夠用Initializr設置一個項目,並且我理解@RequestMapping的概念。我能夠使用Spring Boot運行我的Tomcat服務器,但是當我打開我的http://localhost:8080/home/時,出現404錯誤的默認錯誤頁面。

感謝您的幫助。

我的項目結構: https://imgur.com/pPhngNS

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.shaddox</groupId> 
    <artifactId>spring-boot-demo</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>spring-boot-demo</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.7.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 


</project> 

HelloController.java:

package springbootdemo; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
@Controller 
public class HelloController { 
    @RequestMapping("/") 
    public String showPage() { 
     return "home"; 
    } 
} 

SpringBootDemoApplication.java:

package springbootdemo; 

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 SpringBootDemoApplication extends SpringBootServletInitializer { 

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

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

application.properties:

spring.mvc.view.prefix=/WEB-INF/ 
spring.mvc.view.suffix=.jsp 

回到Home.jsp:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Hello</title> 
</head> 
<body> 

<h2> Welcome to Spring Boot from JSP!</h2> 

</body> 

</html> 

回答

0

首先改變你的POM文件是這樣的:

<dependency> 
      <groupId>org.apache.tomcat.embed</groupId> 
      <artifactId>tomcat-embed-jasper</artifactId> 

     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
     </dependency> 

然後添加配置類:

import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 
import org.springframework.web.servlet.view.JstlView; 

@Configuration 
@EnableWebMvc 
@ComponentScan 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void configureViewResolvers(ViewResolverRegistry registry) { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("/WEB-INF/"); 
     resolver.setSuffix(".jsp"); 
     resolver.setViewClass(JstlView.class); 
     registry.viewResolver(resolver); 
    } 


} 
+0

非常感謝你,我一直在努力爭取2 天! –

+0

不客氣 – mrtasln

相關問題