由于项目需求我需要使用SSH2框架。其中struts2我尝试了一下使用注解的方式,但是配置完之后总是访问不到,请各位大侠帮忙给看看:我的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> <!-- devMode模式是开发模式,默认开启了一些提示功能,方便开发排错 -->
<constant name="struts.devMode" value="true" />

<!-- 交给spring管理 -->
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.objectFactory.spring.autoWire" value="type" />

<!-- 映射JSP目录 -->
<constant name="struts.convention.result.path" value="/WEB-INF/pages/" />

<!-- 包路径包含test的将被视为Action存在的路径来进行搜索 -->
<constant name="struts.convention.package.locators" value="web,action" />

<!-- URL分割规则:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用’-’分割 -->
<constant name="struts.convention.action.name.separator" value="-" />

<!-- 配置默认的Action -->
         <package name="default" namespace="/" extends="struts-default"/>
</struts>
我的Action:package com.z.test.web;import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import com.opensymphony.xwork2.ActionSupport;@Namespace("/")
@ParentPackage("default")
@Action
public class HelloAction extends ActionSupport{ private static final long serialVersionUID = 1L; public String method1(){
String a = "method1";
System.out.println(a);
return SUCCESS;
}

public String method2(){
String b = "method2";
System.out.println(b);
return SUCCESS;
}
}
然后我通过URL访问:http://localhost/SSH2andJPA/hello!method1.action
可是报错:
HTTP Status 404 - There is no Action mapped for namespace / and action name hello.
There is no Action mapped for namespace / and action name hello. - [unknown location]说是没有映射"/"这个命名空间和Action"hello"不知道哪里出了问题。

解决方案 »

  1.   

    你发的东西没有问题,看看是不是配置文件放的位置不正确,或是配置文件名称写错了,检查一下这些细节地方。
    如果是tomcat部署的,检查一下编译路径下配置文件,action是否存在。
      

  2.   

    若楼主换成xml配置action的话 能否访问到 ?若能 那肯定是注解配置有问题 或者哪里配置
    不正确
      

  3.   

    你的@Action沒給名字,
    默認好想是类名,应该http://localhost/SSH2andJPA/HelloAction!method1.action
    要不h小写试试,
    要这样的话
    http://localhost/SSH2andJPA/hello!method1.action
    @Action(vlaue="hello")
      

  4.   

    谢谢大家,你们说的方式我都试了..还是不行.会不会和spring管理struts2有关系?
      

  5.   

    先试试把XML中的中文注释去掉。有些版本的容器不支持中文注释。
      

  6.   

    你直接使用struts零配置多好啊!struts.xml代码:
    <struts>
    <!-- 默认包 --> <constant name="struts.convention.default.parent.package" value="auth-default"/><!-- 如果不用注解默认包就这样 --> <constant name="struts.convention.default.parent.package" value="convention-default"/>
    <!-- 基于什么包 --> <constant name="struts.convention.package.locators.basePackage"value="这里写你自己acion路径比如:com.action"/>
    <!--确定搜索包的路径。只要是结尾为action的包都要搜索。basePackage按照默认不用配置,如果配置,只会找以此配置开头的包。locators及locators.basePackage都是一组以逗号分割的字符串。 --> <constant name="struts.convention.package.locators" value="action"/>
    <!--定于跳转后缀名--> <constant name="struts.action.extension" value="action,ajax,," /> <!--定于注解可要可不要 主要用来方便登陆判断-->
    package name="auth-default" extends="convention-default"> <interceptors>    <interceptor name="auth"class="com.me.code.modules.interceptor.AuthInterceptor"/>            <interceptor-stack name="authStack">            <interceptor-ref name="auth" />            <interceptor-ref name="paramsPrepareParamsStack" />         </interceptor-stack>  </interceptors>  <default-interceptor-ref name="authStack"/></package> </struts>
    wem.xml要注意这些:<filter-mapping>
    <filter-name>struts2Filter</filter-name>
    <url-pattern>*.action</url-pattern>
    <url-pattern>*.ajax</url-pattern>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.html</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    action配上这些就ok了:
    @Results({@Result(name = "success", location = "/ok.jsp") })
    另外要加载一个jar:struts2-convention-plugin-2.1.8.1.jar
      

  7.   

    http://localhost/SSH2andJPA/hello!method1.action
    你的id为hello这个action是在哪里定义的,没有看到你定义的位置,是在spring容器中吗?没定义的话,麻烦定义一下
      

  8.   

    你用了spring?
    那要加个@Controller
      

  9.   


    你好,我把Action的注解改了一下。还是不行:
    @Namespace("/")
    @ParentPackage("default")
    @Action(value="hello")public class HelloAction extends ActionSupport
      

  10.   

    这个是我的spring配置文件:
    <?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: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-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 快速注入配置 -->
    <context:annotation-config/>
    <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
    <context:component-scan base-package="com.z.test.dao"/>
    <context:component-scan base-package="com.z.test.service"/>

    <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath*:config/config.properties</value>
    </list>
    </property>
    </bean>
        
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
        
        <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean
                    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="database" value="MYSQL" />
                    <property name="showSql" value="true" />
                </bean>
            </property>
            <property name="persistenceXmlLocation" value="classpath:config/persistence.xml"></property>
        </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 数据库连接池启动时的初始值 -->
    <property name="initialSize" value="${jdbc.initialSize}" /> <!-- 连接池的最大值 -->
    <property name="maxActive" value="${jdbc.maxActive}" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
    <property name="maxIdle" value="${jdbc.maxIdle}" /> <!-- 最小空闲值,当空闲的连接数少于阀值时,连接池就会去申请一些连接,以免洪峰来时来不及申请 -->
    <property name="minIdle" value="${jdbc.minIdle}" /> <property name="maxWait" value="${jdbc.maxWait}" /> <property name="poolPreparedStatements" value="true" /> <property name="defaultAutoCommit" value="true" /> </bean>
        
        <bean id="transactionManager"
            class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
        
        <bean id="hello" scope="prototype" class="com.z.test.web.HelloAction">  
            <constructor-arg ref="myTestDao"/>  
       </bean>  
        
        
       <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
        
    </beans>
      

  11.   

    1、你把开发模式关闭掉试试 devMode 我记得这个有可能出BUG2、注解我没用过,先绑定,有空研究下= = 呵呵~~
      

  12.   

    http://localhost/hello!method1.action 地址改下
      

  13.   

    谢谢大家,问题已经解决。是我把struts.xml放错位置了....散分