2017-08-06 202 views
1

我在春季啓動新來渲染JSP,我想先從一個簡單的例子,但似乎沒有任何工作 這是我的控制器:無法使用Spring引導

Package controller; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
@Controller 
public class testController { 
RequestMapping("view/aboutus") 
public String greeting(Model model) { 
model.addAttribute("name", "azertyyyy"); 
    return "aboutus"; }} 

,這是JSP視圖:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8"%> 
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
<!DOCTYPE> 
<html> <body> ${name} 


<p>hello world</p> </body></html> 

我添加所需依賴關係:

<dependency> 
<groupId>javax.servlet</groupId> 
<artifactId>jstl</artifactId> 
<version>1.2</version> 


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

和我添加前綴和後綴在application.properties

當我運行應用程序只有你好世界顯示在沒有名稱變量的視圖,在這裏搜索後,我將視圖目錄從src/main/webapp/view更改爲src/main/resource /模板,但彈簧引導無法找到視圖

+0

我可以添加一些細節,如果有什麼不明確 –

回答

2

我不確定您是否完全知道我們必須作出的變化,當我們想運行應用程序作爲戰爭包。

  1. 將包裝更改爲從jar打包並需要依賴關係。
  2. 更改主應用程序類擴展SpringBootServletInitializer並實現配置()
  3. 定義JSP頁面的路徑應用性能
  4. 把JSP頁面中正確的路徑(web應用/ WEB-INF/jsp的檢查截屏)

該項目

Directory Structure 以下目錄結構是代碼的變化我必須做的運行JSP

的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.test</groupId> 
    <artifactId>Test_Exception_Framework</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>Test_Exception_Framework</name> 
    <description>Project to test ExceptionFramework</description> 


    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.4.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> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency> 


     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
     </dependency> 

     <!-- Need this to compile JSP --> 
     <dependency> 
      <groupId>org.apache.tomcat.embed</groupId> 
      <artifactId>tomcat-embed-jasper</artifactId> 
      <scope>provided</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.eclipse.jdt.core.compiler</groupId> 
      <artifactId>ecj</artifactId> 
      <version>4.6.1</version> 
      <scope>provided</scope> 
     </dependency> 

    </dependencies> 

    <build> 
     <plugins> 

      <plugin> 
       <groupId>org.jacoco</groupId> 
       <artifactId>jacoco-maven-plugin</artifactId> 
       <version>0.7.5.201505241946</version> 
       <executions> 
        <execution> 
         <id>default-prepare-agent</id> 
         <goals> 
          <goal>prepare-agent</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>default-report</id> 
         <phase>prepare-package</phase> 
         <goals> 
          <goal>report</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

主類

package com.exception; 

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

    public static void main(String[] args) { 

     SpringApplication.run(SpringBootWebApplication.class, args); 
    } 

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


} 

休息控制器 - 它重定向到JSP

package com.exception.controller; 

import java.util.Map; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
@RequestMapping(method = RequestMethod.GET) 
public class TestExceptionController { 

    @RequestMapping("/") 
    public String welcome(Map<String, Object> model) { 

     System.out.println("in controller"); 

     model.put("message", "hello spring boot"); 

     return "welcome"; 
    } 

} 

application.properties - 定義對JSP

server.port=8085 
spring.mvc.view.prefix: /WEB-INF/jsp/ 
spring.mvc.view.suffix: .jsp 
路徑
+0

謝謝你現在的作品 –