struts2整合ajaxanywhere

很少写博客,最近发现大家都很兴趣整合struts2和ajaxanywhere都出现问题.下面我来记录下我配置并成功运行的经验:

 

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

 

 

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

 

<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

 

<?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类了

 

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()); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOutName() { return outName; } public void setOutName(String outName) { this.outName = outName; } }

 

 

这段代码很简单、其中一个值就是页面传过来的、另外一个值是我从传过来的值赋给它、最后在页面展示;

 

 

4、接下来就是jsp页面了

 

 

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib prefix="aa" uri="http://ajaxanywhere.sourceforge.net/"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <mce:script type="text/javascript" src="js/aa.js" mce_src="js/aa.js"></mce:script> </head> <body> <form action="test!getNameValue.action" method="post"> <input type="text" name="name" /><br /> <!-- <input type="button" onclick="submitTest();" value="submit"><br /> --> <input type="submit"/><br/> <aa:zone name="refleshZone"> ${outName }<br /> </aa:zone> </form> </body> <mce:script type="text/javascript"><!-- function submitTest() { ajaxAnywhere.submitAJAX(); } ajaxAnywhere.getZonesToReload = function() { return "refleshZone"; } // --></mce:script> </html>

 

注意:记得在下载的ajaxanywhere包里面取出aa.js到你的项目中来。

 

这段代码我又两个部分让你看到用ajaxanywhere提交和直接提交的效果。如果你用的直接提交(因为第一个值我没有hidden也没有用struts2的标签)提交后第一个值就不保存了、如果你用ajaxanywhere提交则会存在、因为我没有刷新它、呵呵。到这里应该例子差不多了

 

 

注:转载必须声明出处

 

 

 

你可能感兴趣的:(exception,String,struts,input,action,encoding)