Spring Boot+Hibernate用户资料编辑功能报错求助(无Thymeleaf/Security)
Hey there, let's break down that 500 error you're hitting. The message "Neither BindingResult nor plain target object for bean name 'accountForm' available as request attribute" is Spring MVC's way of saying your JSP is trying to bind form fields to an object called accountForm, but that object isn't present in the request when the page loads (or when you submit it). Let's walk through the fixes step by step.
1. Ensure your controller injects the accountForm object when loading the profile page
When a logged-in user clicks "My Profile" to load editProfile.jsp, your GET request controller method needs to create the form object, populate it with the user's existing data, and add it to the request model. Here's how to do that:
@Controller public class ProfileController { @Autowired private UserRepository userRepository; // Or your user service layer @GetMapping("/editProfile") public String loadEditProfile(Model model, HttpSession session) { // Fetch logged-in user from session (since you aren't using Spring Security) Long loggedInUserId = (Long) session.getAttribute("loggedInUserId"); User user = userRepository.findById(loggedInUserId).orElseThrow(); // Create your form DTO and populate it with user data AccountForm accountForm = new AccountForm(); accountForm.setEmail(user.getEmail()); // Add other fields like username, display name, etc. // Add the form to the model with the EXACT name "accountForm" model.addAttribute("accountForm", accountForm); return "editProfile"; // Matches your JSP filename } }
Note: It's best practice to use a dedicated form DTO (like AccountForm) instead of your Hibernate User entity directly—this avoids issues with JPA annotations interfering with form binding.
2. Fix your JSP's form tag configuration
In editProfile.jsp, make sure your Spring form tag explicitly references the accountForm object using the modelAttribute attribute. Here's the correct setup:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <form:form method="post" modelAttribute="accountForm" action="/updateProfile"> <div class="form-group"> <label for="email">Email:</label> <form:input path="email" id="email" /> <!-- Add other form fields with matching `path` values --> </div> <button type="submit">Save Changes</button> </form:form>
If you omit modelAttribute, Spring will default to looking for an object named command, which won't exist in your case—this is a super common mistake.
3. Correct your POST controller for form submission
When the user submits the form, your POST method needs to accept the accountForm object, and must include a BindingResult parameter immediately after it (order matters here!). This handles validation errors and prevents the 500 error from reoccurring:
@PostMapping("/updateProfile") public String updateProfile( @ModelAttribute("accountForm") AccountForm accountForm, BindingResult bindingResult, HttpSession session) { // First check for form binding errors (e.g., invalid email format) if (bindingResult.hasErrors()) { // Return to the edit page with the form object still in the model return "editProfile"; } // Update the user's data in the database Long loggedInUserId = (Long) session.getAttribute("loggedInUserId"); User user = userRepository.findById(loggedInUserId).orElseThrow(); user.setEmail(accountForm.getEmail()); // Update other fields as needed userRepository.save(user); // Redirect to a success page (use redirect to avoid duplicate form submissions) return "redirect:/welcome"; }
The BindingResult parameter has to come right after the @ModelAttribute parameter—if you put other parameters in between, Spring won't associate binding errors with the form object, which will trigger the same error.
4. Double-check your JSP view resolver configuration
Since you're using Spring Boot with JSP, make sure your application.properties has the correct view resolver setup:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
This tells Spring where to find your JSP files—without it, the controller won't be able to locate editProfile.jsp.
5. Quick sanity checks
- Spelling matters: Ensure the name
accountFormis identical everywhere (controller model attribute, JSPmodelAttribute, and POST@ModelAttribute). Case sensitivity counts! - Field matching: The
pathattribute in your JSP form inputs must exactly match the field names in yourAccountFormclass (e.g.,path="email"maps toaccountForm.getEmail()/setEmail()). - Session user retrieval: Confirm you're correctly storing/fetching the logged-in user's ID in the session—if this fails, you won't be able to populate the form with existing user data.
内容的提问来源于stack exchange,提问作者Donobuz

