Are Are - 3 months ago 17
Java Question

NoClassDefFoundError in the Spring Mvc form handler

I work with a small Spring Mvc project and I get an error in to the time of handling a

POST
request. The error message is provided below,

HTTP Status 500 – Internal Server Error

Type Exception Report

Message An exception occurred processing JSP page [/WEB-INF/jsps/users/userform.jsp] at line [26]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP page [/WEB-INF/jsps/users/userform.jsp] at line [26]

23:
24: <spring:url value="/users" var="userActionUrl"/>
25:
26: <form:form class="form-horizontal" method="post" modelAttribute="userForm" action="${userActionUrl}">
27:
28: <form:hidden path="id"/>
29:


Root Cause

javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/springframework/web/util/ExpressionEvaluationUtils
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
org.apache.jsp.WEB_002dINF.jsps.users.userform_jsp._jspService(userform_jsp.java:1278)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


The landing page is here (
list.jsp
) and mapped to the
http://localhost:8080/users
,

enter image description here

The corresponding controller code,

@Autowired
private UserService userService;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String showHome() {

logger.debug("showHome()");

return "redirect:/users";
}

@RequestMapping(value = "/users", method = RequestMethod.GET)
public String showAllUsers(Model model) {

logger.debug("showAllUsers()");
model.addAttribute("users", userService.findAllUsers());
return "users/list";
}


When I press the
Add User
button, it directs to the address
http://localhost:8080/users/add
. The page shows the file
userform.jsp
and it get the error from there. The respective controller code is provided below,

@RequestMapping(value = "users/add", method = RequestMethod.GET)
public String showAddUserForm(Model model) {

logger.debug("showAddUserForm()");

User user = new User();

user.setName("mkyong123");
user.setEmail("test@gmail.com");
user.setAddress("abc 88");

//user.setPassword("123");
//user.setConfirmPassword("123");
user.setNewsletter(true);
user.setSex("M");
user.setFramework(new ArrayList<String>(Arrays.asList("Spring MVC", "GWT")));
user.setSkill(new ArrayList<String>(Arrays.asList("Spring", "Grails", "Groovy")));
user.setCountry("SG");
user.setNumber(2);

model.addAttribute("userForm", user);

populateDefaultModel(model);
return "users/userform";
}


The necessary code in the
userform.jsp
page is provided below,

<form:form class="form-horizontal" method="post" modelAttribute="userForm" action="${userActionUrl}">

<form:hidden path="id"/>

<spring:bind path="name">
<div class="form-group ${status.error ? 'has-error' : ''}">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<form:input path="name" type="text" class="form-control " id="name" placeholder="Name"/>
<form:errors path="name" class="control-label"/>
</div>
</div>
</spring:bind>

</form:form>


I get the error message in the line,

<form:form class="form-horizontal" method="post" modelAttribute="userForm" action="${userActionUrl}">


This is the controller method suppose to handle the
POST
request,

// save or update user
@RequestMapping(value = "/users", method = RequestMethod.POST)
public String saveOrUpdateUser(@ModelAttribute("userForm") @Validated User user,
BindingResult result, Model model, final RedirectAttributes redirectAttributes) {

logger.debug("saveOrUpdateUser() : {}", user);

if (result.hasErrors()) {
populateDefaultModel(model);
return "users/userform";
} else {
redirectAttributes.addFlashAttribute("css", "success");

if(user.isNew()){
redirectAttributes.addFlashAttribute("msg", "User added successfully!");
}else{
redirectAttributes.addFlashAttribute("msg", "User updated successfully!");
}

userService.saveOrUpdate(user);

// POST/REDIRECT/GET
return "redirect:/users/" + user.getId();

// POST/FORWARD/GET
// return "user/list";
}
}


What do I need to change to run it properly?

Answer Source

class ExpressionEvaluationUtils it's old class , before spring 4 this class was removed. If you see something like :

java.lang.NoClassDefFoundError: org/springframework/web/util/ExpressionEvaluationUtils

it means that you have some old version for spring in pom.xml (this class is not anymore in classpath /dependencies in the latest spring version). To solve this , just update dependencies to relevant versrion that you use.