Improving the controller – HP Integrity NonStop H-Series User Manual
Page 125

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
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">
<bean name="/insert.htm" class="com.hp.empinfo.web.EmployeeController">
<property name="formView">
<value>insert</value>
</property>
<property name="commandClass">
<value>com.hp.empinfo.domain.Employee</value>
</property>
<property name="commandName">
<value>emp</value>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Improving the Controller
Modify the
EmployeeController.java
file to perform the employee database transactions.
To modify the
EmployeeController.java
file, complete the following steps:
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();
Overview of EmpInfo
125