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

Page 110

Advertising
background image

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();
int age = ((Employee) command).getAge();
String email = ((Employee) command).getEmail();
empdao.insertDetail(empid, firstname, lastname, age, email);

Map<String, String> model = new HashMap<String, String>();
model

110

Getting Started with Spring

Advertising
This manual is related to the following products: