Spring Programing laungage

Spring Projects

Spring Project 1

adplus-dvertising
Example of AbstractCommandController
Previous Home Next

Directory Structure of Spring MVC application is given below:

step 1:

Create index.jsp file

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%response.sendRedirect("abstractCommandController.bean");%>

step 2:

Create web.xml file

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.bean</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

step3:

Create dispatcher-servlet.xml file

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/
schema/context/spring-context-2.5.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.
view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean name="/abstractCommandController.bean" 
class="com.r4r.AbstractCommandControllerExample"/>
</beans>

step 4:

Create AbstractCommandControllerExample.java file

package com.r4r;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.
AbstractCommandController;
public class AbstractCommandControllerExample 
	extends AbstractCommandController {
public AbstractCommandControllerExample() {
setCommandClass(MyCommand.class);
setCommandName
("This message is generated by CommandName Method");
}
@Override
protected ModelAndView handle(HttpServletRequest req,
HttpServletResponse res, Object obj, BindException ex)
throws Exception {
String message="This message generated by Controller class";
MyCommand com=(MyCommand)obj;
ModelAndView model=new ModelAndView("page");
model.addObject("msg", com.getMessage());
model.addObject("msg1", getCommandName());
model.addObject("msg2", message);
return model;
}
}

step 5:

Create MyCommand.java file

package com.r4r;
public class MyCommand {
String message="This message is generated by MyCommand class";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

step 6:

Create page.jsp file

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>AbstractCommandController</title>
</head>
<body>
<h3>AbstractCommandController Example in Spring MVC</h3>
<H4>${msg2}</H4>
<H4>${msg1}</H4>
<H4>${msg}</H4>
</body>
</html>

Output:

Previous Home Next