struts2 和ajax整合使用案例(原生态的ajax操作 和 struts2和ajax整合操作)
2017-10-27 09:06
495 查看
struts2和ajax整合使用案例(原生态的ajax操作 和 struts2和ajax整合操作)
struts2使用ajax的方式有两种:
原生态的ajax操作(不需要整合包) 和 struts2和ajax整合操作(需要整合包)
下面将演示两种方式:
struts2项目中需要的使用的包:
两种方式都需要使用jquery.js文件,整合方式的还需要json.js文件
方式一(原生态的ajax操作(不需要整合包)):
示例代码:
1、web.xml 配置过滤器
2、StrutsAndAjaxAction.java 处理请求Action类
3、主配置文件struts.xml
4、请求页面 ajax.jsp
完成代码后将项目部署到服务器,访问 http://localhost:8080/Struts202/ajax.jsp ;接着按下图操作,即实现了原生态struts2使用ajax
方式二 (struts2和ajax整合操作(需要整合包)):
示例代码:
1、web.xml 配置过滤器
2、StrutsAndAjaxAction.java 处理请求Action类
3、主配置文件struts.xml
注:在struts和ajax整合时需要package继承 json-default
4、请求页面 ajax.jsp
完成代码后将项目部署到服务器,访问 http://localhost:8080/Struts202/ajax.jsp ;接着按下图操作,即实现了struts2和ajax整合
struts2使用ajax的方式有两种:
原生态的ajax操作(不需要整合包) 和 struts2和ajax整合操作(需要整合包)
下面将演示两种方式:
struts2项目中需要的使用的包:
两种方式都需要使用jquery.js文件,整合方式的还需要json.js文件
方式一(原生态的ajax操作(不需要整合包)):
示例代码:
1、web.xml 配置过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Struts202</display-name> <!--配置过滤器 --> <filter> <filter-name>front</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>front</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <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> </web-app>
2、StrutsAndAjaxAction.java 处理请求Action类
package cn.sz.action; import java.io.IOException; import java.io.PrintWriter; import org.apache.struts2.ServletActionContext; public class StrutsAndAjaxAction { // 使用普通属性传值的方式结束页面参数过来的参数,这里的属性名和页面的变量名一致 private String name; private String pwd; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String login() throws IOException { // 输出接收的信息 System.out.println(name + "," + pwd); // 测试原生态的struts和ajax操作 String str = "{\"name\":\"fuwuqi\",\"pwd\":\"123\"}"; PrintWriter out = ServletActionContext.getResponse().getWriter(); out.write(str); out.flush(); out.close(); return "main"; } }
3、主配置文件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> <package name="sa" extends="struts-default" namespace="/sa"> <action name="sa" class="cn.sz.action.StrutsAndAjaxAction" method="login"> </action> </package> </struts>
4、请求页面 ajax.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <script type="text/javascript" src="js/jquery-2.0.0.js"></script> <script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript"> $(function() { $("#bu1").click(function() { $.ajax({ url : "sa/sa.action", type : "get", data : { name : "zhsan", pwd : "123" }, success : function(data) { var p = eval("(" + data + ")"); $("#s").text(p.name + "," + p.pwd); }, error : function(data) { alert(data); } }); }); }); </script> <title></title> </head> <body> <button id="bu1">原生态的ajax操作</button> <br /> 请求响应内容: <span id="s"></span> </body> </html>
完成代码后将项目部署到服务器,访问 http://localhost:8080/Struts202/ajax.jsp ;接着按下图操作,即实现了原生态struts2使用ajax
方式二 (struts2和ajax整合操作(需要整合包)):
示例代码:
1、web.xml 配置过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Struts202</display-name> <!--配置过滤器 --> <filter> <filter-name>front</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>front</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <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> </web-app>
2、StrutsAndAjaxAction.java 处理请求Action类
package cn.sz.action; import java.io.IOException; import java.io.PrintWriter; import org.apache.struts2.ServletActionContext; public class StrutsAndAjaxAction { // 使用普通属性传值的方式结束页面参数过来的参数,这里的属性名和页面的变量名一致 private String name; private String pwd; // 添加一个属性,用于返回响应结果 private String result; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String login() throws IOException { // 输出接收的信息 System.out.println(name + "," + pwd); 测试struts和ajax整合的方式 String str1 = "{\"name\":\"struts2\",\"pwd\":\"1230\"}"; result = str1; return "main"; } }
3、主配置文件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> <package name="sa" extends="json-default" namespace="/sa"> <action name="sa" class="cn.sz.action.StrutsAndAjaxAction" method="login"> <result name="main" type="json"> <param name="root">result</param> </result> </action> </package> </struts>
注:在struts和ajax整合时需要package继承 json-default
4、请求页面 ajax.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <script type="text/javascript" src="js/jquery-2.0.0.js"></script> <script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript"> $(function() { $("#bu2").click(function() { $.ajax({ url : "sa/sa.action", type : "get", data : { name : "lisi", pwd : "123" }, success : function(data) { var p = JSON.parse(data); $("#s").text(p.name + "," + p.pwd); }, error : function(data) { alert(data); } }); }); }); </script> <title></title> </head> <body> <button id="bu2">struts2和ajax整合操作</button> <br /> 请求响应内容: <span id="s"></span> </body> </html>
完成代码后将项目部署到服务器,访问 http://localhost:8080/Struts202/ajax.jsp ;接着按下图操作,即实现了struts2和ajax整合
相关文章推荐
- Struts2 使用案例(操作流程)
- struts2中Ajax的操作实现及其核心对象XMLHttpRequest对象的使用
- easyui的datagrid整合struts2,以及Ajax,实现局部刷新功能,并设置分页的实现---------投票案例
- J2EE下使用AJAX(五) jsonplugin -- struts2下的AJAX插件
- J2EE下使用AJAX:jsonplugin -- struts2下的AJAX插件
- 使用DWR进行反向Ajax操作的例子
- 使用Jquery 来AJAX操作!
- 使用AJAX控件来实现Ajax操作(支持服务器事件)
- JSON,Struts2,Spring,Hibernate,Extjs整合应用案例
- 刷新、前进、后退、离开页面,使用ajax对数据库进行操作
- struts2使用JSON插件开发Ajax
- struts2 整合 json struts2 使用 json
- Sitemesh整合struts2使用多个装饰器(非/*)的BUG修正
- Web框架——小编浅谈Struts2中使用JSON插件实现Ajax
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第五篇:MVC整合Ajax
- 常用的Ajax异步操作范例,使用Ajax验证用户名是否已存在
- 快速、简便的使用AJAX技术操作的三部曲
- JSP中使用AJAX示例:分别将结果提交为STRUTS,SERVLET,WEBWORK的不同操作方式
- Struts2,AJAX,json-plugin使用
- asp.net ajax 使用updatepanel进行操作后使用脚本