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

如何解决java.lang.IllegalStateException?附Spring MVC相关代码

Fixing java.lang.IllegalStateException in Your Spring MVC User Management Project

Let's break down what's causing that java.lang.IllegalStateException in your Spring MVC project and fix it step by step. This exception usually pops up when there's a problem with request mapping, view rendering, model data binding, or bean initialization. Let's go through your code line by line:

1. Missing Request Mapping for memberform()

Your memberform() method in UserController doesn't have a @RequestMapping annotation. Without this, Spring can't map any HTTP requests to this method, which will cause routing failures and trigger exceptions if your JSP tries to access this functionality.

Fix: Add a @RequestMapping annotation to the method:

@RequestMapping("/Member")
public ModelAndView memberform() {
    List<Membership> members = memberDao.list();
    ModelAndView obj1 = new ModelAndView("User");
    obj1.addObject("member", new Membership());
    obj1.addObject("Members", members);
    return obj1;
}

2. Incorrect Way to Pass Messages on Redirect

You're using ModelAndView.addObject() to pass messages (Msg1, Msg2) after a redirect. But redirects trigger a new HTTP request, so these attributes won't carry over to the new request. This can cause unexpected behavior and exceptions when your JSP tries to access missing attributes.

Fix: Use RedirectAttributes to add flash attributes (these persist across redirects):

@RequestMapping("/addusr")
public String addusr(@ModelAttribute("user") UserInfo usr, RedirectAttributes redirectAttributes) {
    if(userDao.saveorupdate(usr)) {
        redirectAttributes.addFlashAttribute("Msg1", "User added Successfully");
    } else {
        redirectAttributes.addFlashAttribute("Msg2", "User not added");
    }
    return "redirect:/User";
}

@RequestMapping("/addmember")
public String addmember(@ModelAttribute("member") Membership member, RedirectAttributes redirectAttributes) {
    if(memberDao.saveorupdate(member)) {
        redirectAttributes.addFlashAttribute("Msg1", "Membership User added Successfully");
    } else {
        redirectAttributes.addFlashAttribute("Msg2", "Membership User not added");
    }
    return "redirect:/User";
}

You can then access these messages in your JSP with ${Msg1} or ${Msg2}.

3. Wrong JSTL Tag Library URI

Your JSP uses an incorrect URI for the JSTL core taglib:

<%@ taglib prefix="c" uri="http://www.springframework.org/tags" %>

This will cause tag parsing failures during JSP rendering, leading to IllegalStateException.

Fix: Replace it with the correct JSTL core URI:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

4. Form Binding Issues

A few issues in your JSP form can trigger data binding exceptions that cascade into IllegalStateException:

  • Unnecessary name attributes: Spring's <form:input> generates the correct name attribute from the path value—you don't need to add it manually, which can cause conflicts.
  • Typos in gender option: "Femal" should be "Female" (this might not directly throw the exception, but it's a data consistency issue).
  • Confirm password binding: Ensure your UserInfo class has a Confirmpassword field with proper getter/setter methods. If you don't need to bind this to the model, use a plain HTML <input> instead of <form:input>.

Fix the form code:

<form:form action="${usr}" method="post" commandName="user">
    <form:input type="text" path="First_name" placeholder="First Name"/>
    <form:input type="text" path="Last_name" placeholder="Last Name"/>
    <form:input type="text" path="Email" placeholder="Email"/>
    <form:input type="text" path="Location" placeholder="Location"/>
    <form:input type="password" path="password" placeholder="Password"/>
    <form:input type="password" path="Confirmpassword" placeholder="Confirm Password"/>
    <p><font face="'Merienda', cursive" align="left">Gender</font></p>
    <div style="font-weight: normal;font-style:'Open Sans', sans-serif; font-size:17px">
        <form:radiobutton path="gender" value="Male"/>Male
        <form:radiobutton path="gender" value="female"/>Female
    </div>
    <!-- Don't forget to add a submit button! Your form is missing one -->
    <button type="submit">Sign Up</button>
</form:form>

Note: I added a submit button since your original form didn't have one—without it, users can't submit the form.

5. Incorrect @Autowired for Entity Classes

You're autowiring UserInfo and Membership entities directly in your controller:

@Autowired UserInfo userInfo;
@Autowired Membership membership;

Entities are usually plain POJOs, not Spring-managed beans (unless you explicitly configure them as such). Autowiring them will fail during controller initialization, leading to IllegalStateException.

Fix: Remove these autowires and instantiate the objects directly where needed:

// Remove these lines:
// @Autowired UserInfo userInfo;
// @Autowired Membership membership;

// In userform():
obj.addObject("user", new UserInfo());

// In memberform():
obj1.addObject("member", new Membership());

Final Checks

  • Verify your view resolver is configured correctly (ensure User.jsp is in the right location, e.g., WEB-INF/views/ if you're using InternalResourceViewResolver).
  • Check the full stack trace of the IllegalStateException to pinpoint the exact cause (e.g., if it's a view not found error or a bean initialization failure).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:54:26