You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Spittr示例项目报错:/error无映射及home模板解析失败,求排查

Troubleshooting Your Spittr App's 500 Error & HomeController Question

Hey there! Let's break down the issues you're facing with your Spittr app from Spring in Action (4th Edition) Chapter 5.

First: Why You're Getting the "Failed to parse template 'home'" Error

That 500 internal server error boils down to Spring MVC being unable to locate or access the home template file. Here are the most common fixes to check:

1. Verify Template File Location & Naming

If you're using Thymeleaf (which the book uses for this example), Spring looks for templates by default in src/main/resources/templates/. Double-check:

  • Is there a file named home.html exactly in that directory?
  • Watch out for typos or capitalization mismatches (e.g., Home.html instead of home.html—this matters on Linux/macOS, even if Windows ignores it).

2. Ensure You Have the Right Dependencies

For Thymeleaf to work properly, make sure your build file includes the necessary starter dependency:

  • Maven (pom.xml):
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
  • Gradle (build.gradle):
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    

If you're using a traditional Spring MVC setup (not Spring Boot), you'll need to include Thymeleaf core and Spring integration dependencies instead.

3. Check View Resolver Configuration

If you manually configured a view resolver (common in non-Boot Spring MVC), confirm the prefix and suffix match your template location:
For Thymeleaf, your XML config might look like this:

<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/templates/"/>
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML"/>
</bean>

Make sure the prefix points to the actual folder where your home.html lives (e.g., /WEB-INF/templates/ if that's where you placed it).

Second: Why Isn't HomeController Passing the Request to the View?

Actually, your HomeController is handling the request—this error happens after the controller has done its job. Let me explain:
When your HomeController returns the logical view name "home", Spring MVC uses the configured view resolver to find the corresponding template file. If it can't find that file (which is what's happening here), it throws the 500 error you see.

First, confirm your controller code matches the book's example:

@Controller
public class HomeController {
    @RequestMapping("/")
    public String home() {
        return "home";
    }
}
  • Ensure it has the @Controller annotation (not @RestController, which would return JSON instead of a view).
  • The @RequestMapping("/") (or @GetMapping("/") in newer Spring versions) maps the root URL to the home() method, which returns the logical view name.

Once you fix the template location/dependency/configuration issues, the controller will successfully pass the request to the home.html view.

内容的提问来源于stack exchange,提问作者Khei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:47:06