日期:2014-05-20  浏览次数:21016 次

spring集成struts2访问不到action,也不报错
action类:
我在LoginAction中的login()中输出了一句话,好像访问的时候根本就没这个action嘛,没任何输出,是不是少配置了啥啊?

Java code



package com.xcd.action;

import com.opensymphony.xwork2.ActionSupport;
import com.xcd.service.LoginService;

public class LoginAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private LoginService loginService;

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    public String execute(){
        
        return SUCCESS;
    }
    
    public String login(){
        System.out.println("action...");
        return SUCCESS;
    }
}




配置文件:


XML code

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">
    
    <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>
  
  <!-- spring的监听器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 指定spring配置文件的位置,这里是在类路径里 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.objectFactory" value="spring" />
    <!-- <include file="example.xml"/>  -->


    <!--  
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
    -->
    <!-- Add packages here -->
    
    <package name="loginAction" namespace="/" extends="struts-default">
        <default-action-ref name="login"></default-action-ref>
        <action name="login" class="loginAction" method="login">
            <result name="success">index.jsp</result>
        </action>
    </package>
</struts>


applicationContext.xml:


<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    ">

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml">
        </property>
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property>
    </bean>
    
    <bean id="userDAO" class="com.xcd.dao.UserDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <bean id="loginService" class="com.xcd.service.LoginServiceImpl">
        <property name="userDAO" ref="userDAO"></property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="loginAction" class="com.xcd.action.LoginAction" scope="prototype">
        <property name="loginService" ref="loginService"></property>
    </bean>
    
</beans>