我是使用了struts 2 和 spring做一个简单的尝试,结果发现到使用依赖注入的对象时后台似乎无法执行了,控制台也没有输出异常,连下一步的打印都没有,
但是应该是出错了,因为页面跳转到了struts.xml配置的错误页面。附上代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results>
<result name="error">/failed.html</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>
<action name="testAction" class="test.TestAction">
<result name="success">/successed.html</result>
</action>
</package>
<!-- Add packages here --></struts>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
"> <bean id="testService" class="test.TestService"></bean>
<bean id="testAction" class="test.TestAction">
<property name="testService">
<ref bean="testService"></ref>
</property>
</bean></beans><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>test Project</display-name> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
package test;public class TestService { public void test(){
System.out.println("进入服务");
}
}
package test;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport {
private TestService testService; public TestService getTestService() {
return this.testService;
} public void setTestService(TestService testService) {
this.testService = testService;
} public String execute() {
System.out.println("进入ACTION");
testService.test();
System.out.println("---***");
return SUCCESS;
}
}
执行testAction.action,输出如下:
进入ACTION既没有异常也没有其他信息,但是页面跳转到了/failed.html,所以认为应该是出错了,但是不知道为什么。因为是初学spring,所以不知道有没什么地方没有作配置或错误。请大家指教
但是应该是出错了,因为页面跳转到了struts.xml配置的错误页面。附上代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results>
<result name="error">/failed.html</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>
<action name="testAction" class="test.TestAction">
<result name="success">/successed.html</result>
</action>
</package>
<!-- Add packages here --></struts>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
"> <bean id="testService" class="test.TestService"></bean>
<bean id="testAction" class="test.TestAction">
<property name="testService">
<ref bean="testService"></ref>
</property>
</bean></beans><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>test Project</display-name> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
package test;public class TestService { public void test(){
System.out.println("进入服务");
}
}
package test;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport {
private TestService testService; public TestService getTestService() {
return this.testService;
} public void setTestService(TestService testService) {
this.testService = testService;
} public String execute() {
System.out.println("进入ACTION");
testService.test();
System.out.println("---***");
return SUCCESS;
}
}
执行testAction.action,输出如下:
进入ACTION既没有异常也没有其他信息,但是页面跳转到了/failed.html,所以认为应该是出错了,但是不知道为什么。因为是初学spring,所以不知道有没什么地方没有作配置或错误。请大家指教
解决方案 »
- hibernate.cfg.xml配置了多个<mapping resource="/?hbm.xml" />之类的文件,配置一个就能执行
- SSH架构业务逻辑代码写到那一层比较好?
- mysql中可以嵌套java或者c吗?
- 在struts框架中struts.xml中的一些代码解释一下
- hibernate到底有多少实用价值???
- 如果在JSP页面首次显示时,同时从两个Action获取数据
- 请问如何把这个对象重新加入到session范围内呢?
- 关于session的java.lang.NullPointerException
- 我刚看了java.sun.com上的jndi例子,但是就是调试不可以通过,请高手看看,俺是菜鸟,见笑了;)
- 问高手们开发ejb问题,我刚才那个问题,不能打开了看了,请各位再给我指点下,在线等待,急急!!!!!!!!!!!!!!!!!!
- web servies
- 急求MyEclipse_6_0_1GA_E3_3_1_Installer.bin 文件
<action name="testAction" class="test.TestAction">
<result name="success">/successed.html</result>
</action>
改成<action name="testAction" class="testAction">
<result name="success">/successed.html</result>
</action>2、struts.xml里面加上<constant name="struts.objectFactory" value="spring" />3、struts-spring-plugin.jar有加进去吗
但是因为我是初学,并且没有SSH整合的经验,可以再向我解释一下这个问题的因由和解决的原理吗?
但是因为我是初学,并且没有SSH整合的经验,可以再向我解释一下这个问题的因由和解决的原理吗?
1、<constant name="struts.objectFactory" value="spring" />这句话的意思是吧action交给spring去管理
2、<action name="testAction" class="test.TestAction">这样写的话 还是你自己创建了testaction的实例
你在spring的配置文件中已经不是已经配置了
<bean id="testAction" class="test.TestAction">
<property name="testService">
<ref bean="testService"></ref>
</property>
</bean>
交给spirng管理当然要用spring的bean了 所以要改成<action name="testAction" class="testAction">