struts2系列2:Struts2 Action(一)

Posted on 2010-06-30 03:11 java小爬虫 阅读(2865) 评论(2)  编辑  收藏

  什么是Struts2 Action呢?

  实现了
  public String execute() throws Exception;
  的任何一个java类都可以被认为是Struts2 Action。

  注意点:
    
     1:方法名默认为execute,也可以自定义方法名。
    
     2:返回值应该为:success,none,error,input和login;

  Struts2 Action的实现方法?(三种)

  1:
public class IndexAction1 {
    
public String execute(){
        
return "success";
    }

}

  2:
import com.opensymphony.xwork2.Action;

public class IndexAction2 implements Action{
    
public String execute(){
        
return SUCCESS;
    }
    
}

  3:
import com.opensymphony.xwork2.ActionSupport;

public class IndexAction3 extends ActionSupport {
    
private static final long serialVersionUID = 1L;
    @Override
    
public String execute(){
        
return ERROR;
    }

}

请参考:com.opensymphony.xwork2.Action接口,com.opensymphony.xwork2.ActionSupport类。
        Action接口定义了Struts2 Action应该实现的方法和5个字符串常量作为方法的返回值。
        ActionSupport类是Struts2提供的一个具有基本实现的父类,方便用户的使用。



视图层的跳转

在struts-core jar包中打开struts-default.xml文件,它是一个struts2的默认配置文件。在里面可以找到:

  <result-types>
            
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
            
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
            
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
 
</result-types>

它说明了向视图层跳转时有10种方式:

    默认方式为dispatcher,不写时就代表默认方式;
    redirectAction:Struts2 Action的跳转;
    redirect: 与response.sendRedirect()相同;
    dispatcher:与 

RequestDispatcher    forward(ServletRequest request, ServletResponse response)相同;



Struts2 Action方法的调用(三种):

1:在<action />中没有定义method属性,表示调用当前Action的execute();
<action name="index3" class="com.action.IndexAction3">
            
<result name="error">
                
/hello3.jsp
            
</result>
        
</action>
2: Wildcards
    As an application grows in size, so will the number of action mappings. Wildcards can be used to combine similar mappings into one more generic mapping.

  
 <action name="index1*" class="com.action.IndexAction1" method="{1}" >
            
<result name="success" type="redirectAction" >
            index3
            
</result>
        
</action>
method中的1代表name中的第一个*;
   
3:Dynamic Method Invocation 

    Dynamic Method Invocation (DMI) will use the string following a "!" character in an action name as the name of a method to invoke (instead of execute). A reference to "Category!create.action", says to use the "Category" action mapping, but call the create method instead.
 <action name="index2!*" class="com.action.IndexAction2" method="{1}">
            
<result name="success">
                
/hello2.jsp
            
</result>
        
</action>
method中的1代表name中的第一个*;


实例:
package com.action;

public class IndexAction1 {
    
public String msg(){
        
return "success";
    }

}

package com.action;

import com.opensymphony.xwork2.Action;

public class IndexAction2 implements Action{
    
public String execute(){
        
return SUCCESS;
    }
    
public String succ(){
        
return SUCCESS;
    }
    
}
 
package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class IndexAction3 extends ActionSupport {
    @Override
    
public String execute(){
        
        
return ERROR;
    }

}
<?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="false" />

    
<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>
     
-->
    
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
    
<constant name="struts.devMode" value="true" />
    
<package name="default" namespace="/" extends="struts-default">
        
<action name="index1*" class="com.action.IndexAction1" method="{1}" >
            
<result name="success" type="redirectAction" >
            index3
            
</result>
        
</action>
        
<action name="index2!*" class="com.action.IndexAction2" method="{1}">
            
<result name="success">
                
/hello2.jsp
            
</result>
        
</action>
        
<action name="index3" class="com.action.IndexAction3">
            
<result name="error">
                
/hello3.jsp
            
</result>
        
</action>
    
</package>
    
<!-- Add packages here -->

</struts>
 
测试:
分别用以下URL,注意观察和思考:
http://localhost:8080/struts2_0200_action/index3
http://localhost:8080/struts2_0200_action/index2!succ
http://localhost:8080/struts2_0200_action/index2
http://localhost:8080/struts2_0200_action/index1msg

注意:
<result name="error">
    
/hello3.jsp
</result>
name代表Action方法的返回值,
缺省代表success
type代表Action向视图跳转时的跳转类型,缺省代表plainText



Feedback

# re: struts2系列2:Struts2 Action(一)  回复  更多评论   

2010-06-30 07:39 by 侬本多情
java旅程

# re: struts2系列2:Struts2 Action(一)  回复  更多评论   

2010-06-30 09:19 by @joe
建议写点大家使用中有问题的东西或者struts性能方面的东西。 只是建议

只有注册用户登录后才能发表评论。


网站导航: