Download the Spring Framework 2.0 jars from the link here and unzip the jars and add to the lib folder of the web application.
1) Create a GreetingService.java interface
2) Create a GreetingServiceImpl.java class that implements the GreetingService interface
3) Create a new applicationContext.xml file in the WEB-INF folder of the web application
4) Modify the web.xml file to add the following content,
5) Create a springHelloWorld.jsp file in the WebContent folder
6) Open the following URL in your browser,
1) Create a GreetingService.java interface
package info.icontraining.spring;
public interface GreetingService {
public String sayGreeting();
}
2) Create a GreetingServiceImpl.java class that implements the GreetingService interface
package info.icontraining.spring;
public class GreetingServiceImpl implements GreetingService {
private String greeting;
public GreetingServiceImpl() {}
public GreetingServiceImpl(String greeting) {
this.greeting = greeting;
}
public String sayGreeting() {
return this.greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}
3) Create a new applicationContext.xml file in the WEB-INF folder of the web application
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" >
<bean id="greetingService"
class="info.icontraining.spring.GreetingServiceImpl">
<property name="greeting">
<value>Hello World!</value>
</property>
</bean>
</beans>
4) Modify the web.xml file to add the following content,
<web-app ...>
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
...
</web-app>
5) Create a springHelloWorld.jsp file in the WebContent folder
<%@ page import="org.springframework.context.*,org.springframework.web.context.*,info.icontraining.spring.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ApplicationContext factory =
(ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
GreetingService greetingService = (GreetingService)factory.getBean("greetingService");
%>
<%=greetingService.sayGreeting() %>
</body>
</html>
6) Open the following URL in your browser,
http://localhost:8080/WebAppName/springHelloWorld.jsp
No comments:
Post a Comment