当前位置: 首页 > 工具软件 > NanoContainer > 使用案例 >

nanocontainer-struts 的简单应用(转)

宇文飞羽
2023-12-01

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

<!-- ================================================ Form Bean Definitions --&gt


name="inputForm"
type="org.nanocontainer.sample.nanowar.struts.CheeseForm"/>



<!-- ========================================= Global Exception Definitions --&gt


<!-- sample exception handler
key="expired.password"
type="app.ExpiredPasswordException"
path="/changePassword.jsp"/>
end sample --&gt


<!-- =========================================== Global Forward Definitions --&gt


<!-- Default forward to "Welcome" action --&gt
<!-- Demonstrates using index.jsp to forward --&gt

<!-- =========================================== Action Mapping Definitions --&gt


path="/nanicoTest"
type="org.nanocontainer.sample.nanowar.struts.CheeseAction"
name="inputForm" >

path="/result.jsp" />



<!-- ============================================= Controller Configuration --&gt

processorClass="org.nanocontainer.nanowar.struts.PicoRequestProcessor"/>


<!-- ======================================== Message Resources Definitions --&gt



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)+"--
");
%>





zmlogin.jsp












Name
Country











5.提交的结果页面 result.jsp
out.println("=="+request.getAttribute("cheesesOfTheWord"));
%>

将这几个文件保存,放到合适的位置,启动tomcat,请求naoweb.jsp文件即可。
我们可以从源码CheeseAction看到,CheeseService 是个接口,这个代码中并没有给出接口的实现,
并没有和某一个具体的实现产生依赖关系,可见依赖关系是在运行时由容器建立的,pico容器使用的是构造子注入,可以看到代码中有对应的构造器。容器是从nanocontainer.groovy配置文件中找到具体实现的。然而代码是可以正常运行的,这就是我们想要达到的理想的效果,真正的面向接口的编程。这样的结果对我们的单元测试是非常有利的。
public class CheeseAction extends Action {

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/

 类似资料: