Loading
 
What's Next: View All Tutorial →

How to Create Business and DB Service Method Implementation using Spring MVC @Service annotation

In This article, you can learn how to Create Business and DB Service Method Implementation using Spring MVC @Service annotation Refer Spring MVC Hello World for base project setup.     1. Create a new Interface Service UserS...

Published Date: 14/02/2015  Last Modified Date: 14/02/2015    New Post



In This article, you can learn how to Create Business and DB Service Method Implementation using Spring MVC @Service annotation

Refer Spring MVC Hello World for base project setup.
 
 

1. Create a new Interface Service UserService.java and the Class Implementation UserServiceImpl.java


Create a package com.tutorial.service and interface UserService.java
 
package com.tutorial.service;

import com.tutorial.domain.User;

public interface UserService {

    public User getUser();
}
inteface file - This file has only method implementation

Create a package com.tutorial.service and class UserServiceImpl.java
 
package com.tutorial.serviceImpl;

import com.tutorial.domain.User;
import com.tutorial.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Override
    public User getUser() {
        User user = new User();
        user.setAgeGroup("24");
        user.setGender("F");
        user.setUserName("Usha");
        return user;
    }

}
implements UserService  - This implements the UserService Interface

@Service   - This create a instance for the UserService class. The same instance can be used in any other class using @Autowired (Dependency Injection)
 

2. Bind the UserService instance to the Controller class


Create a package com.tutorail.web and the userController.java file
 
package com.tutorial.web;

import com.tutorial.domain.User;
import com.tutorial.service.UserService;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @Autowired
    public UserService userService;

    @RequestMapping("/getUser")
    public String getUser(ModelMap map, HttpServletRequest request) {

        map.addAttribute("user", userService.getUser());
        return "userdetail";
    }

}

@Contoller - to declare as controller file

@Autowired - to bind the instance to the UserService class
 

3. User component-scan in the dispatcher-servlet.xml to scan all the annotated class 

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"

       xmlns:util="http://www.springframework.org/schema/util"       
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
		
    <mvc:annotation-driven  />
    <context:component-scan base-package="com.tutorial" />    
  
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
  
</beans>

<context:component-scan base-package="com.tutorial"/>  - will scan all the classes with the annotation and create a single instance for this
 

4. Create userDetail.jsp to get the response content that controller method return


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h3>Access using ModalMap attribute</h3>
        Name: ${user.userName}<br/>
        Gender: ${user.gender}<br/>
        Age Group : ${user.ageGroup}<br/>
    </body>
</html>
 

Output:


Hit the Url 

http://localhost:8084/SpringMVC/getUser

Response:

Access using ModalMap attribute

Name: Usha
Gender: F
Age Group : 24
 

Download the project


https://drive.google.com/file/d/0B_9jt9kIlxciUU1Ib296S212QWc/view?usp=sharing

 
Awaiting for Administrator approval





Tags: Spring MVC

← Back to list


Related Post




×