日期:2014-05-16  浏览次数:20783 次

struts2整合ajaxanywhere例子

1、首先配置struts2、这个是必须的。我来简单的把配置和部分代码粘贴上来吧:


web.xml配置(我这里用的struts2.0+的,是懒得去下载2.1了,如果你是用2.1把你的过滤器改下其他类就好了):

[java] view plaincopy
<display-name>Struts Showcase Application</display-name> 
<filter> 
    <filter-name>struts-cleanup</filter-name> 
    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> 
</filter> 
 
<filter> 
    <filter-name>AjaxAnywhere</filter-name> 
    <filter-class>org.ajaxanywhere.AAFilter</filter-class> 
</filter> 
 
<filter-mapping> 
    <filter-name>AjaxAnywhere</filter-name> 
    <url-pattern>*.jsp</url-pattern> 
</filter-mapping> 
<filter-mapping> 
    <filter-name>AjaxAnywhere</filter-name> 
    <url-pattern>*.action</url-pattern> 
</filter-mapping> 
 
 
<filter> 
    <filter-name>struts</filter-name> 
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
    <init-param> 
        <param-name>actionPackages</param-name> 
        <param-value>org.apache.struts2.showcase.person</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>struts</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 


注意上面的配置、我已经把ajaxanywhere放上了,记得把ajaxanywhere的过滤器放在所以过滤器的最前面、因为这些过滤链中,我想一开始就先用它过滤、还有就是struts2的过滤器要改到合适你的版本哦。


2、我们接下来简单的配置下struts.xml

[java] view plaincopy
<?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> 
    <package name="test" extends="struts-default"> 
        <action name="test" class="com.test.TestAction"> 
            <result name="success">/index.jsp</result> 
        </action> 
    </package> 
</struts>     


这是我简单的配置了struts.xml文件


3、接下来就是我们的action类了

[java] view plaincopy
package com.test; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
public class TestAction extends ActionSupport { 
    private static final long serialVersionUID = -479427231759775671L; 
    private String name; 
    private String outName; 
 
    @Override 
    public String execute() throws Exception { 
        return super.execute(); 
    } 
 
    public String getNameValue() { 
        this.setOutName(this.getName(