Professional Documents
Culture Documents
Codes
Declaration:
Project Details:
Keywords:
Introduction:
Problem Statement:
To explain using a step by step process, the flow from the presentation layer
to the DAO layer, retrieve data from the database and display the retrieved
data on the presentation layer using Spring framework.
Solution:
Note: Sample code is given to this functionality; please make the changes
according to your requirement.
Assumptions:
function Validate()
{
var uName = document.sampleForm.userName.value;
var password = document.sampleForm.password.value;
if(uName=="" || password=="")
{
alert("Please enter both values to submit");
if(uName=="")
{
document.sampleForm.userName.focus();
return false;
}
else
{
document.sampleForm.password.focus();
return false;
}
}
else{
document.sampleForm.submit();
}
}
3. Now comes the main part – where is this JSP submitted to? There is no
action defined in the form. For this we will have to create an entry in the
web.xml file. (In terms of Struts - It is as good as creating our own
Struts-Config.xml file)
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
4. The next step is creating springapp-servlet.xml file which will contain the
beans and the wiring of the application. (Remember the name given here
should match exactly with the one given in the web.xml)
<beans>
<bean id="springappController" //bean for Controller
class="com.abc.strutspring.controller.SpringappController">
<property name="sessionForm"><value>true</value></property>
<property name="commandClass"><value>com.abc.strutspring.beans.LoginBean</value>
</property>
<property name="formView"><value>indexNew.jsp</value></property>
<property name="successView"><value>hello.jsp</value></property>
<property name="stringManipulator" ref="auth.service"/> //Reference to
Service interface.
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
</props>
</property>
</bean>
</bean>
<bean id="auth.service"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"
value="com.abc.strutspring.service.Login"/>
<property name="target" ref="auth.service.impl"/> //Reference to the
service implementation class.
</bean>
<bean id="auth.service.daoimpl"
class="com.abc.strutspring.dao.LoginDataManipulatorImpl">
<property name="dataSource"> <ref local="dataSourceDBDirect"/>
</property> //Reference to the dataSourceDBDirect bean which
contains the connection parameters.
</bean>
<bean id="authenticatorDAOService"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"
value="com.abc.strutspring.dao.LoginDataManipulator"/>
<property name="target" ref="auth.service.daoimpl"/>//Reference to
the DAO implementation class.
</bean>
<bean id="dataSourceDBDirect"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-
method="close">
<property name="driverClassName"
value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<property name="url" value="jdbc:odbc:DBNAME "/>
<property name="username" value="U@Name"/>
<property name="password" value="abc@1234"/>
</bean>//Connection Parameters
</beans>
// Do not directly connect your components and services together in code but
describe which services are needed by which components in a configuration file.
This concept is called Wiring.
5. Since the JSP submits itself to the Controller mentioned in the springapp-
servlet.xml file, the next step is to create the Controller itself.
/**Package Declaration**/
package com.abc.strutspring.controller;
/**Java Imports**/
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
/**Application Imports**/
import com.abc.strutspring.beans.LoginBean;
import com.abc.strutspring.service.Login;
/**
* Method onSubmit
* @param HttpServletRequest
* @param HttpServletResponse
* @param Object
* @param BindException
* @return ModelAndView
*
*/
/**
* @param login
*/
public void setStringManipulator(Login login) {
stringManipulator = login;
}
6. After the jsp is submitted, the control comes to the onSubmit method of
the controller class (Same as the execute method of the Struts Action
class). The data from the JSP is passed to this method using the command
object of the Object class. The Controller then typecasts the command
object of the Object class into the LoginBean object to access the attribute
values. The controller refers to the service interface using the reference of
the Service interface created in the controller class itself. Using the
reference of the service interface, the controller calls a method of the
Service class and passes the LoginBean object as an argument to this
method.
7. The Service interface contains the definitions of the methods in the
Service layer.
/**Package Declaration**/
package com.abc.strutspring.service;
/**Java Imports**/
import java.util.ArrayList;
import java.util.List;
/**Application Imports**/
import com.abc.strutspring.beans.LoginBean;
/**Package Declaration**/
package com.abc.strutspring.service;
/**Java Imports**/
import java.util.ArrayList;
import java.util.List;
/**Application Imports**/
import com.abc.strutspring.beans.LoginBean;
import com.abc.strutspring.dao.LoginDataManipulator;
/**
* Method carryData
* @param LoginBean
* @return String
*/
/**
* Method fetchData
* @param LoginBean
* @return List
*/
/**
* @param manipulator
*/
public void setAuthenticatorDAO(LoginDataManipulator manipulator) {
authenticatorDAO = manipulator;
}
9. The DAO interface contains the definitions of the methods in the DAO
layer.
/**Package Declaration**/
package com.abc.strutspring.dao;
/**Java Imports**/
import java.util.ArrayList;
import java.util.List;
/**Application Imports**/
import com.abc.strutspring.beans.LoginBean;
10. The DAO Implementation class receives the loginBean object from the
Service Implementation class and contains all the methods in which the
database transactions are performed. This class extends JdbcDaoSupport
class which is an inbuilt class of Spring framework which provides the
JDBC functionality. The query is fired by making an object of the
JdbcTemplate class which is again provided by the framework.
/**Package Declaration**/
package com.abc.strutspring.dao;
/**Java Imports**/
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultReader;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**Application Imports**/
import com.abc.strutspring.beans.LoginBean;
import com.abc.strutspring.dto.DataDTO;
/**
* Method carryData
* @param LoginBean
* @return String
*/
//String retValue =
loginBean.getUserName()+loginBean.getPassword();
String userName = loginBean.getUserName();
String retValue = userName+" : Following drugs are covered under
your plan";
return retValue;
}
/**
* Method fetchData
* @param LoginBean
* @return List
*/
/**
* Method mapRow
* @param ResultSet
* @param int
* @return Object
*/
11. The data is returned to the Controller in the form of a DTO object. (DTO
class contains the getter and setter methods of the fields on the JSP.)
12. The controller sets the received data in the session and returns the control
to the target jsp (return new ModelAndView (“target.jsp”);).
13. On the target JSP, the data is retrieved from the session and displayed to
the user using JSTL tags or Scriptlets.
References:
1. http://www-128.ibm.com/developerworks/library/wa-spring1/
2. http://www.devx.com/Java/Article/22134