Spring MVC using Multiple ActionController
-----------------------------------




--------------------------------------------------------------------
Create EmpCrudController controller class
-------------------------------------------------------------------
package controller;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class EmpCrudController extends MultiActionController {

 //empsave 
 public ModelAndView empsave(HttpServletRequest req,HttpServletResponse res) throws Exception
 {
  String name=req.getParameter("name");
  String email=req.getParameter("email");
  String address=req.getParameter("address");
 
  System.out.println("---------------Database connectipn --------");
  Class.forName("oracle.jdbc.OracleDriver");
  System.out.println("---------------Database connectipn registered--------");
  Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","chandra","oracle");
  System.out.println("---------------Database connectipn made--------");
  ResultSet rs=con.createStatement().executeQuery("select max(id) from emp ");
  int maxid=0;
  if(rs.next())
  {
   maxid=rs.getInt(1);
   
  }
  maxid++;
  PreparedStatement ps=con.prepareStatement("insert into  emp values(?,?,?,?)");
  ps.setInt(1, maxid);
  ps.setString(2, name);
  ps.setString(3, email);
  ps.setString(4, address);
  int i=ps.executeUpdate();
  ModelAndView mav=null;
  if(i!=0)
   mav=new ModelAndView("success");
  else
   mav=new ModelAndView("fail") ;
  // mav=new ModelAndView("success");
  return mav;
  
 }
 //empUpdate 
  public ModelAndView empupdate(HttpServletRequest req,HttpServletResponse res) throws Exception
  {
   int id=Integer.parseInt(req.getParameter("id"));
   
   String name=req.getParameter("name");
   String email=req.getParameter("email");
   String address=req.getParameter("address");
   

   //Class.forName("oracle.jdbc.oracleDriver");
   Class.forName("oracle.jdbc.OracleDriver");
   
   Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","chandra","oracle");
   
   PreparedStatement ps=con.prepareStatement("update emp set name=?,email=?,address=? where id=?");
   ps.setInt(4, id);
   ps.setString(1, name);
   ps.setString(2, email);
   ps.setString(3, address);
   int i=ps.executeUpdate();
   ModelAndView mav=null;
   if(i!=0)
    mav=new ModelAndView("success");
   else
    mav=new ModelAndView("fail") ;
   return mav;
   
  }
 
}


-----------------------------------------------------------
create MAC-servlet.xml file inside /WEB-INF folder
---------------------------------------------------------------


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/empsave.ds">Empcrud</prop>
<prop key="/empupdate.ds">Empcrud</prop>

</props>
</property>
</bean>
<bean name="Empcrud" class="controller.EmpCrudController"/>
<!--  View resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"/>
</bean>

</beans>

-----------------------------------------------------
Modify web.xml file
-----------------------------------------------------
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Spring_MVC_CRUD_multiple_action_controller</display-name>
  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>
  
  <servlet>
  <servlet-name>MAC</servlet-name>
  
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>MAC</servlet-name>
<url-pattern>*.ds</url-pattern>
</servlet-mapping>
</web-app>
-------------------------------------------------------------------
Create empsave.jsp file
-------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<h1>Employe Save Form</h1>
<form action="./empsave.ds">
<pre>
Name: <input type="text" name="name" />
Email: <input type="text" name="email" />
Address: <input rows="5" cols="15" name="address" />
<input type="submit" value="submit">
</pre>
</form>
</body>
</html>
-------------------------------------------------------------------
empsave.jsp
---------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<h1>Employe Update Form</h1>
<form action="./empupdate.ds">
<pre>
ID <input type="text" name="id"/>
Name: <input type="text" name="name"/>
Email: <input type="text" name="email"/>
Address: <input rows="5" cols="15" name="address"/>
<input type="submit" value="update">
</pre>
</form>
</body>
</html>
--------------------------------------------------------------------------
Create success.jsp file
------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<h1> Success JSP file</h1>
</body>
</html>
--------------------------------------------------------------------------
Create fail.jsp file
------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<h1>Failed to login</h1>
</body>
</html>
-----------------------------------------------------------------------------
Modify on POM.xml file
-----------------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Spring_MVC_CRUD_multiple_action_controller</groupId>
  <artifactId>Spring_MVC_CRUD_multiple_action_controller</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
</dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
--------------------------------------

Comments

Popular posts from this blog