my issue is different form apoorvabade's issue . sorry for the wrong statement in my previous question.
thanks for the immediate reply .here is my controller , mvc-config.xml ,app-config.xml.employeebeans.xml and employeeList.jsp .
All get requests are working fine. when i added post related code, when accessing http://localhost:8080/myfirstspring3/emp i am getting the error
"Neither BindingResult nor plain target object for bean name 'employee' available as request attribute" Appreciate your help.
EmployeeController.java
Code:/* all imports */ @Controller @RequestMapping(value="/emp") public class EmployeeController { private Map<Integer, Employee> accounts = new ConcurrentHashMap<Integer, Employee>(); private Validator validator; @Autowired private EmployeeDao employeeDao; //setters getters for employeeDao @Autowired public EmployeeController(Validator validator) {this.validator = validator;} @RequestMapping(method=RequestMethod.GET) public ModelAndView getEmployeeList(Model model) { String now = (new Date()).toString(); Map<String, Object> myModel = new HashMap<String, Object>(); myModel.put("now", now); myModel.put("employees", getEmployeeDao().getEmployees()); return new ModelAndView("emp/employeeList", "model", myModel); } @RequestMapping(value="{empid}", method=RequestMethod.GET) public @ResponseBody Employee get(@PathVariable int empid) { Employee emp = getEmployeeDao().getEmployee(empid); if (emp == null) { throw new ResourceNotFoundException(empid); } return emp; } @RequestMapping(method=RequestMethod.POST) public @ResponseBody Map<String,? extends Object> processForm(@RequestBody @ModelAttribute("employee") Employee employee, BindingResult result, Map model) { Set<ConstraintViolation<Employee>> failures = validator.validate(employee); if (!failures.isEmpty()) { //response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return validationMessages(failures); //return null; } else { getEmployeeDao().updateEmployee(employee); return Collections.singletonMap("message","success"); } } private Map<String, String> validationMessages(Set<ConstraintViolation<Employee>> failures) { Map<String, String> failureMessages = new HashMap<String, String>(); for (ConstraintViolation<Employee> failure : failures) { failureMessages.put(failure.getPropertyPath().toString(), failure.getMessage()); } return failureMessages; } }
app-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.abc.basic" />
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.Reloada bleResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
<!-- Configures Spring MVC -->
<import resource="mvc-config.xml" />
<import resource="employee-beans.xml" />
</beans>
employee-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.springframework.beans.factory.annotatio n.RequiredAnnotationBeanPostProcessor"/>
<bean id = "employeeDao" class= "com.abc.basic.crud.hardcoded.EmployeeDaoImpl" />
<bean id = "employee" class= "com.abc.basic.crud.Employee" />
</beans>
mvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="welcome"/>
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.Locale ChangeInterceptor" />
</mvc:interceptors>
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.Cookie LocaleResolver" />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>


Reply With Quote