Skip to main content

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>

Comments

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...