使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——ajax与struts2的交互
来源:互联网 发布:大数据对贵州的影响 编辑:程序博客网 时间:2024/05/19 17:23
写博客的时候,我在想,是不是以后也有人来问我我向别人提过的问题。认识了一些人之后,在技术还有其他的一些问题上的疑问,在有经验的人身上得到一些启示。然后将自己的东西写出来之后,又有人来问我,这是一件很奇妙的事情。我曾经受过别人帮助,当别人有求的时候,我也希望能把自己会的说给他人听。
继上篇:《使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互》之后,让ajax与Servlet进行交互。接下来是与struts2的结合。
struts的相关知识不多讲,没学的补上即可。
struts所需jar包。
web.xml加上strut2过滤器。
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><display-name>ZeroToOne</display-name><!-- <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/* </url-pattern></filter-mapping><servlet><servlet-name>Hello</servlet-name><servlet-class>Servlet.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>Hello</servlet-name><url-pattern>/helloServlet</url-pattern></servlet-mapping></web-app>
根目录添加struts.xml
<?xml version="1.0" encoding= "UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts><constant name="struts.enable.DynamicMethodInvocation" value="false" /><constant name="struts.devMode" value="true" /><constant name="struts.locale" value="zh_CN" /><constant name="struts.i18n.encoding" value="utf-8" /><package name="default" namespace="/" extends="struts-default"><default-action-ref name="index" /><global-results><result name="error">/error.jsp </result><result name="success">/hello.jsp </result></global-results><global-exception-mappings><exception-mapping exception="java.lang.Exception"result="error" /></global-exception-mappings> <action name="ajaxaction" class="action.AjaxAction" method="ajax"><result name="success">/hello.jsp</result></action></package><!-- <include file="example.xml" /> --><!-- Add packages here --></struts>
新建AjaxAction实现Struts2的Action接口。像第一篇文章那样直接用PrintWriter直接输出,
return Success.
AjaxAction类
public class AjaxAction implements Action{public String ajax() throws Exception {System.out.println("action execute");ServletActionContext.getResponse().getWriter().println("struts data response to ajax");return SUCCESS;}@Overridepublic String execute() throws Exception {return null;}}
由于struts过滤器的存在,ajax.html点击ajax按钮的时候,请求ajaxaction,命名空间为/,找到namespace为/的
package标签。
根据struts.xml的配置,找到action标签中name为ajaxaction的。请求action.AjaxAction的ajax方法。
执行完之后result为success,跳转hello.jsp页面。
hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!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></head><body><h3>the response do not response to ajax</h3></body></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的返回语句改为:
ServletActionContext.getResponse().getWriter().println("struts data response to ajax");return Action.NONE;
去除struts.xml中的:
<result name="success">/hello.jsp</result>
最后让struts与ajax交互。
代码下载:http://download.csdn.net/detail/iaiti/9118327
代码
除了用none返回外,其实也可以正常使用success这些普通的字符串,当然结果就像前面说的一样跳转到特定页面。
但是换一下思路,将我们想要输出的东西放到这个页面也是可以的。
如果将Action类里面的东西放到指定页面呢,可以通过ActionContext将特定内容放入,再从页面使用Request.getAttribute将这个特定内容的值提取出来。
再用out.write输出,也是可以。
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——ajax与struts2的交互
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——ajax与struts2的交互
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(三)——ajax,struts2使用json格式的交互
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(三)——ajax,struts2使用json格式的交互
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(八)——Struts与EasyUI使用JSON进行交互
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(八)——Struts与EasyUI使用JSON进行交互
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(五)——jsp,json,EasyUI的结合
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(五)——jsp,json,EasyUI的结合
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(四)——基础环境搭建
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(七)——数据分页处理
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(四)——基础环境搭建
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(七)——数据分页处理
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(六)——复选框进行多选删除操作
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(六)——复选框进行多选删除操作
- Struts2学习笔记(二)使用jQuery—Ajax技术的简单Demo
- struts2 easyui实现datagrid的crud
- opengl 和direct3d坐标系的区别
- oracle 数据导入导出
- Java和C++程序在编译和运行上有什么区别
- python 偏函数解析
- 《构建之法》第三章 软件工程师的成长
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——ajax与struts2的交互
- 分组算法
- 按行获取TextView的内容
- 解密得出PIID Value产生的Unknown wire-type 6异常
- JAVA HttpURLConnection Post方式提交传递参数
- ImageCenterButton
- mysql数据库授权所有人
- Struts2-简单了解
- Ubuntu15.04 下编译Android4.2.2的问题总结