Spring Boot中JSP文件modelAttribute无法解析userRequest问题
解决Spring Boot中JSP表单modelAttribute无法解析的问题
问题描述
使用Spring Boot开发Web应用时,希望将Web表单输入映射到UserRequest对象,但在JSP中使用<form:form method="POST" action="/" modelAttribute="userRequest">时,IntelliJ提示“Cannot resolve symbol 'userRequest'”,同时表单数据无法正确绑定到对象。
核心问题与修复方案
1. 控制器注解错误
你的RestService类使用了@Service注解,但处理HTTP请求的类需要用@Controller注解——@Service是业务层组件标记,不会被Spring MVC识别为请求控制器,导致@GetMapping和@PostMapping无法生效,model.addAttribute("userRequest", userRequest)根本不会执行,JSP自然找不到该属性。
修复后的RestService.java:
@Controller // 替换原有的@Service注解 public class RestService { @GetMapping("/") public String showForm(Model model){ UserRequest userRequest = new UserRequest(); model.addAttribute("userRequest",userRequest); List<String> danceStyles = Arrays.asList("Kizomba", "Salsa", "Bachata"); List<String> danceEvents = Arrays.asList("Festival", "Class", "Workshop"); List<String> distances = Arrays.asList("2_5km", "5km", "7_5km", "10km", "over10km"); model.addAttribute("danceStyles", danceStyles); model.addAttribute("eventType", danceEvents); model.addAttribute("distances", distances); return "index"; } @PostMapping("/") public String submitForm(@ModelAttribute("userRequest") UserRequest userRequest) { System.out.println(userRequest); return "register_success"; } }
2. 缺少JSP支持依赖
Spring Boot的spring-boot-starter-web默认不包含JSP相关依赖,需要手动添加:
修改pom.xml,新增以下依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
3. JSP表单路径与对象字段不匹配
你的JSP表单中存在多处path属性与UserRequest字段不匹配的问题,同时模型属性名称也不一致:
path="profession":UserRequest无此字段,需删除path="Dance Event":对应UserRequest的typeOfEvent字段,且后台添加的模型属性是eventType,不是danceEventspath="Dance Style":对应UserRequest的danceStyle字段
修复后的index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Find a Dance Event</title> </head> <body> <h2>Find your next Dance Party</h2> <form:form method="POST" action="/" modelAttribute="userRequest"> <form:label path="typeOfEvent">Event Type:</form:label> <form:select path="typeOfEvent" items="${eventType}" /><br/> <form:label path="danceStyle">Dance Style:</form:label> <form:select path="danceStyle" items="${danceStyles}" /><br/> <form:label path="street">Street:</form:label><br> <form:input path="street"/><br/> <input type="submit" value="Submit"> </form:form> </body> </html>
4. IntelliJ IDE提示修复
完成上述修改后,执行以下操作消除IDE提示:
- 点击IntelliJ Maven工具栏的「Reload All Maven Projects」,确保依赖加载完成
- 执行「Build → Clean Project」,再「Build → Rebuild Project」
- 若
UserRequest不在主应用类的包下,需在主类添加@ComponentScan指定扫描路径
内容的提问来源于stack exchange,提问作者CatiaV5
相关产品推荐
相关产品推荐

