التخطي إلى المحتوى الرئيسي

Create Simple Interest Program using Bean class in Spring MVC



Step1st:-  Create a dynamic web project and add Dispatcher Servlet under web-inf/scs-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: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>

step2nd:-  append code 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>

STEP 3rd:-
Create bean class using scsbean package under src:-
package scsbean;

public class SI {
private float p;
private float r;
private float t;
public float getP() {
    return p;
}
public void setP(float p) {
    this.p = p;
}
public float getR() {
    return r;
}
public void setR(float r) {
    this.r = r;
}
public float getT() {
    return t;
}
public void setT(float t) {
    this.t = t;
}



}
STEP 4thCreate Controller under bao package

package bao;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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 scsbean.SI;

@Controller
public class SIController {
    @RequestMapping(value = "/si", method = RequestMethod.GET)
      public ModelAndView siload() {
        return new ModelAndView("siform", "command", new SI());
       }
    @RequestMapping(value = "/silogic", method = RequestMethod.POST)
      public ModelAndView silogic(@ModelAttribute("SpringWeb")SI s, ModelMap model) {
         
         float s1 = (s.getP()*s.getR()*s.getT())/100;
         return new ModelAndView("siresult","res",s1);
       }
}
step 5th:-

Create View to load form and action siform.jsp and siresult.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>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form action="silogic.html" method="POST">     
<table><tbody>
<tr>        <td><form:label path="p">P :</form:label></td>      <td><form:input path="p"></form:input></td>    </tr>
<tr>      <td><form:label path="r">R:</form:label></td>       <td><form:input path="r"></form:input></td>    </tr>
<tr>       <td><form:label path="t">T:</form:label></td>       <td><form:input path="t"></form:input></td>     </tr>

<tr>         <td colspan="2"><input type="submit" value="Submit"/> </td>       </tr>
</tbody></table>
</form:form>


</body>
</html>

siresult.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>
${res}
</body>
</html>


Note:-

If we want to display results on the same pages then we will implement two modifications

1)  edit logic method code


@RequestMapping("silogic")
public ModelAndView siLogic(@ModelAttribute("SpringMVCHello")SI s, ModelMap model)
{
float si = (s.getP()*s.getR()*s.getT())/100;
ModelAndView obj =new ModelAndView("siform", "command", new SI());
obj.addObject("res",  "Result is "+si);
return obj ;
}

2)  write code on JSP pages

<%@ 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>
   <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
</head>
<body>
<form:form action="silogic.html" method="POST">

<table>

<tr><td><form:label path="p" /></td><td><form:input path="p" /></td></tr>
<tr><td><form:label path="r" /></td><td><form:input path="r" /></td></tr>
<tr><td><form:label path="t" /></td><td><form:input path="t" /></td></tr>
<tr><td></td><td><input type="submit" value="Submit"/></td></tr>
</table>
</form:form>
<%
if(request.getAttribute("res")!=null)
{
out.print(request.getAttribute("res"));
}


%>
</body>
</html>


Another Example of Spring MVC to Check Prime Number?


1)  Create bean class

package dao;

public class PrimeNum {
private int num;

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}


}


2)  Create Controller to load JSP Page

package bao;

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

import dao.PrimeNum;;

@Controller
public class PrimeController {
@RequestMapping("prime")
public ModelAndView pload()
{
return new ModelAndView("prime", "command", new PrimeNum());
}
}


3)  Create JSP Page and bind bean Object

<%@ 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>
 <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
</head>
<body>
<form:form action="primelogic.html" method="post">
<form:label path="num"></form:label>
<form:input path="num"/>
<br>
<input type="submit" name="btnsubmit" value="Click" />

</form:form>


</body>
</html>


4)  Create Logic Method under the controller

@RequestMapping("primelogic")
public ModelAndView primeLogic(@ModelAttribute("SpringMVCHello")PrimeNum s, ModelMap model)
{
String result="";
int i;
for(i=2;i<s.getNum();i++)
{
if(s.getNum()%i==0)
{
result = "Not prime";
break;
}
}
if(s.getNum()==i)
{
result = "prime"; 
}
ModelAndView obj =new ModelAndView("prime", "command", new PrimeNum());
obj.addObject("res",  "Result is "+result);
return obj ;
}


5)  Write output code under JSP Page

<%
if(request.getAttribute("res")!=null)
{
out.print(request.getAttribute("res"));
}


%>

6)  Call PrimeController from index.jsp using Hyperlink

    <a href="prime.html">Prime</a>



FIBONACCI SERIES?

package dao;

public class Fab {
private int num;

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}


}



    package bao;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import dao.Fab;

@Controller
public class FabController {

@RequestMapping("fab")
public ModelAndView pload()
{
return new ModelAndView("fab", "command", new Fab());
}
@RequestMapping("fablogic")
public ModelAndView fabLogic(@ModelAttribute("SpringMVCHello")Fab s, ModelMap model)
{
String result="";
int i,a=-1,b=1,c=0;
for(i=1;i<s.getNum();i++)
{
c=a+b;
result = result +c + " ";
a=b;
b=c;
}
ModelAndView obj =new ModelAndView("fab", "command", new Fab());
obj.addObject("res",  "Result is "+result);
return obj ;
  
}
}


<%@ 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>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
</head>
<body>
<form:form action="fablogic.html" method="post">
<form:label path="num"></form:label>
<form:input path="num"/>
<br>
<input type="submit" name="btnsubmit" value="Click" />

</form:form>

<%
if(request.getAttribute("res")!=null)
{
out.print(request.getAttribute("res"));
}


%>
</body>
</html>

تعليقات

المشاركات الشائعة من هذه المدونة

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025

 Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025 Now a days most of the IT Company asked NODE JS Question mostly in interview. I am creating this article to provide help to all MERN Stack developer , who is in doubt that which type of question can be asked in MERN Stack  then they can learn from this article. I am Shiva Gautam,  I have 15 Years of experience in Multiple IT Technology, I am Founder of Shiva Concept Solution Best Programming Institute with 100% Job placement guarantee. for more information visit  Shiva Concept Solution 1. What is the MERN Stack? Answer : MERN Stack is a full-stack JavaScript framework using MongoDB (database), Express.js (backend framework), React (frontend library), and Node.js (server runtime). It’s popular for building fast, scalable web apps with one language—JavaScript. 2. What is MongoDB, and why use it in MERN? Answer : MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It...