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

 
 
 
 
0 تعليقات
POST Answer of Questions and ASK to Doubt