Here I am explaining Simple Add 2 Numbers Application In Springs
TestController.java
package com.springs;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
Numberbean num= new Numberbean();
int n1=Integer.parseInt(request.getParameter("n1"));
int n2=Integer.parseInt(request.getParameter("n2"));
num.setN1(n1);
num.setN2(n2);
int add1 = num.addition();
Map map=new HashMap();
map.put("num" , Integer.toString(add1));
return new ModelAndView("display.jsp", "num", map.get("num"));
}
}
Numberbean.java
package com.springs;
public class Numberbean {
int n1=0;
int n2=0;
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
public int addition()
{
return this.n1+this.n2;
}
}
Test-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="TestController" class="com.springs.TestController"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/display.htm">TestController</prop>
</props>
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
display.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<body>
<h3>Addition Of Two Numbers is :: <c:out value="${num}"/></h3>
</body>
</html>
addition.jsp
<html>
<body>
<form method="post" action="display.htm">
<h3><u>Add Form</u></h3><br/>
Number1::<input type="text" name="n1" value=""/><br/><br/>
Number2::<input type="text" name="n2" value=""/><br/><br/>
<input type="submit" value="Submit"/><br/>
</form>
</body>
</html>
No comments:
Post a Comment