我正在尝试使用spring& amp;创建简单的注册表单冬眠。当我尝试运行程序时,我得到了bindingresult的错误。
这是我的用户pojo类
package com.kalam.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
@Table(name="User")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long userId;
@Column
@NotEmpty(message="username can't be blank")
private String UserName = "";
@Column
@NotEmpty(message="password can't be blank")
private String Password = "";
@Column
@NotEmpty(message="Mobile can't be blank")
private int MobileNo;
@Enumerated(EnumType.ORDINAL)
private Role userRole;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public int getMobileNo() {
return MobileNo;
}
public void setMobileNo(int mobileNo) {
MobileNo = mobileNo;
}
public Role getUserRole() {
return userRole;
}
public void setUserRole(Role userRole) {
this.userRole = userRole;
}
}
这是User.jsp(注册表单)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration</title>
</head>
<body>
<div align="center">
<form:form action="/addUser" method="post" modelAttribute="userForm">
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Spring MVC Form Demo - Registration</h2></td>
</tr>
<tr>
<td>User Name:</td>
<td><form:input path="UserName" /></td>
</tr>
<tr>
<td>Password:</td>
<td><form:password path="Password" /></td>
</tr>
<tr>
<td>Mobile No:</td>
<td><form:input path="MobileNo" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Register" /></td>
</tr>
</table>
</form:form>
</div>
</body>
</html>
最后是Controller类。
package com.kalam.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.kalam.daoimpl.EmployeeDaoImpl;
import com.kalam.model.Employee;
import com.kalam.model.User;
import com.kalam.service.EmployeeService;
import com.kalam.serviceimpl.EmployeeServiceImpl;
@Controller
public class KalamController {
//@Autowired
//EmployeeService employeeService;
@RequestMapping("/kalam")
public String showMessage(ModelMap map) {
map.put("dollar", "50 US $");
return "KalamWorld";
}
@RequestMapping("/insertData")
public void InserData() {
Employee emp= new Employee();
emp.setEmpID(11);
emp.setEmpName("On Target");
emp.setEmpSalary(20000);
emp.setAddress("Mumbai");
ApplicationContext context=new ClassPathXmlApplicationContext("hibernate-cfg.xml");
EmployeeDaoImpl dao= (EmployeeDaoImpl)context.getBean("employeeDaoImpl");
dao.addEmployee(emp);
System.out.println("Data successfully inserted");
// employeeService.addEmployee(emp);
}
@RequestMapping(value ="/",method = RequestMethod.GET)
public String login(ModelMap model) {
User userFrom=new User();
model.put("userFrom", userFrom);
return "User";
}
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addEmployee(@Valid @ModelAttribute("userFrom")User user, ModelMap model) {
model.addAttribute("name", user.getUserName());
model.addAttribute("Id", user.getUserId());
model.addAttribute("Mobile", user.getMobileNo());
return "success";
}
@RequestMapping(value ="/welcome",method = RequestMethod.GET)
public String welcome(ModelMap model) {
return "User";
}
}
我希望在运行应用程序时将User.jsp页面显示为第一页。因此我试图调用@RequestMapping(value =“/”,method = RequestMethod.GET)处理程序。
但是当我尝试运行该程序时,我收到以下错误。
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userForm' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.WEB_002dINF.views.User_jsp._jspx_meth_form_005finput_005f0(User_jsp.java:220)
at org.apache.jsp.WEB_002dINF.views.User_jsp._jspx_meth_form_005fform_005f0(User_jsp.java:158)
at org.apache.jsp.WEB_002dINF.views.User_jsp._jspService(User_jsp.java:104)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:743)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:485)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:410)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:337)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Apr 16, 2018 9:25:25 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/KalamProject] threw exception [An exception occurred processing JSP page /WEB-INF/views/User.jsp at line 20
17: </tr>
18: <tr>
19: <td>User Name:</td>
20: <td><form:input path="UserName" /></td>
21: </tr>
22:
23: <tr>
请帮我解决错误。提前致谢
答案 0 :(得分:0)
在User
处理程序中返回视图名称之前,您需要初始化welcome
对象并将其放入模型中:
@RequestMapping(value ="/welcome", method = RequestMethod.GET)
public String welcome(ModelMap model) {
User user = new User();
model.put("userForm", user);
return "User";
}
或者你可以这样做以获得你想要实现的目标:
@RequestMapping(value ="/welcome", method = RequestMethod.GET)
public String welcome() {
return "redirect:/";
}
注意:强>
在login
处理程序中
model.put("userFrom", userFrom);
应该是
model.put("userForm", userFrom);
与User.jsp
...modelAttribute="userForm">
这也会显示相同的错误。