2017-11-16 5 views
1

GETリクエストをマップされていないURLにマップして、index.htmlファイルを表示しようとしています。SpringブートでAngular2 Single PageのアプリケーションのHTMLファイルではなく文字列が表示される

@RequestMapping(method=RequestMethod.GET) 
public String redirectToIndex() { 
    return "index.html"; 
} 

マイindex.htmlresources/static/index.htmlである:

だから、私は@RestController次のように作成しました。

@RequestMapping(method=RequestMethod.GET) 
public String redirectToIndex() { 
    return "/static/index.html"; 
} 

しかしながら、両方の場合に、ブラウザーは文字通りストリングindex.html/static/index.htmlそれぞれの代わりに、HTMLファイルを示す例を示します。私は、次のコントローラを試みました。私はここで同様の質問に別のアプローチをたくさん試しましたが、解決することができません...助けてください。

EDIT:

マイ塗りつぶし@RequestControllerクラス:

@RestController 
public class FeedbackController { 
    @Autowired 
    private FeedbackService feedbackService; 

    @RequestMapping(method=RequestMethod.POST, value="/feedback") 
    public Feedback addFeedback(@RequestBody Feedback feedback) { 
     return feedbackService.addFeedback(feedback); 
    } 

    @RequestMapping(method=RequestMethod.GET) 
    public String redirectToIndex() { 
     return "index.html"; 
    } 

} 

マイpom.xml:@RestController注釈ResponseBodyで

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.8.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-data-jpa</artifactId> 
     </dependency> 
     <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> 

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

jspsまたはThymeleafを使用していますか? –

+0

HTMLのための角度 – swdon

答えて

1

が生成されます。この場合、文字列はビューのマッピングではなく文字列として生成されます。あなたのケースでは

あなたは@Controllerを使用する必要があります、あなたは、「インデックス」は以下

が、私はこれらを追加

の考えることができるコードでリダイレクトを使用するか、index.jspを適切に作成して返却する必要がありますあなたのapplication.propertiesに

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

/main/webapp/WEB-INF/viewフォルダの下にindex.jspを追加します。コントローラの場合、次のコードを追加してください。

@Controller 
public class MyController { 

    @RequestMapping("/index") 
    public String index() { 
     return "index" 
    } 
} 
+0

あなたは例を挙げることができますか? – swdon

0

SpringBootプロジェクトでテンプレートリゾルバとして使用している場合は、

例えば:

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

、その後のsrc /メイン/リソース/テンプレート内のあなたのindex.htmlを置きます。これは、Thymeleafが静的リソースを探す場所です。

あなたRestControllerは持っている必要があり、次の

@Controller 
public class FeedbackController { 
@Autowired 
private FeedbackService feedbackService; 

@RequestMapping(method=RequestMethod.POST, value="/feedback") 
@ResponseBody 
public Feedback addFeedback(@RequestBody Feedback feedback) { 
    return feedbackService.addFeedback(feedback); 
} 

@RequestMapping(method=RequestMethod.GET) 
public String redirectToIndex() { 
    return "index"; 
} 

} 
あなたはこのアクセスを持っているために、正しいセキュリティ設定を配置する必要があるかもしれません

関連する問題