nanocontainer对picocontainer容器作了一些扩充的包装,它提供了用多种脚本(groovy, .bsh, .js, .py or .xml等)配置容器管理的对象的功能,同时提供了对web容器的支持,并且针对struts、webwork也有专门的支持,这里我调试成功了在struts中如何使用nanocontainer容器。nanocontainer的网站上的文档提供的非常少,从这里可以下载到一个简单的web应用的例子。
http://dist.codehaus.org/nanocontainer/war/nanocontainer-sample-nanowar.war
但是这个war并不完整,解开war看到只有一些编译过的class,要看源代码可以从cvs上下载到
http://www.nanocontainer.org/Source+Repositories
并没有web.xml struts-config.xml等配置文件和jsp文件。nanocontainer也太不注重文档和例子代码的详细程度了。我自己补充完整了这个例子需要的其他文件,在下面贴出来。
运行这个例子需要以下几个文件:
1. web.xml
这个文件只要从struts提供的例子中的blank中找到一个空的web.xml文件,加入下面配置就可以了
nanocontainer.groovy
/WEB-INF/nanocontainer.groovy
org.nanocontainer.nanowar.ServletContainerListener
2.nanocontainer.groovy 放到/WEB-INF中,我在这里使用了groovy 脚本的配置方式。
pico = new org.picocontainer.defaults.DefaultPicoContainer(parent)
if(assemblyScope instanceof javax.servlet.ServletContext) {
pico.registerComponentImplementation(org.nanocontainer.sample.nanowar.service.defaults.DefaultCheeseService)
pico.registerComponentImplementation(org.nanocontainer.sample.nanowar.dao.simple.MemoryCheeseDao)
} else if(assemblyScope instanceof javax.servlet.http.HttpSession) {
} else if(assemblyScope instanceof javax.servlet.ServletRequest) {
}
3.struts-config.xml文件内容如下
<?xml version="1.0" encoding="ISO-8859-1" ?>
br /> "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<!-- ============================================ Data Source Configuration -->
<!-- ================================================ Form Bean Definitions -->
name="inputForm"
type="org.nanocontainer.sample.nanowar.struts.CheeseForm"/>
<!-- ========================================= Global Exception Definitions -->
<!-- sample exception handler
key="expired.password"
type="app.ExpiredPasswordException"
path="/changePassword.jsp"/>
end sample -->
<!-- =========================================== Global Forward Definitions -->
<!-- Default forward to "Welcome" action -->
<!-- Demonstrates using index.jsp to forward -->
<!-- =========================================== Action Mapping Definitions -->
path="/nanicoTest"
type="org.nanocontainer.sample.nanowar.struts.CheeseAction"
name="inputForm" >
path="/result.jsp" />
<!-- ============================================= Controller Configuration -->
processorClass="org.nanocontainer.nanowar.struts.PicoRequestProcessor"/>
<!-- ======================================== Message Resources Definitions -->
4. naoweb.jsp 一个测试的jsp form表单页面。
http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
http://jakarta.apache.org/struts/tags-html" prefix="html" %>
http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
out.println(session.getAttribute(org.apache.struts.Globals.LOCALE_KEY)+"--
");
%>
Name | |
Country | |
|
private final CheeseService cheeseService;
public CheeseAction(CheeseService cheeseService) {
this.cheeseService = cheeseService;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
CheeseForm cheeseForm = (CheeseForm) form;
if (!isEmpty(cheeseForm.getName())) {
Cheese cheese = new Cheese(cheeseForm.getName(), cheeseForm.getCountry());
cheeseService.save(cheese);
}
Collection cheeses = cheeseService.getCheeses();
request.setAttribute("cheesesOfTheWord", cheeses);
return mapping.findForward("next");
}
private boolean isEmpty(String s) {
return s == null || "".equals(s.trim());
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-122483/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10294527/viewspace-122483/