Friday 21 October 2016

Spring MVC Using Login Page

Step 1:   Create Maven Project.

              Create View folder under WEB-INF and then create jsp folders here.

Step 2: Add dependencies in pom.xml
            Spring-core
            Spring-context
            Spring-web
            Spring-webmvc

Step 3: Create Controller class(Homecontroller.java)

registration.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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>  ${msg}  </title>
</head>
<body>
<form:form action="submit" method="post">
Name <input type="text" name="name">
Password<input type="password" name="password">
Mobile No<input type="text" name="mobile">
City<input type="text" name="city">
<input type="submit" value="Submit">
</form:form>
<h1> ${msg1} </h1>
</body>

</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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>Spring_4</display-name>
  <servlet>
  <servlet-name>springmvcdispatcher1</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>springmvcdispatcher1</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/springmvcdispatcher1-servlet.xml</param-value>
  </context-param>
  <listener>
  <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

</web-app>


                   Create Spring bean configuration file.the file name must be same as servlet name.here the servlet name is springmvcdispatcher1.so the the bean configuration file name is 
springmvcdispatcher1-servlet.xml.


springmvcdispatcher1-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.spring.register"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>


Homecontroller.java


package com.spring.register;
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {

@RequestMapping(value="/registration",method=RequestMethod.GET)
public String init(Model model)
{
model.addAttribute("msg","Enter the values");
return "registration";
}
@RequestMapping(value="/submit",method = RequestMethod.POST)
   public String sub(Model model, @ModelAttribute("register") Registration register)
   {
String name=register.getName();
String password=register.getPassword();
String city=register.getCity();
int mobile=register.getMobile();
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
Transaction t=session.beginTransaction();
//register.setId(2);
register.setName(name);
register.setPassword(password);
register.setCity(city);
register.setMobile(mobile);
session.save(register);

//PreparedStatement ps=session.connection().prepareStatement("insert into registration values(null,?,?,?,?)");
 t.commit();
session.close();
model.addAttribute("msg1","successfully registered");
return "registration";
         }
}











                                                                                           

No comments:

Post a Comment