My controller class is in com.tps.sphbIntegration.controllers package
My applicationContext.xml file is in WEB-INF/spring/applicationContext.xml
In the controller class:
@Controller
@RequestMapping("jsp")
public class SpringController {
@RequestMapping(value="register.html" , method = RequestMethod.POST)
public String enterSucess(@Valid Login login , BindingResult result , Map model,HttpSession session){
if(result.hasErrors()){
System.out.println("Error happened...");
return "register";
}else{
System.out.println("I am an controller for get method of jsp/success.html ");
login = (Login) model.get("login");
session.setAttribute("empId", login.getEmpId()) ;
session.setAttribute("empName", login.getEmpName()) ;
session.setAttribute("empPassword", login.getEmpPassword()) ;
//session.setAttribute("empGender", login.getGender()) ;
//session.setAttribute("empType", login.getEmpType()) ;
ApplicationContext factory = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
EmployeeDao dao=(EmployeeDao)factory.getBean("d");
dao.saveEmployee(login);
return "registerCheck";
}
}
}
When execution I got the exception as
java.io.FileNotFoundException: class path resource [spring/applicationContext.xml] cannot be opened because it does not exist
Please help me to set the path of applicationContext.xml in the controller or give some example that how to access the applicationContext.xml in controller.
You have to tell the servlet context loader listener where to find your Spring application context XML files in the web.xml. Your error suggests to me that you didn’t do that.
If you do have it in your web.xml, check the paths to see if they’re correct.
If the paths are correct, open the WAR file and see if the XML is missing. Perhaps you have a deployment and packaging issue.
A web app should NOT be calling this:
This will work if there’s a
spring/applicationContext.xmlin yourWEB-INF/classesdirectory, but the preferred idiom is to use theContextLoaderListener:Use a ContextLoaderListener in accordance with DispatchServlet
You ought to be loading the application context for your entire app on startup, not for one controller and certainly not every time this URL is called by clients. Load it once on startup.