日期:2014-05-17  浏览次数:20825 次

struts2 零配置 注解方式
今天想尝试下Struts2的注解方式

在web.xml里修改了原来的Filter 
<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>manning.chapterTwo</param-value>
</init-param>
</filter>

然后在包manning.chapterTwo下面添加一个Action,代码如下:

Java code

package manning.chapterTwo;

import org.apache.struts2.convention.annotation.Result;


import com.opensymphony.xwork2.ActionSupport;





@Result(name="success",location="login.jsp")

public class AnnotationAction extends ActionSupport {
     
    /**
     * 
     */
    private static final long serialVersionUID = 2296720831644414741L;
    public String execute()
    {
        
        return "success";
    }

}






启动,访问http://127.0.0.1:8888/Struts/annotationAction.action Struts是工程名

提示说找不到action


是我少配什么东西了么?

------解决方案--------------------
我还是有配置文件的,只是少了action的节点
XML code
<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
    
    <struts>
    
    
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.custom.i18n.resources" value="messages" />
    <!-- 配置默认的Action -->
         
        <package name="default" namespace="/" extends="struts-default">
            
            <interceptors>
            <!-- 定义权限验证拦截器 -->
            <interceptor name="AuthInterceptor" class="cn.jbit.votingsystem.interceptor.AuthInterceptor"/>
            
            
            
            <!-- 自定义连接器栈-->    
            <interceptor-stack name="myStack">
                <interceptor-ref name="defaultStack"/>
                <interceptor-ref name="AuthInterceptor"/>
                
            </interceptor-stack>
            </interceptors>
            
            <!-- 使用拦截器栈 -->
           <default-interceptor-ref name="myStack"/>
           
           <global-results>
                <result name="error" >/WEB-INF/page/error.jsp</result>
                <result name="login">/WEB-INF/page/login.jsp</result>
            </global-results>
            
            
            
            
            
        </package>
        
    </struts>

------解决方案--------------------
你的ACTION访问路径错了,应该是
http://127.0.0.1:8888/Struts/annotation.action

约定的规则是:如果是已Action结尾要去掉Action,以大小写为单词分隔,以“-”符号分割。
我的项目里有个ACTION的命名是WsThreadInfoAction,访问它的链接就是ws-thread-info.action