Spring MVC step by step more advance with form handling Part-6


If you completed my previous post on spring mvc step by step you can proceed with us.Or if you have already this knowledge please visit my post Spring MVC Step By Step with final task for beginners part-5  and download project and continue with us.

In this Post I will show to you about form handling in spring. Here we use jstl for form handling.We will register a user through form.

So you need a jar to use jstl. you can add this dependency into maven.

 pom.xml
*************************************
     
        
            jstl
            jstl
            1.2
        
*************************************


Now we need to create a Controller that will responsible for Handle User.Here we need two methods one is post and one is get.

So we create a controllet with two methods UserController.java

********************************
package com.flopcoder.controller;

import com.flopcoder.dao.UserDao;
import com.flopcoder.model.User;
import com.flopcoder.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Method;

/**
 * Created by Flop Coder on 6/15/14.
 */
@Controller
@RequestMapping("/")
public class UserController {

    @Autowired
    UserService userService;
    @RequestMapping(value = "/registration",method = RequestMethod.GET)
    public ModelAndView register(User user)
    {
        ModelAndView ret = new ModelAndView();
        ret.setViewName("register");
        ret.addObject("user",user);
        return ret;
    }

    @RequestMapping(value = "/registration",method = RequestMethod.POST)
    public ModelAndView registerProcess(@ModelAttribute("user") User user)
    {
        ModelAndView ret = new ModelAndView();
        userService.save(user);
        ret.setViewName("register");
        ret.addObject("user",user);
        return ret;
    }
}

****************************************


Now we need to create a JSP page to create a user

***********************************************
<%--
  Created by IntelliJ IDEA.
  User: Flop Coder
  Date: 6/15/14
  Time: 11:24 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>



    




    
    
    





SUBMIT
****************************************
Now we need to remove setting user statically from UserService.java class

Update UserService.java

**********************************************
package com.flopcoder.service;

import com.flopcoder.dao.IUserDao;
import com.flopcoder.dao.UserDao;
import com.flopcoder.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Flop Coder on 6/1/14.
 */

@Service
public class UserService implements IUserService{

    @Autowired
    IUserDao userDao;
    @Override
    public User getUserById(Integer id)
    {
        return userDao.getUserById(id);
    }

    public User save(User user)
    {
        return userDao.save(user);
    }
}

********************************************
Reactions

Post a Comment

0 Comments