Spring Boot中Controller与Mustache映射异常及白标错误页解决求助
问题解决方案
核心问题分析
你遇到的两个问题根源清晰:
- 访问
http://localhost:8080出现白标错误:Controller误用@RestController注解,导致返回的"main"被直接作为字符串响应,而非视图名匹配Mustache模板;同时Repository泛型与实体类ID类型不匹配,引发数据查询失败,进一步触发错误。 - 访问
http://localhost:8080/greeting返回字符串"greeting":同样是@RestController的问题,它会直接输出方法返回值,不会渲染对应的Mustache模板。
具体修复步骤
1. 替换Controller注解
将@RestController改为@Controller,让Spring把方法返回的字符串视为视图名称,去匹配templates目录下的Mustache模板:
import com.work.Dormitory.domain.Information; import com.work.Dormitory.repositories.InfoRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.Map; @Controller // 替换@RestController为@Controller public class GreetingController { @Autowired private InfoRepository infoRepository; @GetMapping("/greeting") public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) { model.addAttribute("name", name); return "greeting"; } @GetMapping public String main(Map<String,Object> model){ Iterable<Information> information = infoRepository.findAll(); model.put("information", information); return "main"; } @PostMapping public String add(@RequestParam String numberDormitory, @RequestParam String room, Map<String,Object> model){ Information information = new Information(numberDormitory, room); infoRepository.save(information); Iterable<Information> info = infoRepository.findAll(); model.put("information", info); // 统一模型变量名,与模板对应 return "main"; } }
2. 修正Repository泛型类型
实体类Information的ID类型是Integer,但Repository泛型用了Long,会导致数据操作报错,修改如下:
import com.work.Dormitory.domain.Information; import org.springframework.data.repository.CrudRepository; // 将Long改为Integer,匹配实体类ID类型 public interface InfoRepository extends CrudRepository<Information, Integer> { }
3. 修正Mustache模板变量名
main.mustache中使用了{{#messages}},但Controller传入的模型变量是information,同时移除无效的Thymeleaf命名空间(你用的是Mustache,不需要该命名空间):
<!DOCTYPE HTML> <html> <body> <div> <form method="post"> <input type="text" name="numberDormitory" placeholder="Write number of dormitory" /> <input type="text" name="room" placeholder="Number of room: "> <button type="submit"> Add </button> </form> </div> <div>list </div> {{#information}} <!-- 改为与Controller中一致的变量名 --> <div> <b>{{id}}</b> <span>{{room}}</span> <i>{{dormitoryNumber}}</i> </div> {{/information}} </body> </html>
4. 确保模板文件位置正确
Mustache模板必须放在src/main/resources/templates/目录下,同时需要创建greeting.mustache模板文件(示例内容如下):
<!DOCTYPE HTML> <html> <body> <h1>Hello, {{name}}!</h1> </body> </html>
5. 数据库配置验证(可选)
如果使用MySQL,确保application.properties中有正确的配置:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.jpa.hibernate.ddl-auto=update
验证修复
重启应用后:
- 访问
http://localhost:8080:可正常显示表单和数据列表 - 访问
http://localhost:8080/greeting:显示"Hello, World!";访问http://localhost:8080/greeting?name=Ira则显示"Hello, Ira!"
内容的提问来源于stack exchange,提问作者Ira
相关产品推荐
相关产品推荐

