Spring — Web Container | Code Factory
Donate : Link
WordPress Blog : Link
Applications… : Link
Spring Tutorial Index Page: Link
- WebApplicationContext is the child of plain ApplicationContext. It is used in web applications. It provides features to deal with web-related components like- controllers, view resolvers etc.
public interface WebApplicationContext extends ApplicationContext
- A Web Application can have multiple WebApplicationContext to handle requests.
- Each DispatcherServlet is associated with one WebApplicationContext.
Create Web Project
- Open Eclipse
- Go to File -> New -> Dynamic Web Project
- Create IOC-Web project
- Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs (Used 2.X jars)
- commons-logging-X.X.jar
- spring-beans-X.X.X.jar
- spring-context-X.X.X.jar
- spring-core-X.X.X.jar
- spring-web-X.X.X.jar
- spring-webmvc-X.X.X.jar - Also add these jars into WEB-INF/lib folder
- * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<display-name>IOC-Web</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>helloWorld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- listener class -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- context param -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/helloWorld-servlet.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>helloWorld</servlet-name>
<url-pattern>*.code</url-pattern>
</servlet-mapping></web-app>
helloWorld-servlet.java
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans>
<!-- default handler mapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- controller -->
<bean name="/hello.code" class="com.codeFactory.controller.HelloController" /> <!-- view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean> <bean id="t" class="com.codeFactory.beans.Test" scope="prototype" />
</beans>
Test.java
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Test {
private Test() {
System.out.println("Test.Test()");
}
public void print(String msg) {
System.out.println(msg);
}
}
HelloController.java
package com.codeFactory.controller;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;import com.codeFactory.beans.Test;public class HelloController extends AbstractController { @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = request.getParameter("name");
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
/* get bean object */
Test t = (Test) context.getBean("t");
t.print(name);
ModelAndView modelAndView = new ModelAndView("success", "msg", name);
return modelAndView;
}}
index.jsp
<h2>Hello World</h2>
<form action="./hello.code">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
success.jsp
<p>Hello ${msg}</p>
Run Project:
- Right click on project -> Run As -> Run As Server
Hit http://localhost:8080/IOC-Web/
- Insert value and click on Submit and also check console.
Test.Test()
Code Factory