Ajax的流行带不仅带来客户的良好体验(无刷新效果),更重要的它作为把客户端与后台的勾通桥梁起到承上启下的作用,通过Js收集前台信息交给ajax,后台将处理结果就给ajax,再由js通过DHTML展示给前台
尝试Struts ajax结合使用的例子
处理流程1:点击提交,交有ajax组件处理(这里使用prototype1.4框架)
2:此Url通过get方式交给action处理(可能需要处理乱码)
3.分离参数交给业务处理,接受处理结果通过PrintWriter对象方法将返回的结果
交给ajax处理,这里的action的execute()返回null;而不是传统的返回到指定forward
4.ajax接受通过js展示给前台页面
具体代码:
//showpage.htm,前台页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<script language="JavaScript" src="prototype1_4.js"></script>
<style type="text/css">
<!--
#menu ul {
list-style:none;
margin:0px;
text-align:center;
line-height: 50px;
}
#menu {
position:absolute;
left:18px;
top:27px;
width:395px;
height:112px;
z-index:1;
}
-->
</style>
字串9
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<script language="JavaScript" src="prototype1_4.js"></script>
<style type="text/css">
<!--
#menu ul {
list-style:none;
margin:0px;
text-align:center;
line-height: 50px;
}
#menu {
position:absolute;
left:18px;
top:27px;
width:395px;
height:112px;
z-index:1;
}
-->
</style>
<body>
<script language="javascript" type="text/javascript">
function postAjax()
{
var myAjax;
var param="textfield=" $F("t1");
alert(param);
var options={
method:"get",
parameters:param,
onComplete:showResponse
};
myAjax=new Ajax.Request("getPageAction.do",options);
{
var myAjax;
var param="textfield=" $F("t1");
alert(param);
var options={
method:"get",
parameters:param,
onComplete:showResponse
字串9
};
myAjax=new Ajax.Request("getPageAction.do",options);
}
function showResponse(request)
{
alert(request.responseText);
$("show").innerHTML=request.responseText;
}
</script>
<div id="menu">
<form>
<ul>
<li>
<label>
输入:
</label>
<input type="text" id="t1" />
</li>
<li>
<label>
<input type="button" name="Submit" value="提交" onClick="postAjax();" />
</label>
</li>
</ul>
</form>
</div>
<div id="show"></div>
</body>
</html>
<div id="menu">
<form>
<ul>
<li>
<label>
输入:
</label>
<input type="text" id="t1" />
</li>
<li>
<label>
<input type="button" name="Submit" value="提交" onClick="postAjax();" />
</label>
</li>
</ul>
</form>
</div>
<div id="show"></div>
</body>
</html>
//ajaxAction.java
/*
* Template path: templates/java/JavaClass.vtl 字串6
*/
package wf;
import java.io.IOException;
import java.io.PrintWriter;
*/
package wf;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ajaxAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException {
String textfiled = new String(request.getParameter("textfield").getBytes("ISO8859-1")); //输入编码转换
System.out.print(textfiled);
response.setContentType("text/html"); //输出编码转换
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print("欢迎你" textfiled);
return null;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException {
String textfiled = new String(request.getParameter("textfield").getBytes("ISO8859-1")); //输入编码转换
System.out.print(textfiled);
response.setContentType("text/html"); //输出编码转换
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print("欢迎你" textfiled);
return null;
}
}
字串5
}
//Struts_config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

