Use Spring 3.0 into your Servlet based application (No MVC framewrok plugin only Servlet/Jsp application)
Note : Spring3.0 is also having support to use annotation based configuration unlike configuration file based support in earlier version of Spring
Step 1: Create a Service Class Calculator
public class Calculator {
private String sum;
public Double add(String one , String two){
sum = parseDouble(one) + parseDouble(two);
}
public String getSum(){
return sum;
}
}
Step 2: Create a Class MySpringConfig to use annotation based configuration
package com.spring.web.config;
import org.springframework.context.annotation.*;
@Configuration
public class MySpringConfig {
@Bean()
@Scope(value="request")
public Calculator calculator() { return new Calculator(); }
}
Step 3: Use following contextListeners and Custom Configurations class into your web.xml (deployment descriptor)
as below :
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.spring.web.config.MySpringConfig</param-value>
</context-param>
Step 4: Use following code to load calculator instance form the Spring configuration into your JSP scriplet or in your servlet
ApplicationContext beanFactory =
WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext());
Calculator calc =
(Calculator)beanFactory
.getBean("calculator", Calculator.class);
String clickedButton = request.getParameter("command");
String num1= request.getParameter("num1");
String num2= request.getParameter("num2");
if (clickedButton != null && clickedButton.equals("add")) { calc.add(calc.add()); }
Step 5 : HTML code
<html>
<body>
<form action="index.jsp">
<input type="text" name="num1">
<input type="text" name="num2">
<input type="submit" name="command" value="ClickToCount!">
</form>
Sum : <%=calc.getSum()%>
</body>
</html>
Now Your app is ready with Spring 3.0 and simple JSP Servlet web application
Cheers
Kapil
This blog is dedicated to share my experience during my development as a purpose of notes and explorer various web / enterprise technologies like JAVA , JEE , Spring ,hybris, Portal , Jquery , RAI , JMS, Weblogic , SSL , Security, CS, MAC< Linux, Windows, Search, IOT, Arduino, Machine Learning, Tips, Angular, Node JS, React, Mac, Windows, Stack, Exception, Error etc. with examples.
Search This Blog
Subscribe to:
Post Comments (Atom)
Popular Posts
-
While making a HTTP(s) connection to external resource from weblogic server following exception comes because underline API uses weblogic im...
-
We can use following JQuery code to allow only number in input box : $('#inputId').bind('keypress', function(e) { return ...
-
Just walking through with some tips to create MEAN project in few steps and minutes... Step1 : go to mean.io and follow the followi...
-
All caches have a set of rules that they use to determine when to serve an object from the cache, if it's available. Some of these rules...
Above steps are very effective to learn spring 3.0 into your servlet based app. After 2 hours searching i get these important steps from this page. event app iphone
ReplyDelete