Spring Annotation Based Configuration With insert Module
Add below dependency in pom.xml file:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.17.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
</dependencies>
AppConfig.class
package com.deepsingh44.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.
EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.deepsingh44.model.User;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.deepsingh44" })
@EnableTransactionManagement
public class AppConfig {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
return new InternalResourceViewResolver
("/WEB-INF/views/", ".jsp");
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource =
new DriverManagerDataSource();
dataSource.
setDriverClassName("com.mysql.jdbc.Driver");
dataSource.
setUrl("jdbc:mysql://localhost:3306/rahishdb");
dataSource.setUsername("root");
dataSource.setPassword("root");
System.out.println("Deep database " + dataSource);
return dataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new
LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
// sessionFactory.
//setPackagesToScan(new String[] {"com.example.model"});
sessionFactory.setAnnotatedClasses(User.class);
sessionFactory.
setHibernateProperties(hibernateProperties());
System.out.println("deep session " + sessionFactory);
return sessionFactory;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "update");
System.out.println(properties);
return properties;
}
@Bean
public HibernateTransactionManager getTransactionManager() {
System.out.println("transaction " + "hello");
HibernateTransactionManager transactionManager =
new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().
getObject());
System.out.println("transaction " + transactionManager);
return transactionManager;
}
}
Reception.class
package com.deepsingh44.controller;
import org.springframework.web.
servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.deepsingh44.config.AppConfig;
//We just create Reception class as a FrontController
public class Reception
extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class[] {AppConfig.class};
}
@Override
protected Class[] getServletConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] {"/"};
}
}
PageController.java
package com.deepsingh44.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class PageController {
@GetMapping("/")
public String mainPage() {
return "index";
}
@GetMapping("/register")
public String register() {
return "register";
}
}
LoginController.java
package com.deepsingh44.controlle
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMappi
import com.deepsingh44.dao.UserDao;
import com.deepsingh44.model.User;
@Controller
public class LoginController {
@Autowired
private UserDao userDao;
@PostMapping("/signup")
public String register(User user) {
int i=userDao.insert(user);
return "index";
}
}
UserDao.java
package com.deepsingh44.dao;
import com.deepsingh44.model.User;
public interface UserDao {
int insert(User user);
}
UserDaoImpl.java
package com.deepsingh44.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.deepsingh44.model.User;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionfactory;
@Transactional
@Override
public int insert(User user) {
int i = 0;
sessionfactory.getCurrentSession().save(user);
i = 1;
return i;
}
}
User.java
package com.deepsingh44.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.constraints.Length;
@Entity
@Table(name = "users")
public class User {
@Length(max = 35)
private String name;
@Id
private String email;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
head.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<head>
<title>ABC Project</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js">
</script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js">
</script>
</head>
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
<a class="navbar-brand" href="#">MyApp</a>
<button class="navbar-toggler"
type="button" data-toggle="collapse"
data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse"
id="collapsibleNavbar">
<ul class="navbar-nav">
<c:choose>
<c:when test="${sessionScope.user !=null}">
<li class="nav-item">
<a class="nav-link" href="#">Profile</a></li>
<li class="nav-item">
<a class="nav-link" href="#">Logout</a></li>
</c:when>
<c:otherwise>
<li class="nav-item"><a class="nav-link" href="register">Register</a></li>
<li class="nav-item"><a class="nav-link" href="#">Login</a></li>
</c:otherwise>
</c:choose>
</ul>
</div>
</nav>
index.jsp
<%@include file="head.jsp"%>
register.jsp
<%@include file="head.jsp"%>
<div class="container">
<form action="signup" method="post">
<div class="form-group">
<label for="name">Name:</label> <input type="text"
class="form-control" placeholder="Enter name" name="name">
</div>
<div class="form-group">
<label for="pwd">Password:</label> <input type="password"
class="form-control" placeholder="Enter password" name="pass">
</div>
<div class="form-group">
<label for="email">Email address:</label> <input type="email"
class="form-control" placeholder="Enter email" name="email">
</div>
<div align="center">
<button type="submit" class="btn btn-primary"
style="width: 60%;">Submit</button></div>
</form>
</div>
No comments:
Post a Comment