Spring Boot整合Velocity模板报错:无法找到资源文件求助
Hey there, let's dig into why you're hitting that Unable to find resource '/VBS Template Files/StandardVariables.vm' error when integrating Velocity into your Spring Boot app. Here are the key checks and fixes to sort this out:
1. Double-Check Your Velocity Configuration
First, make sure your Spring Boot properties are pointing to the right template directory. Velocity relies on the resource-loader-path setting to locate templates.
If your VBS Template Files folder lives under src/main/resources, add this to your application.properties:
spring.velocity.resource-loader-path=classpath:/VBS Template Files/ spring.velocity.suffix=.vm
Or if you prefer YAML (application.yml):
spring: velocity: resource-loader-path: classpath:/VBS Template Files/ suffix: .vm
Pro tip: Don't skip the trailing slash in the path — it ensures Velocity looks inside the folder, not at the folder itself.
2. Confirm Your Resource Folder Setup
Spring Boot only automatically loads resources from the src/main/resources directory. Verify these two things:
- Your
VBS Template Filesfolder is directly undersrc/main/resources(not tucked insrc/main/javaor the project root). - In Eclipse, right-click the folder → Build Path → Use as Source Folder. This tells Eclipse to copy the templates to the
target/classesdirectory during builds, so they end up in the classpath where Velocity can find them.
3. Fix Path Problems: Spaces & Case Sensitivity
Velocity's resource loader is surprisingly picky about these details:
- Spaces in folder names: Spaces can mess up path resolution. Try renaming
VBS Template FilestoVBS_Template_FilesorVBSTemplateFiles, then update your configuration and any template references to match the new name. - Case sensitivity: On Linux/macOS (and even some Windows setups), file paths are case-sensitive. Make sure the folder name and
StandardVariables.vmfilename match exactly — no typos likestandardvariables.vminstead of the capitalized version.
4. Check Template References
If you're using #parse or #include to reference StandardVariables.vm from another template, the path needs to align with your configured resource-loader-path:
- If your
resource-loader-pathisclasspath:/VBS Template Files/, use:#parse("StandardVariables.vm") - If your
resource-loader-pathisclasspath:/, use:#parse("/VBS Template Files/StandardVariables.vm")
Avoid mixing up absolute and relative paths here — that's a common pitfall.
5. Don't Sweat the Controller Package Location
Your controller being in a different package doesn't affect Velocity's ability to find templates. This issue has nothing to do with controller placement — it's strictly about resource configuration and path resolution.
6. Validate Resources Are Packaged Correctly
Build your project and check the target/classes directory (or unpack the generated JAR file). You should see your VBS Template Files folder and StandardVariables.vm inside it. If they're missing, Eclipse isn't copying them over — head back to step 2 to mark the folder as a Source Folder.
内容的提问来源于stack exchange,提问作者hell_storm2004

