beautypg.com

Improving the controller – HP Integrity NonStop J-Series User Manual

Page 69

background image

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


insert


com.hp.empinfo.domain.Employee


emp


class="org.springframework.web.servlet.view.InternalResourceViewResolver">




Improving the Controller

Modify the EmployeeController.java file as follows to perform the employee database
transactions.
1.

Add a reference to the EmployeeDao.java in the EmployeeController.java controller
class as shown below:

private EmployeeDao empdao;

2.

Replace the onSubmit method with the handleRequest method.

This is done to:

1.

Recognize the inputs from the View

2.

Pass the recognized inputs to the Model

3.

Pass the output back to the View

Before modification:

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

String now = (new Date()).toString();
logger.info("Returning view with " + now);
return new ModelAndView("insert", "now", now);
}

After modification:

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 = new HashMap();
model
.put("add",
"Transaction Complete - One Employee Added to Employee Database");
return new ModelAndView("insertresult", "model", model);
}

Getting Started with Spring

69

This manual is related to the following products: