2015-11-15 73 views
0

你好,我是新來的春天,我試圖顯示一個簡單的HTML文件與百里香,但有問題渲染視圖。它只顯示字符串「index」,但不顯示index.html中的html內容。請能有人告訴我,我做錯了什麼百里香葉不渲染與春天mvc

web.xml file: 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 
    <display-name>Hotel Management System</display-name> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    </web-app> 

調度員servle.xml文件:

<context:component-scan base-package="org.myExample.controller" /> 

     <bean class ="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> 
     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> 
     <bean name="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"> 
      <property name="prefix" value="/WEB-INF/templates"/> 
      <property name="suffix" value=".html"/> 
      <property name="templateMode" value="HTML5"/> 
     </bean> 

     <bean name="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> 
      <property name="templateResolver" ref="templateResolver"/> 
     </bean> 

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> 
     <property name="templateEngine" ref="templateEngine" /> 
    </bean> 
     <mvc:annotation-driven /> 
</beans> 

TestController.java:

@RestController 
public class TestController { 


    @RequestMapping(value = "/") 
    public String printWelcome() { 

     return "index"; 
    } 
} 

index.html文件:在Web應用程序中定義/ templates/index.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initialscale=1"> 
    <title>Bootstrap 101 Template</title> 

    <!-- Bootstrap --> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous"> 

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 
    elements and media queries --> 
    <!-- WARNING: Respond.js doesn't work if you view the 
    page via file:// --> 
    <!--[if lt IE 9]> 
    <script src="https://oss.maxcdn.com/libs/html5shiv/ 
3.7.0/html5shiv.js"></script> 
    <script src="https://oss.maxcdn.com/libs/respond.js/ 
1.4.2/respond.min.js"></script> 
    <![endif]--> 

    <style> 

     .box { 

      background-color:#d3d3d3 

     } 

    </style> 
</head> 
<body> 
<h1>Hello, world!</h1> 

<div class="container"> 

    <div class="row"> 

     <div class="col-md-6 box">Content</div> 

     <div class="col-md-6 box">Content</div> 

    </div> 


</div> 

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/ 
jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script> 
</body> 
</html> 
+0

看着[this](https://spring.io/guides/gs/rest-service/)會有幾個理由我覺得很有價值。 – ChiefTwoPencils

+0

如果您是Spring的新手,請從https://start.spring.io中的Spring Boot和一個基本的入門項目(使用Web和Thymeleaf)開始。 – chrylis

+0

@chrylis我想了解xml配置並擁有更多控制權 –

回答

3

將@RestController註釋更改爲@Controller註釋。其餘控制器是字符串特定的。