Friday, October 14, 2005

example of common use of web application context with struts

<!—web.xml -->

<web-app>

<display-name>Config Filter Application</display-name>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/webContext-hibernate.xml</param-value>

</context-param>

<servlet>

<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Standard Action Servlet Configuration (with debugging) -->

<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>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>2</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<!-- Standard Action Servlet Mapping -->

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<!-- The Usual Welcome File List -->

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!-- Struts Tag Library Descriptors -->

<taglib>

<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-html.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/c.tld</taglib-uri>

<taglib-location>/WEB-INF/c.tld</taglib-location>

</taglib>


</web-app>

//ACTION BASE CLASS

import javax.servlet.ServletContext;
import org.apache.struts.action.Action;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class SpringBeanAccessorAction extends Action {

/** To be cast by client
*/

protected Object makeBLServiceBean(String name)
{

ServletContext servletContext = this.servlet.getServletContext();
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
Object blservice = context.getBean(name);
return blservice;

}

}

No comments: