使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——ajax与struts2的交互

来源:互联网 发布:柏拉图表软件 编辑:程序博客网 时间:2024/05/19 18:10


struts所需jar包。



web.xml加上strut2过滤器。

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.     <display-name>ZeroToOne</display-name>  
  7.     <!-- <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file>   
  8.         <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file>   
  9.         <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file>   
  10.         </welcome-file-list> -->  
  11.     <filter>  
  12.         <filter-name>struts2</filter-name>  
  13.         <filter-class>  
  14.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  15.         </filter-class>  
  16.     </filter>  
  17.   
  18.     <filter-mapping>  
  19.         <filter-name>struts2</filter-name>  
  20.         <url-pattern>/* </url-pattern>  
  21.     </filter-mapping>  
  22.     <servlet>  
  23.         <servlet-name>Hello</servlet-name>  
  24.         <servlet-class>Servlet.HelloServlet</servlet-class>  
  25.     </servlet>  
  26.     <servlet-mapping>  
  27.         <servlet-name>Hello</servlet-name>  
  28.         <url-pattern>/helloServlet</url-pattern>  
  29.     </servlet-mapping>  
  30. </web-app>  

根目录添加struts.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding"UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.      "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5. <struts>  
  6.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  7.     <constant name="struts.devMode" value="true" />  
  8.     <constant name="struts.locale" value="zh_CN" />  
  9.     <constant name="struts.i18n.encoding" value="utf-8" />  
  10.     <package name="default" namespace="/" extends="struts-default">  
  11.         <default-action-ref name="index" />  
  12.         <global-results>  
  13.             <result name="error">/error.jsp </result>  
  14.             <result name="success">/hello.jsp </result>  
  15.         </global-results>  
  16.     <global-exception-mappings>  
  17.         <exception-mapping exception="java.lang.Exception"  
  18.             result="error" />  
  19.     </global-exception-mappings>   
  20.   
  21.         <action name="ajaxaction" class="action.AjaxAction" method="ajax">  
  22.             <result name="success">/hello.jsp</result>  
  23.         </action>  
  24.     </package>  
  25.   
  26.     <!-- <include file="example.xml" /> -->  
  27.     <!-- Add packages here -->  
  28.   
  29. </struts>  


新建AjaxAction实现Struts2的Action接口。像第一篇文章那样直接用PrintWriter直接输出,
return Success.

AjaxAction类

[java] view plaincopy
  1. public class AjaxAction implements Action{  
  2.     public String ajax() throws Exception {  
  3.         System.out.println("action execute");  
  4.         ServletActionContext.getResponse().getWriter().println("struts data response to ajax");  
  5.         return SUCCESS;  
  6.     }  
  7.     @Override  
  8.     public String execute() throws Exception {  
  9.         return null;  
  10.     }  
  11. }  

由于struts过滤器的存在,ajax.html点击ajax按钮的时候,请求ajaxaction,命名空间为/,找到namespace为/的
package标签。

根据struts.xml的配置,找到action标签中name为ajaxaction的。请求action.AjaxAction的ajax方法。

执行完之后result为success,跳转hello.jsp页面。


hello.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. <h3>the response do not response to ajax</h3>  
  11. </body>  
  12. </html>  


执行过程中会报:java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
解决:project-》properties-》Deployment Assembly-》add-》java build path entries,build path是加载到class path里面,tomcat在刚启动的时候,filter需要经过struts,
而本身的web-inf/lib没有jar包所以报类不在的错误。所以需要手动复制进lib目录或者用上面的方法。

然后我们就能得到像ajax与Servlet交互那样的结果,得到PrintWriter所输出的字符串。


但是结果却没有拿到ServletActionContext.getResponse().getWriter().println("struts data response to ajax")输出的字符串。

xmlhttp.responseText拿到的却是hello.jsp的内容。


那我们就需要让struts2访问此方法后不跳转到hello.jsp。找了下有Action.NONE这个值。

在struts的官方guides中:https://struts.apache.org/docs/result-configuration.html

有它的说明,Returning ActionSupport.NONE (or null) from an Action class method causes the results processing to be skipped. This is useful if the action fully handles the result processing such as writing directly to the HttpServletResponse OutputStream.


从Action类中返回ActionSupport.NONE(或者null)可以让result的处理省略,当你想完全控制result处理,像通过HttpServletResponse OutputStream直接输出的时候很有用。

是的,我们就是要这样用。

将AjaxAction的返回语句改为:

[java] view plaincopy
  1. ServletActionContext.getResponse().getWriter().println("struts data response to ajax");  
  2.         return Action.NONE;  
去除struts.xml中的:

[html] view plaincopy
  1. <result name="success">/hello.jsp</result>  

最后让struts与ajax交互。


代码下载:http://download.csdn.net/detail/iaiti/9118327

代码

除了用none返回外,其实也可以正常使用success这些普通的字符串,当然结果就像前面说的一样跳转到特定页面。

但是换一下思路,将我们想要输出的东西放到这个页面也是可以的。

如果将Action类里面的东西放到指定页面呢,可以通过ActionContext将特定内容放入,再从页面使用Request.getAttribute将这个特定内容的值提取出来。

再用out.write输出,也是可以。

struts所需jar包。



web.xml加上strut2过滤器。

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.     <display-name>ZeroToOne</display-name>  
  7.     <!-- <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file>   
  8.         <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file>   
  9.         <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file>   
  10.         </welcome-file-list> -->  
  11.     <filter>  
  12.         <filter-name>struts2</filter-name>  
  13.         <filter-class>  
  14.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  15.         </filter-class>  
  16.     </filter>  
  17.   
  18.     <filter-mapping>  
  19.         <filter-name>struts2</filter-name>  
  20.         <url-pattern>/* </url-pattern>  
  21.     </filter-mapping>  
  22.     <servlet>  
  23.         <servlet-name>Hello</servlet-name>  
  24.         <servlet-class>Servlet.HelloServlet</servlet-class>  
  25.     </servlet>  
  26.     <servlet-mapping>  
  27.         <servlet-name>Hello</servlet-name>  
  28.         <url-pattern>/helloServlet</url-pattern>  
  29.     </servlet-mapping>  
  30. </web-app>  

根目录添加struts.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding"UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.      "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5. <struts>  
  6.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  7.     <constant name="struts.devMode" value="true" />  
  8.     <constant name="struts.locale" value="zh_CN" />  
  9.     <constant name="struts.i18n.encoding" value="utf-8" />  
  10.     <package name="default" namespace="/" extends="struts-default">  
  11.         <default-action-ref name="index" />  
  12.         <global-results>  
  13.             <result name="error">/error.jsp </result>  
  14.             <result name="success">/hello.jsp </result>  
  15.         </global-results>  
  16.     <global-exception-mappings>  
  17.         <exception-mapping exception="java.lang.Exception"  
  18.             result="error" />  
  19.     </global-exception-mappings>   
  20.   
  21.         <action name="ajaxaction" class="action.AjaxAction" method="ajax">  
  22.             <result name="success">/hello.jsp</result>  
  23.         </action>  
  24.     </package>  
  25.   
  26.     <!-- <include file="example.xml" /> -->  
  27.     <!-- Add packages here -->  
  28.   
  29. </struts>  


新建AjaxAction实现Struts2的Action接口。像第一篇文章那样直接用PrintWriter直接输出,
return Success.

AjaxAction类

[java] view plaincopy
  1. public class AjaxAction implements Action{  
  2.     public String ajax() throws Exception {  
  3.         System.out.println("action execute");  
  4.         ServletActionContext.getResponse().getWriter().println("struts data response to ajax");  
  5.         return SUCCESS;  
  6.     }  
  7.     @Override  
  8.     public String execute() throws Exception {  
  9.         return null;  
  10.     }  
  11. }  

由于struts过滤器的存在,ajax.html点击ajax按钮的时候,请求ajaxaction,命名空间为/,找到namespace为/的
package标签。

根据struts.xml的配置,找到action标签中name为ajaxaction的。请求action.AjaxAction的ajax方法。

执行完之后result为success,跳转hello.jsp页面。


hello.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. <h3>the response do not response to ajax</h3>  
  11. </body>  
  12. </html>  


执行过程中会报:java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
解决:project-》properties-》Deployment Assembly-》add-》java build path entries,build path是加载到class path里面,tomcat在刚启动的时候,filter需要经过struts,
而本身的web-inf/lib没有jar包所以报类不在的错误。所以需要手动复制进lib目录或者用上面的方法。

然后我们就能得到像ajax与Servlet交互那样的结果,得到PrintWriter所输出的字符串。


但是结果却没有拿到ServletActionContext.getResponse().getWriter().println("struts data response to ajax")输出的字符串。

xmlhttp.responseText拿到的却是hello.jsp的内容。


那我们就需要让struts2访问此方法后不跳转到hello.jsp。找了下有Action.NONE这个值。

在struts的官方guides中:https://struts.apache.org/docs/result-configuration.html

有它的说明,Returning ActionSupport.NONE (or null) from an Action class method causes the results processing to be skipped. This is useful if the action fully handles the result processing such as writing directly to the HttpServletResponse OutputStream.


从Action类中返回ActionSupport.NONE(或者null)可以让result的处理省略,当你想完全控制result处理,像通过HttpServletResponse OutputStream直接输出的时候很有用。

是的,我们就是要这样用。

将AjaxAction的返回语句改为:

[java] view plaincopy
  1. ServletActionContext.getResponse().getWriter().println("struts data response to ajax");  
  2.         return Action.NONE;  

去除struts.xml中的:

[html] view plaincopy
  1. <result name="success">/hello.jsp</result>  

最后让struts与ajax交互。


代码下载:http://download.csdn.net/detail/iaiti/9118327

代码


除了用none返回外,其实也可以正常使用success这些普通的字符串,当然结果就像前面说的一样跳转到特定页面。

但是换一下思路,将我们想要输出的东西放到这个页面也是可以的。

如果将Action类里面的东西放到指定页面呢,可以通过ActionContext将特定内容放入,再从页面使用Request.getAttribute将这个特定内容的值提取出来。

再用out.write输出,也是可以。

0 0
原创粉丝点击