We have yet not created all Master Screens. However, i will give a sneek peek with a petite' example of how to display transaction detail of a registered user.
Let's say you have 2 tables:
1. Table "users" - to store user master data
2. Table "user_transaction" - to store transaction data
users sql:
CREATE TABLE users
(
user_id serial NOT NULL,
password character varying(10) NOT NULL,
email_id character varying(30) NOT NULL,
create_date timestamp without time zone,
CONSTRAINT users_pkey PRIMARY KEY (user_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE users
OWNER TO postgres;
user_transaction sql:
CREATE TABLE user_transaction
(
transaction_id integer NOT NULL,
user_id integer,
CONSTRAINT user_transaction_pkey PRIMARY KEY (transaction_id),
CONSTRAINT user_transaction_user_id_fkey FOREIGN KEY (user_id)
REFERENCES users (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE user_transaction
OWNER TO postgres;
How the relation is:
Project Structure.. JAR file are same as in http://lillbitofeverythingagain.blogspot.in/2015/07/simple-spring-login.html
---------------------------------------------------------------------------------
Let's say you have 2 tables:
1. Table "users" - to store user master data
2. Table "user_transaction" - to store transaction data
users sql:
CREATE TABLE users
(
user_id serial NOT NULL,
password character varying(10) NOT NULL,
email_id character varying(30) NOT NULL,
create_date timestamp without time zone,
CONSTRAINT users_pkey PRIMARY KEY (user_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE users
OWNER TO postgres;
user_transaction sql:
CREATE TABLE user_transaction
(
transaction_id integer NOT NULL,
user_id integer,
CONSTRAINT user_transaction_pkey PRIMARY KEY (transaction_id),
CONSTRAINT user_transaction_user_id_fkey FOREIGN KEY (user_id)
REFERENCES users (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE user_transaction
OWNER TO postgres;
How the relation is:
Project Structure.. JAR file are same as in http://lillbitofeverythingagain.blogspot.in/2015/07/simple-spring-login.html
---------------------------------------------------------------------------------
web.xml
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<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>login.html</welcome-file>
</welcome-file-list>
</web-app>
login.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="login2.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="Log-in"/>
</form:form>
</body>
</html>
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>
UserController.java
--------------------------------------------
package com.users.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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 java.util.List;
import com.users.bean.TransactionDetails;
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("1");
userService.addUser(u);
// mail m = new mail(u1.getEmail_id());
System.out.println("2");
return new ModelAndView("save");
}
@RequestMapping(value = "/login2", method = RequestMethod.POST)
public ModelAndView login( @ModelAttribute("user") UserBean u1, BindingResult result) {
System.out.println("In controller");
Map<String, Object> model = new HashMap<String, Object>();
try
{
System.out.println("a");
User u = userService.validateUser(u1.getEmail_id(), u1.getPassword());
if(u.getEmail_id().equalsIgnoreCase("null"))
return new ModelAndView("notsave");
}
catch(Exception e)
{
}
System.out.println("trans1");
List t2 = userService.listTransaction(u1.getEmail_id());
System.out.println("XoXo");
Iterator it=t2.iterator();
List<TransactionDetails> lst = new ArrayList<TransactionDetails>();
while(it.hasNext())
{
TransactionDetails d1 = new TransactionDetails();
Object rows[] = (Object[])it.next();
System.out.println("kaula");
String s = rows[0].toString();
String t = rows[1].toString();
d1.setTransaction_id(s);
d1.setUser_id(t);
System.out.println("umpur");
lst.add(d1);
System.out.println(rows[0]+ " -- " +rows[1]);
}
Iterator io=lst.iterator();
while(io.hasNext())
{
System.out.println(io.next());
}
model.put("t1", lst);
System.out.println("inbetween");
return new ModelAndView("save", model);
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView welcome(@ModelAttribute("user") UserBean u1, BindingResult result) {
return new ModelAndView("login");
}
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;
}
}
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;
}
}
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;
}
}
TransactionDetails.java
---------------------------------------------------
package com.users.bean;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="user_transaction")
public class TransactionDetails implements Serializable{
String user_id, transaction_id;
@Column(name = "user_id")
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
@Column(name = "transaction_id")
public String getTransaction_id() {
return transaction_id;
}
public void setTransaction_id(String transaction_id) {
this.transaction_id = transaction_id;
}
}
UserService.java
-----------------------------------------
package com.users.service;
import java.util.List;
import com.users.bean.TransactionDetails;
import com.users.model.User;
public interface UserService {
public void addUser(User user);
public User validateUser(String uid, String password);
public List<TransactionDetails> listTransaction(String email_id);
}
UserServiceImpl.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.bean.TransactionDetails;
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) {
System.out.println("3");
uDao.addUser(user);
}
@Override
public User validateUser(String uid, String password) {
System.out.println("b");
return uDao.validateUser(uid, password);
}
public List<TransactionDetails> listTransaction(String email_id)
{
System.out.println("trans2");
List<TransactionDetails> l1 = uDao.listTransaction(email_id);
System.out.println("size"+l1.size());
return l1;
}
}
UserDao.java
-----------------------------------
package com.users.dao;
import java.util.List;
import com.users.bean.TransactionDetails;
import com.users.model.User;
import com.users.model.User;
public interface UserDao {
public void addUser(User user);
public User validateUser(String uid, String password);
public List<TransactionDetails> listTransaction(String email_id);
}
UserDaoImpl.java
---------------------------------------
package com.users.dao;
import java.sql.Array;
import java.util.List;
import java.util.Map;
import java.util.Iterator;
import javax.jms.Session;
import javax.management.Query;
import javax.resource.cci.ResultSet;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.users.bean.TransactionDetails;
import com.users.bean.UserBean;
import com.users.model.User;
@Repository("uDao")
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
int k;
public void addUser(User user) {
System.out.println("4");
sessionFactory.getCurrentSession().save(user);
}
@SuppressWarnings("finally")
public User validateUser(String email_id, String password)
{
System.out.println("cccc");
User l1 = new User();
try
{
System.out.println("cc1111");
String sql = "SELECT user_id FROM USERS WHERE email_id = :email_id and password = :password";
SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
query.setParameter("password",password);
query.setParameter("email_id",email_id);
int u1 = (Integer) query.uniqueResult();
k = u1;
System.out.println(u1);
System.out.println("d");
}
catch(Exception e)
{
if(k==0)
{
l1.setEmail_id("null");
l1.setPassword("null");
}
}
finally
{
System.out.println(l1.getEmail_id());
return l1;
}
}
@SuppressWarnings("unchecked")
public List<TransactionDetails> listTransaction(String email_id) {
System.out.println("trans3");
String sql = "SELECT a.user_id, a.transaction_id FROM user_transaction a inner join users b on a.user_id = b.user_id AND b.email_id = :email_id";
SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
query.setParameter("email_id",email_id);
TransactionDetails toto = new TransactionDetails();
System.out.println("hoho");
List<TransactionDetails> l = query.list();
Iterator it=l.iterator();
List<TransactionDetails> l2 = null;
while(it.hasNext())
{
Object rows[] = (Object[])it.next();
System.out.println(rows[0]+ " -- " +rows[1]);
}
return l;
}
}
notsave.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 style="color:red">Incorrect Credentials, please try again</h2>
<form:form method="POST" action="login2.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="Log-in"/>
</form:form>
</body>
</html>
save.jsp
-----------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h2>Add Employee Data</h2>
<form:form method="POST" action="save.html">
<table>
<tr>
<td><form:label path="id">Transaction ID:</form:label></td>
<td><form:input path="id" value="${i.transaction_id}" readonly="true"/></td>
</tr>
<tr>
<td><form:label path="name">User ID:</form:label></td>
<td><form:input path="name" value="${i.user_id}"/></td>
</tr>
</table>
<input type="submit" value="Save"/>
</form:form>
<c:if test="${!empty t1}">
<table align="left" border="1">
<tr>
<th>transaction ID</th>
<th>User ID</th>
</tr>
<c:forEach var="i" items="${t1}">
<tr>
<td><c:out value="${i.transaction_id}"/></td>
<td><c:out value="${i.user_id}"/></td>
<td align="center"><a href="edit.html?id=${i.transaction_id}">Edit</a> | <a href="delete.html?id=${i.transaction_id}">Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h2>Add Employee Data</h2>
<form:form method="POST" action="save.html">
<table>
<tr>
<td><form:label path="id">Transaction ID:</form:label></td>
<td><form:input path="id" value="${i.transaction_id}" readonly="true"/></td>
</tr>
<tr>
<td><form:label path="name">User ID:</form:label></td>
<td><form:input path="name" value="${i.user_id}"/></td>
</tr>
</table>
<input type="submit" value="Save"/>
</form:form>
<c:if test="${!empty t1}">
<table align="left" border="1">
<tr>
<th>transaction ID</th>
<th>User ID</th>
</tr>
<c:forEach var="i" items="${t1}">
<tr>
<td><c:out value="${i.transaction_id}"/></td>
<td><c:out value="${i.user_id}"/></td>
<td align="center"><a href="edit.html?id=${i.transaction_id}">Edit</a> | <a href="delete.html?id=${i.transaction_id}">Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
On successful login, transaction data of user: