Spring Web Module
Spring web module is given for two important operations
1.To Make existing spring application communication from
other web framework software based web application like struts,JSF…ect
2.This Module gives WEB MVC/Web work flow software as
spring own web framework software
to develop MVC2 architecture based web
applications
Sample Program to describe Spring and Struts Communication
Application Structure
Step-1:Create Index1.jsp as shown below.
Application Structure
Step-1:Create Index1.jsp as shown below.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<html>
<%@taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<head>
</head>
<body>
<html:form action="/demo">
Enter User name:<html:text property="uname"></html:text>
<html:submit>Wish</html:submit>
</html:form>
</body>
</html>
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>12samplespring_struts_approach1</display-name>
<welcome-file-list>
<welcome-file>index1.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Step 3: Create struts-config.xml
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="demofrm" type="demopack.DemoForm"/>
</form-beans>
<action-mappings>
<action path="/demo" name="demofrm" type="demopack.DemoAction">
<forward name="success" path="/result.jsp" />
</action>
</action-mappings>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
</plug-in>
</struts-config>
Step 4:create action-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="d1" class="demopack.ModelImpl"></bean>
</beans>
Step 5: Create FormBean class
package demopack;
import org.apache.struts.action.ActionForm;
public class DemoForm extends ActionForm {
private static final long serialVersionUID = 1L;
private String uname;
public void setUname(String uname) {
this.uname = uname;
}
public String getUname() {
return uname;
}
}
Step 6:Create ModelInter.java(interface)
package demopack;
public interface ModelInter {
public String getWish();
}
Step 7:Implementation for above interface(ModelImpl.java)
package demopack;
public class ModelImpl implements ModelInter {
public String getWish() {
// TODO Auto-generated method stub
return "Good Morning";
}
}
Step 8: Create DemoAction.java
package demopack;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;
public class DemoAction extends ActionSupport {
public ActionForward execute(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{
System.out.println("In Execute Method is started.....");
DemoForm f1=(DemoForm)form;
String uname=f1.getUname();
WebApplicationContext context=getWebApplicationContext();
ModelInter m1=(ModelInter)context.getBean("d1");
String msg=m1.getWish()+" "+uname;
req.setAttribute("message", msg);
return map.findForward("success");
}
}
Step 9: result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hai.....
<%=request.getAttribute("message") %>
</body>
</html>
No comments:
Post a Comment