Spring MVC step by step with Model Introduction-Part2

Model is the representative of the real world entity.Entity represents the property of an object.We can relate an Entity with database tables. We can use this entity with different layers like service,dao and GUI layer to represent a entity to the user.

To continue with this tutorial I suggest please visit my previous post Spring MVC step by step with maven-Part1

Now we will practice with an Entity User and we create a package named by model and then create a User.java class in this package. and then we will use this entity in controller and view class.

*******************************User.java***********************************

package com.flopcoder.model;

/**
 * Created by Flop Coder on 6/1/14.
 */
public class User {
    private Integer id;
    private String name;
    private String address;
    private String mobile;
    private String email;
    private String userName;
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

****************************************End**********************************

Then we replace the  WelcomeController.java class by following code.Here we instantiate a simple pojo class in this controller and set a sample data to this pojo object.

********************************WelcomeController.java*************************
package com.flopcoder.controller;

import com.flopcoder.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class WelcomeController {
@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
        User user = new User();
        user.setId(1);
        user.setName("FlopCoder");
        user.setAddress("Bangladesh");
        user.setEmail("flopcoder@gmail.com");
        user.setMobile("01684398208");
        user.setPassword("secret");
        user.setUserName("flopcoder");
        model.addAttribute("user",user);
model.addAttribute("message", "Welcome to learn spring Step By Step for modeling introduction. This is part 2.");
return "hello";
}
}

****************************************End**********************************

And then we need to show this user data to user.We need to change the hello.jsp class 


**************************************hello.jsp********************************

<html>
<body>
    <h1>${message}</h1>
    <h1>Name : ${user.name}</h1>
    <h1>E-mail : ${user.email}</h1>
    <h1>User Name : ${user.userName}</h1>
    <h1>Mobile : ${user.mobile}</h1>
    <h1>Address : ${user.address}</h1>
</body>
</html>

**********************************End****************************************

Now you can deploy it by using web container or any web server.I'll provide my next post on introduction of service and dao layer
Reactions

Post a Comment

0 Comments