求助一个struts2跳转问题

duzidengdaichongfeng 2012-05-04 03:47:02
自己搭了个SSh框架。在地址栏输入http://localhost:7080/mobilBuyTicket/twe/mobilBooking!test.action测试数据测持久化。打断点能访问到test方法。但是无法跳转到result页面

web.xml中
<!-- Struts2拦截器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>main.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

struts.xml中:
<action name="input" class="OutputAction" method="actionMethod">
<result name="output">/success.jsp</result>
</action>

action中方法

public String test(){
//this.mobilBookingService.creat();
return "out";
}
我把业务代码都注释掉了,就一个跳转,就跳不过去。提示
No result defined for action main.action.MobilBookingAction and result out
我把斜杠去掉<result name="output">success.jsp</result>还是报错,变为<result name="out" type="redirect">/success.jsp</result>也报错,我的success.jsp页面就是建在webRoot下的
请问是什么问题啊
...全文
373 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
有点小进展,我发现了个错误。我的success.jsp建文件名时前面多了个空格,删掉后。可以进入action的断点了,但是跳转不到jsp。报错No result defined for action main.action.MobilBookingAction and result out2。我的struts.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="strut2" extends="struts-default" >
<interceptors>
<interceptor-stack name="testStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="testStack"></default-interceptor-ref>
</package>

<package name="mobil" namespace="/twe" extends="struts-default" >
<action name="mobilBooking" class="mobilBookingAction" method="test">
<result name="out2">/success.jsp</result>
</action>
</package>
</struts>
把mobil包的继承extends改为上面的包strut2的话,连action断点也进不去。报以前的错

换种方法,加入struts2-codebehind-plugin-2.1.6.jar包。删掉struts.xml中mobil包的配置

action改为

@Namespace("/twe")
@Results( { @Result(name = "out2", value = "/success.jsp")
})
public class MobilBookingAction extends ActionSupport{

private static final long serialVersionUID = 1L;
private MobilBookingService mobilBookingService;

public MobilBookingService getMobilBookingService() {
return mobilBookingService;
}

public void setMobilBookingService(MobilBookingService mobilBookingService) {
this.mobilBookingService = mobilBookingService;
}

public String test(){
//this.mobilBookingService.creat();
断点处return "out2";
}
} 这种注解方法就可以跳转成功。可用xml配置就找不到视图。求高手指点!
昨日凡阳 2012-05-05
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 的回复:]

引用 17 楼 的回复:
1、你是否有结合spring?将struts的action交由spring托管?
2、如果1没有,则struts.xml中的配置,

struts.xml中:
<action name="input" class="OutputAction" method="actionMethod">
<result name="output">/success.jsp……
[/Quote]

6楼没有<constant name="struts.objectFactory" value="spring" />这句。我怀疑你struts 2 + spring配置的不对,另外,既然你交由spring管理bean了,你的application.xml中,
OutputAction是如何宣告的?另外,最好将这个名字改为output。
在struts.xml中,改为

<action name="input" class="output" method="test">
<result name="output">/success.jsp</result>
</action>
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 的回复:]
1、你是否有结合spring?将struts的action交由spring托管?
2、如果1没有,则struts.xml中的配置,

struts.xml中:
<action name="input" class="OutputAction" method="actionMethod">
<result name="output">/success.jsp</result>
</a……
[/Quote]

我发帖时黏贴错了,又没有权限修改,请看6楼。我是交给spring了。写了<constant name="struts.objectFactory" value="spring" />了,不知道哪里错了。晕啊
昨日凡阳 2012-05-05
  • 打赏
  • 举报
回复
1、你是否有结合spring?将struts的action交由spring托管?
2、如果1没有,则struts.xml中的配置,

struts.xml中:
<action name="input" class="OutputAction" method="actionMethod">
<result name="output">/success.jsp</result>
</action>

不对,class要写全路径。
并且,method也不对,也给成你的test
昨日凡阳 2012-05-05
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

引用 13 楼 的回复:
改成如下,struts 2的不同版本,配置是有差异的,另外,你的package命名不规范。

XML code


<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecu……
[/Quote]

2.1.3之后,就必须用StrutsPrepareAndExecuteFilter这个class了。
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
改成如下,struts 2的不同版本,配置是有差异的,另外,你的package命名不规范。

XML code


<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</fi……
[/Quote]

我试过这个新的默认拦截器,还是不行。我都把struts2由2.1.8降为2.1.6了,换了好几个jar包,还是不行。真是晕了
聖傑 2012-05-05
  • 打赏
  • 举报
回复
把过滤器
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
换成
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
struts.xml的配置不正确
<action name="input" class="OutputAction" method="actionMethod">
<result name="output">/success.jsp</result>
</action>
其中的class应该加上OutputAction所在的包名,且要保证OutputAction中有actionMethod方法,不然无法实现跳转
根据你所说的
action中方法
public String test(){
//this.mobilBookingService.creat();
return "out";
}
上面的struts.xml应该这样配置(class要加上包名!)
<action name="input" class="包名.OutputAction" method="test">
<result name="out">/success.jsp</result>
</action>

还有一点是,你所写的jsp页面的action=“”是否写正确。
比如:
index.jsp
<form action="aa/test">
struts.xml
<package name="default" namespace="/aa"extends="struts-default">
<action name="test" class="包名.OutputAction" method="test">
<result name="out">/success.jsp</result>
</action>

希望能帮到楼主!
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 的回复:]
struts 2是什么版本,2.1.3之后,要用StrutsPrepareAndExecuteFilter。
[/Quote]
struts是2.1.8后来改了2.1.6换成org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter也不行。
算了,先用注解做吧。结贴了,散分。第一次提问,体验下怎么散分,呵呵。
昨日凡阳 2012-05-05
  • 打赏
  • 举报
