Friday, 25 September 2015

Module 1..User Master Data Save application

In this blog, i will be creating user interface to save new user master data in PMS-Users table.

Our end screen should look something like this and should sav data in table post submit:


We are developing this application using Spring 3.2 and are using hibernate for database connection.

Project Structure:


JAR files are same as given in the LIBRARY folder image this link:
http://lillbitofeverythingagain.blogspot.in/2015/07/simple-spring-login.html

web.xml
------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>sdnext</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/config/sdnext-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>sdnext</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>welcome.html</welcome-file>
  </welcome-file-list>
</web-app>

Welcome.jsp
------------------------------

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<body>

<h2>New User Master Data</h2>

<form:form method="POST" action="save.html" modelAttribute="user" id="form">

<table>
<tr><td>  <form:input path="user_id" placeholder="User ID:" value="" disabled="true"/></td></tr>

    <tr><td><form:input path="email_id" placeholder="Email ID:" value="" /></td></tr>
<tr> <td> <form:password path="password" placeholder="Password" value="" /> </td></tr>


</table>
<input type="submit" value="Save"/>

</form:form>

</body>


</html>

Controller class
--------------------------------------

package com.users.controller;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
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 org.springframework.web.servlet.ModelAndView;

import com.users.bean.UserBean;

import com.users.model.User;
import com.users.service.UserService;


@Controller
public class UserController {

@Autowired
private UserService userService;


@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveUser( @ModelAttribute("user") UserBean u1, BindingResult result) {
User u = prepareModel(u1);
System.out.println("+++++++++++++"+u);
userService.addUser(u);
return new ModelAndView("save");
}



@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public ModelAndView welcome(@ModelAttribute("user") UserBean u1, BindingResult result) {

System.out.println("1*****************");
return new ModelAndView("welcome");
}





private User prepareModel(UserBean uBean){
User u1 = new User();
u1.setPassword(uBean.getPassword());
u1.setEmail_id(uBean.getEmail_id());
u1.setUser_id(uBean.getUser_id());
uBean.setUser_id(null);
return u1;
}


}


UserBean.java
-------------------------------

package com.users.bean;
import java.util.Date;

import com.ibm.icu.util.Calendar;

public class UserBean {
private Integer user_id;
private String email_id;
private String password;
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getEmail_id() {
return email_id;
}
public void setEmail_id(String email_id) {
this.email_id = email_id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}


Model class - User.java
-------------------------------------

package com.users.model;

import java.io.Serializable;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="users")
public class User implements Serializable{

private static final long serialVersionUID = -723583058586873479L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "user_id")
private Integer user_id;

public static long getSerialversionuid() {
return serialVersionUID;
}

@Column(name = "email_id")
private String email_id;
@Column(name="password")
private String password;

public Integer getUser_id() {
return user_id;
}

public void setUser_id(Integer user_id) {
this.user_id = user_id;
}

public String getEmail_id() {
return email_id;
}

public void setEmail_id(String email_id) {
this.email_id = email_id;
}

public String getPassword() {
return password;
}

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





}

UserService.java
------------------------------------
package com.users.service;

import java.util.List;

import com.users.model.User;


public interface UserService {
public void addUser(User user);
}


UserServImpl.java (Implementation of UserService.java)
---------------------------------------------------------------------------------

package com.users.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.users.dao.UserDao;
import com.users.model.User;

@Service("userService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class UserServiceImpl implements UserService {

@Autowired
private UserDao uDao;
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addUser(User user) {
uDao.addUser(user);
}

}


userDao.java
-------------------------------------

package com.users.dao;

import java.util.List;

import com.users.model.User;

public interface UserDao {
public void addUser(User user);
}


userDaoImpl (implementing userDao interface)
------------------------------------------------------------------------

package com.users.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.users.bean.UserBean;
import com.users.model.User;


@Repository("uDao")
public class UserDaoImpl implements UserDao {

@Autowired
private SessionFactory sessionFactory;
public void addUser(User user) {
sessionFactory.getCurrentSession().save(user);
}



}

package com.users.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.users.bean.UserBean;
import com.users.model.User;


@Repository("uDao")
public class UserDaoImpl implements UserDao {

@Autowired
private SessionFactory sessionFactory;
public void addUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
}
config > sdnext-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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">

<context:property-placeholder location="classpath:resources/database.properties" />
<context:component-scan base-package="com.users" />
<mvc:annotation-driven/>

<mvc:resources mapping="/js/**" location="/js" />

 <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>  
  <mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="prefixJson" value="true"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.users.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>

<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="webBindingInitializer">
    <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"/>
  </property>
  <property name="messageConverters">
    <list>
      <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
    </list>
  </property>
</bean>

</beans>

Save,jsp
------------------------

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>  
<body>  
<% out.print("data saved."); %>  
</body>  
</html>

resources- database.properties
----------------------------------------------

database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost:5432/PMS
database.user=postgres
database.password=12345
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create

Now export WAR file to webapps folder in apache folder, deploy and run..

Save the data in the screen and database has the data:


Message post saving:

Data in table post saving:



No comments:

Post a Comment