Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Create Addition Program using Spring MVC

Step1st:-  Create Dynamic Web Project using eclipse
step2nd:-  Add Jar file under lib folder web-inf/lib
step3rd:-   Create Dispatcher Servlet it should be filename-servlet.xml

complete code of dispatcher servlet


<?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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="bao"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/uao/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
    <bean id="multipartResolver"  
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> 
    </beans>



step4th:-  Add disptacher servlet under web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>SpringHelloWorldNew</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
   <servlet>
    <servlet-name>scs</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>scs</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>


step5th:-

Create Controller to load view:-

under src folder under bao package

package bao;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Hello {
       @RequestMapping("/add")
    public String add()
    {
        return "addition";
    }
  }


step6:-  Create View to design addition using add.jsp page under uao folder under  webcontent


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="addlogic.html" method="post">
<input type="text" name="txtnum1" placeholder="enter first number" />
<br>
<br>

<input type="text" name="txtnum2" placeholder="enter second number" />

<br>
<br>
<input type="submit" name="btnsubmit" value="Add" />
</form>


</body>
</html>


step7:-  create action method under controller then controller will be this type:-

package bao;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Hello {
    @RequestMapping("/abc")
    public String fun()
    {
        return "hello";
    }
    @RequestMapping("/add")
    public String add()
    {
        return "addition";
    }
    @RequestMapping("/addlogic")
    public ModelAndView addlogic(HttpServletRequest request,HttpServletResponse response)
    {
        int a = Integer.parseInt(request.getParameter("txtnum1"));
        int b = Integer.parseInt(request.getParameter("txtnum2"));
        int c = a+b;
        return new ModelAndView("addition","res","result is "+c);
    }
}


Step 9th :-

Write output code under JSP Page  add.jsp

${res}


step8:-

Call add.html under index.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<a href="add.html">Addition</a>
</body>
</html>


Run this code 




Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post