您的位置:首页 > 编程语言 > Java开发

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 配置过滤器

<?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整合



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: