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 a 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) {
         model.addAttribute("p", s.getP());
         model.addAttribute("r", s.getR());
         model.addAttribute("t", s.getT());
         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>


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