I'm new to spring and hibernate.I've created a project and here is what it does. The application has the form elements in JSP which redirects to the Controller class after pressing submit. The controller calls the service layer which in turn calls DAO and finally adds the records to the data base. The issue I am having is that in the Controller class, I can receive the request parameters (POST) but the thing is that there is an issue while calling the addMember() of the service layer. The issue is with the Null Pointer Exception on object. Could you guys review the code and provide me suggestions?
here is my controller class LoginController
package com.online.site.member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.online.site.Member.Member;
import com.online.site.Member.MemberBoImpl;
@Controller
@RequestMapping("/Login.htm")
public class LoginController {
@Autowired
//@Qualifier(value="memberServiceImp")
private MemberBoImpl memberServiceImpl;
public void setMemberServiceImp(MemberBoImpl memberServiceImpl) {
this.memberServiceImpl = memberServiceImpl;
System.out.println(memberServiceImpl.toString());
}
@RequestMapping(method=RequestMethod.POST)
public String executeLogin(@ModelAttribute("Login") Member member, BindingResult result)
{
/* call member record fetch method from service layer.
* if login successful redirect to home page. If not a member
* call create member method from service layer and redirect to create account page*/
memberServiceImpl.addMember(member);
//memberServiceImp.getMemberDetails(member.getMemberId());
return "CreateMemberProfile";
}
@RequestMapping(method=RequestMethod.GET)
public String initializeMemberLoginForm(ModelMap model)
{
Member member = new Member();
model.addAttribute("Login", member);
return "Login";
}
}
Here is the service class
@Service
public class MemberBoImpl implements MemberInterface{
//@Autowired
private MemberDaoImpl memberDaoImpl;
/*private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;}*/
public void setMemberDaoImpl(MemberDaoImpl memberDaoImpl) {
this.memberDaoImpl = memberDaoImpl;
System.out.println("in doa setter");
}
public boolean addMember(Member member) {
//hibernateTemplate.save(member);
System.out.println("in bo impl"+member.getMemberId());
System.out.println(memberDaoImpl.toString());
memberDaoImpl.addMember(member);
return false;
}
}
Here is DAO class
package com.online.site.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class MemberDaoImpl implements MemberInterface{
@Autowired
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
System.out.println("in hibernate setter");}
public boolean addMember(Member member) {
System.out.println("dao impl");
hibernateTemplate.save(member);
return false;
}
}
here is config.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd">
<context:annotation-config/>
<!-- <context:component-scan base-package="com.online.site"></context:component-scan> -->
<bean id="memberServiceImpl" class="com.online.site.Member.MemberBoImpl"
scope="singleton" autowire="byType">
<property name="memberDaoImpl" ref="memberDaoImpl"></property>
<!-- <property name="hibernateTemplate" ref="hibernateTemplate"></property>-->
</bean>
<bean id="member" class="com.online.site.Member.Member"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/onlineshopssite"></property>
<property name="username" value="root"></property>
<property name="password" value="admin"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.online.site.Member.Member</value>
</list>
</property>
</bean>
<bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="memberDaoImpl" class="com.online.site.Member.MemberDaoImpl" autowire="byType">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
</beans>
Here is dispatcher-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
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.xsd">
<import resource="classpath*:/onlineShop-config.xml" />
<!-- step eight: http://localhost:8080/Spring3MVCDemo/customer.htm -->
<context:component-scan base-package="com.online.site,com.online.site.Billing,com.online.site.Member"></context:component-scan>
<!-- step 12 public String initializeCustomerForm(ModelMap model) { returning
string value return "createCustomer" createCustomer prefix and suffix will
be added by the InternalResourceViewResolver http://localhost:8080/Spring3MVCDemo/createCustomer.jsp -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
I've tried many suggestions on various forums but nothing seems to work.I'm very likely missing some basci step. Can anyone please take a look at my code and suggest?