整合S2SH时做了个测试程序,总是报无法实例化Action的错误。郁闷。
测试部分代码是:
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentAction students = new StudentAction();students.leader();
运行时总是显示Action不能实例化,开始很郁闷。看配置什么的都没问题啊。最后发现用
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentBean students = (StudentBean)ctx.getBean("studentBean");
students.findLeader();
就没有问题,郁闷中。。
后来发现应该是这样的。
Struts.xml中应该是这样:
<action name="loginAction" class="loginAction" >//此处的action class属性应该是applicationContext中spring实例化该action的 bean id 属性值。这样才是spring实例化的。
<result name="leader">/Class_intro.jsp</result>
<result name="cards">/Class_member.jsp</result>
</action>
applicationContext.xml中:
<bean id="loginAction"
class="com.struts.action.StudentAction"
scope="prototype">
<property name="studentBean" ref="studentBean" />
</bean>
struts1.x
<action name="treeForm" path="/treeAction" scope="request"
parameter="actions"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="select"
path="/platform/common/treeSelect.jsp" />
<forward name="defeat" path="/common/error.jsp" />
</action>
applicationContext.xml中:
<bean id="treeAction"
class="com.struts.action.treeAction"
scope="prototype">
<property name="studentBean" ref="studentBean" />
</bean>