我的applicationContext.xml和struts-config.xml都在WEB-INF一个目录下边。
服务器用的Tomcat。启动Tomcat以后运行test.java以后结果为null。
不知道为什么?
是applicationContext.xml文件没有配struts加载还是我那里写错了,请大侠们帮忙看看!!!!!!
applicationContext.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="cctv" class="test.cctv" abstract="false" singleton="true"
lazy-init="default" autowire="default" dependency-check="default">
<property name="user">
<value type="java.lang.String">user</value>
</property>
</bean></bean>
struts-config.xml 文件--------------------------------<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"><struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings />
<message-resources parameter="my.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
cctv.java 文件--------------------------------------
package test;public class cctv {
private String user; public String getUser() {
return user;
} public void setUser(String user) {
this.user = user;
}
}
test.java 文件------------------------ package test;public class test { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
cctv c=new cctv();
System.out.println(c.getUser());
}}
服务器用的Tomcat。启动Tomcat以后运行test.java以后结果为null。
不知道为什么?
是applicationContext.xml文件没有配struts加载还是我那里写错了,请大侠们帮忙看看!!!!!!
applicationContext.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="cctv" class="test.cctv" abstract="false" singleton="true"
lazy-init="default" autowire="default" dependency-check="default">
<property name="user">
<value type="java.lang.String">user</value>
</property>
</bean></bean>
struts-config.xml 文件--------------------------------<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"><struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings />
<message-resources parameter="my.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
cctv.java 文件--------------------------------------
package test;public class cctv {
private String user; public String getUser() {
return user;
} public void setUser(String user) {
this.user = user;
}
}
test.java 文件------------------------ package test;public class test { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
cctv c=new cctv();
System.out.println(c.getUser());
}}
解决方案 »
- java中如何检测视频文件的格式?求高手指点
- 我是新手,帮我编写一下问题的代码吧,谢谢各位前辈了!!!
- struts的action的配置还是不行
- 关于用openstar进行测试的问题
- <html:multibox>标签是什么东西,<html:checkbox>又是什么东西?有什么区别?
- JSF中,用richfaces的<rich:effect>不起作用
- tomcat5 的classes目录放在哪里﹗
- Tomcat是不是很不稳定?
- 哪里有jfreechart的文档
- 急求!!!!请各位大侠帮帮忙,错误!
- 50分求个通用的分页代码,帮顶的也有分,:)谢谢了
- 怎么动态生成播放列表ASX文件?(请求各位高人指点,急!!!在线等~~~)
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<controller processorClass= "org.springframework.web.struts.DelegatingRequestProcessor " /> 。 以后报错: The content of element type "struts-config" must match "(data- sources?,form-beans?,global-exceptions?,global-forwards?,action- mappings?,controller?,message-resources*,plug-in*)".
我没有用到Struts 的action 和 form-bean 。
我只是想用Struts加载spring,用Spring来初始化数据。
可是用<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>不知道为什么加载不上.
是因为c.getUser()==null,你没有初始化USER,把你的test类改为:package test;public class test { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
cctv c=new cctv();
c.setUser("haha");
System.out.println(c.getUser());
}}
如控制台输出haha,就说明STRUTS加载SPRING成功。
这个是自己赋的值,好像和SPRING没关系吧?
public void testQuickStart() {
ApplicationContext ctx=new
FileSystemXmlApplicationContext("bean.xml");
Action action = (Action) ctx.getBean("TheAction");
System.out.println(action.execute("Rod Johnson"));
}package test;public class test { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
ApplicationContext ctx=new FileSystemXmlApplicationContext("applicationContext.xml");
cctv c = (cctv) ctx.getBean("cctv");
System.out.println(c.getUser());
}}
其实这同STRUTS和SPRING整合无关
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
ApplicationContext ctx=new FileSystemXmlApplicationContext("applicationContext.xml");
cctv c = (cctv) ctx.getBean("cctv");
System.out.println(c.getUser());
}}建议你先看看书,如林信良的《spring2.0技术手册》
我想通过STRUTS的<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
来加载applicationContext.xml文件。可是加载不上。
你能帮我看看如何通过STRUTS来加载applicationContext.xml文件吗?
--------------------------------------------------------------------------------
eRedLab-JaNer [email protected]
关键词: Spring、 Jakarta-Struts 、WebIDE、 MVC、 J2EE引言
Spring是一个轻量级(大小和系统开支的角度)的IoC和AOP容器.它力图简化J2EE开发即J2EE without EJB.而且作为帮助企业级开发的核心支柱,Spring为模型层(OR持久层:Hibernate、JDO、iBatis等)服务层(EJB、JNDI、WebService)以及表现层(Struts、JSF、Velocity)都提供了良好的支持和集成方案. 访问Spring官方站
Jakarta-Struts是Apache软件组织提供的一个开源项目.它为Java Web应用提供了基于Model-View-Controller的MVC框架,尤其适用于开发大型可扩展的Web应用.尽管基于Java的MVC框架层出不穷,事实上Spring的MVC模型也提供了驱动应用系统Web层的能力,但Jakarta-Struts仍然是所有这些框架中的佼佼者. 访问Struts官方站
下面,我就将如何整合这两个J2EE领域的经典项目给出两套详尽的集成方案.进入主题
1.首先我们来看一个Spring-Struts整合应用下的控制器Action类源代码.
public class CourceAction extends Action {
private CourceService courceService;
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Set allCources = courceService.getAllCources();
//..........the other statements
request.setAttribute("cources", allCources);
return mapping.findForward("jspView");
} }
分析:CourceService为一个业务实现的接口,此接口声明了一系列的业务处理方法.该方法的实现配置为Spring上下问的一个Bean.由此看来,我们大家都可能会产生一个疑问:Struts action如何取得一个包含在Spring上下文中的Bean呢?为了回答这个问题,Spring提供了两种与Struts集成的方式:(1).从一个知晓Spring上下文的基类派生我们自己的Struts Action类.然后,在派生类中就可以使用super.XX()方法来获得一个对Spring受控Bean的引用.
(2).将请求委托给作为Spring Bean管理的Struts Action来处理.
2.注册Spring插件:为了使Struts Action能够访问由Spring管理的Bean,我们就必须要注册一个知道Spring应用上下文的Struts插件.可以在struts-config.xml中通过如下的方式来完成注册.
<plug-in classname="org.springframework.web.struts.ContextLoadPlugin">
<set-property value="WEB-INF/Yhcip.xml,......" property="contextConfigLocation">
</PLUG-IN>
ContextLoadPlugin()负责装载一个Spring应用上下文.(具体的说:是一个WebApplicationContext).value属性值为要加载的配置Spring受控Bean的xml文件的URI.
3.完成第一种集成方案:实现一个知晓Spring的Action基类.
这种集成方案是从一个公共的能够访问Spring应用上下文的基类中派生所有的Struts Action,但值得庆幸的是:我们不用自己去编写这个知晓Spring应用上下文的基类,因为Spring已经提供了org.springframework.web.struts.ActionSupport:一个org.apache.struts.action.Action的抽象实现.它重载了setServlet()方法以从ContextLoaderPlugin中获取WebapplicationContext.因此,任何时候我们只需要调用super.getBean()方法即可获得一Spring上下文中的一个Bean的引用.
我们再来看一段Action源代码:
public class CourceAction extends ActionSupport {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
//取得Spring上下文
ApplicationContext context = super.getWebApplicationContext();
//取得CourceService Bean CourseService courseService = (CourseService) context.getBean("courseService");
Set allCources = courceService.getAllCources(); request.setAttribute("cources", allCources);
//..........the other statements.
return mapping.findForward("jspView");
}}
分析:这个Action类由ActionSupport派生,当CourceAction需要一个Spring受控Bean时:它首先调用基类的getWebApplicationContext()方法以取得一个Spring应用上下文的引用;接着它调用getBean()方法来获取由Spring管理的courceService Bean的一个引用.
小结
至此,我们已经用第一种方案圆满的完成了Spring与Struts的集成工作.这种集成方式的好处在于直观简洁容易上手.除了需要从ActionSupport中派生,以及需要从应用上下文中获取Bean之外,其他都与在非Spring的Struts中编写和配置Action的方法相似.但这种集成方案也有不利的一面.最为显著的是:我们的Action类将直接使用Spring提供的特定类,这样会使我们的Struts Action(即控制层)的代码与Spring紧密耦合在一起.这是我们不情愿看到的.另外,Action类也负责查找由Spring管理的Bean,这违背了反向控制(IoC)的原则.
4.实现第二种集成方案:代理和委托Action.
这种集成方案要求我们编写一个Struts Action,但它只不过是一个包含在Spring应用上下文中的真正Struts Action的一个代理.该代理Action从Struts插件ContextLoaderPlugIn中获取应用上下文,从中查找真正的Struts Action,然后将处理委托给真正的Struts Action.这个方法的幽雅之处在于:只有代理action才会包含Spring特定的处理.真正的Action可以作为org.apache.struts.Action的子类来编写.
下面我们来看一段在之中集成方式下的Struts Action源代码:
public class CourceAction extends Action {
private CourceService courceService;
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Set allCources = courceService.getAllCources();
request.setAttribute("cources", allCources);
//..........the other statements.
return mapping.findForward("jspView");
}
/* 注入CourceService */
public void setCourceService(CourceService courceService) {
this.courceService = courceService;
}}
分析:大家可以看到,在这种方式之下,我们的Struts Action类和Spring是低耦合的,它仅仅依赖了Spring提供的反向控制(IoC)机制把CourceService注入到了我们的Action中.到此,大家肯定会有一个疑问:那就是Spring到底是如何提供IoC反向控制的呢?回答这个问题,我们需要完成两个步骤的配置:
(1).在struts-config.xml中注册Struts Action.但要注意的是我们在这里注册的是代理Action.幸运的是,我们不必亲自编写这个类.因为Spring已经通过org.springframework.web.struts.DelegatingActionProxy提供了这个代理的Action.具体的配置方法如下:
<action type="org.springframework.web.struts.DelegatingActionProxy" path="/listCourses">
(2)将真正的Struts Action作为一个Spring Bean并在Spring上下文配置文件中作为一个Bean注册之.并将Action所要引用的courceService注入给它.
<bean class="com.eRedCIP.web.CourceAction" name="/listCourses">
<property name="">
<ref bean="courseService">
</property>
</bean>
注意:name属性的值是非常重要的,它必须和struts-config.xml中的path属性完全一致.这是因为DelegatingActionProxy会使用path属性值在Spring上下文中查找真正的Action.使用DelegatingActionProxy的好处在于我们可以不使用任何Spring特定的类来编写Struts Action.同时,Struts动作能够利用IoC取得和他合作的对象.唯一不足之处就是不太直观,配置相对复杂.为了使action委托显得更为直观一些,我们可对这种集成方案做进一步的改进:使用请求委托.
5.使用请求委托.
为了使action委托看上去更为直观一些,Spring提供了DelegatingRequestProcessor,另一种专门用于Spring的请求处理器.需要在struts-config.xml中做如下配置:
<controller processorclass="org.springframework.web.struts.DelegatingRequestProcessor">
这样,DelegatingRequestProcessor将告诉Struts自动将动作请求委托给Spring上下文中的Action来处理.这使得我们可以在struts-config.xml中用struts action的真正类型来声明它们.例如:
<action type="com.eRedCIP.web.CourceAction" path="/listCourses"<
当接受到一个针对/listCourses的请求时,DelegatingRequestProcessor会自动从Spring上下文配置文件中查找一个名为/listCourses的Bean(实为一个Struts Action)类.
<action type="com.eRedCIP.web.CourceAction" path="/listCourses">结束语
在当今J2EE开发领域,涌现出了一大批优秀的开源项目可供我们选择.让J2EE软件架构师们困惑的问题也由是否要引入开源转变为引入哪些开源以及如何高效整合以做为我们的一个基础开发平台上来.在这篇文章里,我根据自己在J2EE方面的开发经验并结合学习心得向大家提供了两个整合Spring与Struts开源项目的有效集成方案.由于作者水平有限,纰漏之处恳请各路剑客侠女批评指正.
你要检查一下<controller></controller>是否在mappings和message-resources之间?