我的问题是在整合spring3.0和struts2的2.1.1的版本的时候出现的,下面我列出来我的主要配置:
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">
    
        <!-- begin Spring配置 -->
     <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml;</param-value>
 </context-param>    <filter>
        <filter-name>ssh</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ssh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>  
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>2:我的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.multipart.maxSize" value="4194304" /> 
      <package name="strtus2TestOne" namespace=""  extends="struts-default">
          <action name="struts2Test" class="struts2TestAction">
              <result name="positive">/positive.jsp</result>
              <result name="negative">/negative.jsp</result>
          </action>
      </package>
  </struts>
3:我的action:
package com.cn.wadata.strtus2.action;import java.util.Map;import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionSupport;
/**
 * 测试struts2和spring的集成
 * @author jianfeng.ye 
 * 
 * strtus2
 *
 */
public class Struts2TestAction extends ActionSupport implements SessionAware{

    /**
 * 
 */
private static final long serialVersionUID = 1435913095150696163L;
private int operand1;
    private int operand2;
private Map<String, Object> session;
  
    public String index() throws Exception   //大家注意看这个方法,我本来是使用execute来作为方法名
//的,但是后来程序控制台居然提示我默认方法是index!!!
    {
        if (getSum() >= 0)  // 如果代码数和是非负整数,跳到positive.jsp页面
        {
            return "positive";
        }
        else  // 如果代码数和是负整数,跳到negative.jsp页面
        {
            return "negative";
        }
    }
  
    public int getOperand1()
    {
        return operand1;
    }
  
    public void setOperand1(int operand1)
    {
        System.out.println(operand1);
          this.operand1 = operand1;
    }
  
    public int getOperand2()
    {
        return operand2;
    }  
    public void setOperand2(int operand2)
    {
        System.out.println(operand2);
        this.operand2 = operand2;
    }
    public int getSum()
    {
        return operand1 + operand2;  // 计算两个整数的代码数和
    } public void setSession(Map<String, Object> session) {
this.session = session;

}
}
4:我的问题:
:我要说明下,我的配置应该是没有问题的,请大家不要回复诸如bean有没有注入的问题。因为大家一定要看清楚,很明显,我的程序是可以访问的。只不过不能以我想要的方式。所以接下来我要说明下我奇怪的问题所在:
我在项目启动以后访问:http://localhost:8089/ssh/struts2Test!execute.action
不行,报404错误.然后我想了很多办法,最后我在struts.xml文件加了一句:  <constant name="struts.action.extension" value="do,action" />我再用:http://localhost:8089/ssh/struts2Test!execute.action
访问,可以了。我的问题是:strtus2应该默认是.action结尾的吧,为什么我这里不加这句话就访问不到呢???另外,还有一个很诡异的问题,大家都说strtus2默认的方法入口是execute方法,但是我在实验过程中发现,竟然不是!而是以index方法为入口。真是诡异!!!!!我为什么会想到是这个方法呢?因为提示我说没有这个方法,然后我加了以后就可以访问了。

解决方案 »

  1.   

    http://localhost:8089/ssh/struts2Test!execute.action
    貌似这种方式是要配置一下才可以访问的!
      

  2.   

    不需要啊,按道理来说struts2默认就是action结尾的!这个也正是我奇怪的地方
      

  3.   

    我拿你的Action代码放在纯Struts的环境下跑,依然是需要使用execute方法名,然后访问时也没有什么问题。不行你再贴你Spring的配置文件大家看看?
      

  4.   

    OK,spring的配置文件:<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    <beans>
        <!-- 配置文件加载 -->
        <bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
     <value>WEB-INF/dbconfig.properties</value> 
    </list>
    </property>
    </bean>     <!-- 数据源配置 -->
        <bean id="dataSourceTMS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
            <property name="driverClassName">
                <value>${jdbc.driverClassName}</value>
            </property>
            <property name="url">
                <value>${jdbc.url.for.xtgl}</value>
            </property>
            <property name="username">
                <value>${jdbc.username.for.xtgl}</value>
            </property>
            <property name="password">
                <value>${jdbc.password.for.xtgl}</value>
            </property>
            <property name="maxActive">
                <value>${maxActive}</value>
            </property>
            <property name="maxWait">
                <value>${maxWait}</value>
            </property>
            <property name="maxIdle">
                <value>${maxIdle}</value>
            </property>
            <property name="validationQuery">
                <value>${validationQuery}</value>
            </property>
        </bean>
        
       <bean id="dataSource" class="com.cn.wadata.util.impl.DynamicDataSource">      
                <property name="targetDataSources">      
                   <map key-type="java.lang.String">      
                      <entry key="1" value-ref="dataSourceTMS"/>   
                   </map>      
                </property>      
                <property name="defaultTargetDataSource" ref="dataSourceTMS"/>      
        </bean>   
        
        <!--创建一个bean:struts2TestAction,其实现类为Struts2TestAction -->  
          <bean id="struts2TestAction" class="com.cn.wadata.strtus2.action.Struts2TestAction">  
          </bean>
        
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <property name="dataSource" ref="dataSource"/>
        </bean>

    </beans>
    但是我要声明下,因为spring的配置文件引起的可能性极度小楼上的呢要么去下载个最新版本的struts2试试看,我真的觉得好诡异
      

  5.   

    你新帖的代码(数据源定义除外)我这边加进去没问题。
    走的是execute,也无需加配constant我用的Spring3.0.4  Struts2.2.1
    建议你建一个新项目将上述代码测试一遍。再逐一排除其他代码问题。
      

  6.   

    建议,楼主有问题先看一看LOG,还有 别忘记你的web.xml的配置