Modifying the web.xml file – HP Integrity NonStop J-Series User Manual
Page 70

3.
Add the following import statements to include the classes used in the controller.
import com.hp.empinfo.domain.Employee;
import com.hp.empinfo.service.EmployeeDao;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
After incorporating the above modifications, the EmployeeController.java controller
class must appear as:
package com.hp.empinfo.web;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import com.hp.empinfo.domain.Employee;
import com.hp.empinfo.service.EmployeeDao;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class EmployeeController extends SimpleFormController {
private EmployeeDao empdao;
public ModelAndView onSubmit(Object command) throws ServletException,
SQLException {
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext());
empdao = (EmployeeDao) wac.getBean("empdao");
int empid = ((Employee) command).getEmpid();
String firstname = ((Employee) command).getFirstname();
String lastname = ((Employee) command).getLastname();
int age = ((Employee) command).getAge();
String email = ((Employee) command).getEmail();
empdao.insertDetail(empid, firstname, lastname, age, email);
Map
model
.put("add",
"Transaction Complete - One Employee Added to Employee Database");
return new ModelAndView("insertresult", "model", model);
}
Modifying the web.xml File
1.
Add ContextLoaderListener to load the applicationContext.xml file under the
tag.
2.
Increase the startup load to 2 after adding the ContextLoaderListener to make
DispatcherServlet
as the secondary servlet class. The ContextLoaderListener
servlet class becomes the primary servlet class.
After modification, the web.xml file must appear as:
70
Spring Framework