回复
改成如下,struts 2的不同版本,配置是有差异的,另外,你的package命名不规范。

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>main.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern> 拦截.action 方法
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern> 拦截.action 方法
</filter-mapping>
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
把这个“web.xml中
<!-- Struts2拦截器 -->
<init-param>
<param-name>actionPackages</param-name>
<param-value>main.action</param-value>
</init-param>
”删掉,试试吧
[/Quote]
我把上面的删掉了。tomcat启动报错Caused by: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=java.lang.String, name='actionPackages'] in public void org.apache.struts2.config.ClasspathPackageProvider.setActionPackages(java.lang.String).我又把struts2-codebehind-plugin-2.1.6.jar包删掉后tomcat启动正常
可这次连action都访问不到了。报错
2012-05-05 08:48:33,765 - org.apache.struts2.dispatcher.Dispatcher -33656 [http-7080-1] WARN - Could not find action or result
There is no Action mapped for action name mobilBooking. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1600)
at java.lang.Thread.run(Unknown Source)
愁死我了,是不是jar包有问题啊。
昨日凡阳 2012-05-05
  • 打赏
  • 举报
回复
struts 2是什么版本,2.1.3之后,要用StrutsPrepareAndExecuteFilter。
cxw3152 2012-05-05
  • 打赏
  • 举报
回复
你返回的字符串是out但是咋配置文件中配置的是output
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 的回复:]
引用 20 楼 的回复:

有点小进展,我发现了个错误。我的success.jsp建文件名时前面多了个空格,删掉后。可以进入action的断点了,但是跳转不到jsp。报错No result defined for action main.action.MobilBookingAction and result out2。我的struts.xml如下
<?xml version="1.0" e……
[/Quote]

感谢昨日凡阳的指导,actionPackages原来是注解的配置,我还以为是过滤器的配置呢。可是我删掉这段后。又把struts2-codebehind-plugin-2.1.6.jar包删掉(否则启动报错)。再运行连action断点都进不去了,这是为什么啊。还是哪里配置不对啊。
我的web.xml配置如下

<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 添加spring监听,加载spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 添加spring上下文使用的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:resource/applicationContext.xml
</param-value>
</context-param>

<!-- Struts2拦截器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>

srping配置如下

<?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:flex="http://www.springframework.org/schema/flex"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName">

<context:property-placeholder location="classpath:resource/jdbc.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass"><value>${jdbc.driverClassName}</value></property>
<property name="jdbcUrl"><value>${jdbc.url}</value></property>
<property name="user"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
<property name="minPoolSize"><value>10</value></property>
<property name="maxPoolSize"><value>100</value></property>
<property name="maxIdleTime"><value>1800</value></property>
<property name="acquireIncrement"><value>2</value></property>
<property name="maxStatements"><value>0</value></property>
<property name="initialPoolSize"><value>10</value></property>
<property name="idleConnectionTestPeriod"><value>1000</value></property>
<property name="acquireRetryAttempts"><value>30</value></property>
<property name="acquireRetryDelay"><value>100</value></property>
<property name="breakAfterAcquireFailure"><value>false</value></property>
<property name="testConnectionOnCheckout"><value>false</value></property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> -->
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:resource/hibernate.cfg.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- dao注入 -->
<bean id="merchantDao" class="main.dao.imp.MerchantDaoImp"/>
<!-- service注入 -->
<bean id="mobilBookingService" class="main.service.imp.MobilBookingServiceImp"/>


<!-- action注入 -->
<bean id="mobilBookingAction" class="main.action.MobilBookingAction"/>
</beans>
请高手指教
昨日凡阳 2012-05-05
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 的回复:]

有点小进展,我发现了个错误。我的success.jsp建文件名时前面多了个空格,删掉后。可以进入action的断点了,但是跳转不到jsp。报错No result defined for action main.action.MobilBookingAction and result out2。我的struts.xml如下
<?xml version="1.0" encoding="UTF-8"……
[/Quote]

还是配置的问题呀。是相关jar的问题。

你记得吗。你的web.xml中,


<init-param>
<param-name>actionPackages</param-name>
<param-value>main.action</param-value>
</init-param>

这句话是什么作用吗?
你既然有这句话,说明你的action就是希望通过注解来宣告。
luckdjc 2012-05-04
  • 打赏
  • 举报
回复
No result defined for action main.action.MobilBookingAction and result out!!!
没有out对应的result视图啊 你检查下 result中 的name'写对了没
luckdjc 2012-05-04
  • 打赏
  • 举报
回复
<action name="mobilBooking" class="main.action.MobilBookingAction" method="test">


在你的action类中 有这个test()方法?
wangdanyangtc 2012-05-04
  • 打赏
  • 举报
回复
我觉得也是拦截器actionPackages的问题
herminen 2012-05-04
  • 打赏
  • 举报
回复
把这个“web.xml中
<!-- Struts2拦截器 -->
<init-param>
<param-name>actionPackages</param-name>
<param-value>main.action</param-value>
</init-param>
”删掉,试试吧

  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</……
[/Quote]

改成这样也跳转不到jsp
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
一般写成上面那样 就可以截取到浏览器所有的请求, 简单些
你的Struts的配置文件 class 应该是action的路径名称!
[/Quote]

我上面黏贴错了。我的struts.xml配置文件中是
<package name="strut3" namespace="/twe" extends="struts-default" >
<action name="mobilBooking" class="main.action.MobilBookingAction" method="test">
<result name="out2" type="redirect">/success.jsp</result>
</action>
</package>

能访问到action。就是跳转不到jsp
加载更多回复(5)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