web.xml
<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 容器对于web.xml的加载过程是context-param、listener、filter、servlet-->
<!-- 配置Struts2的核心Filter -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 配置Struts2拦截的请求 /* 表示所有请求 -->
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 配置错误页面 -->
<error-page>
<error-code>500</error-code>
<location>/pages/login/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/pages/login/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/pages/login/error.jsp</location>
</error-page>
<!-- 配置Web应用的首页列表 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="user" extends="struts-default">
<action name="ajaxLogin" class="com.huawei.mun.AjaxAction" method="ajaxLogin">
<result>index.jsp</result>
</action>
</package>
</struts>
AjaxAction.java:
package com.huawei.mun;
import java.io.PrintWriter;
import java.net.URLDecoder;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class AjaxAction extends ActionSupport {
/** 处理AJAX请求:异步请求 **/
public String ajaxLogin() {
try {
// 读取请求的参数
HttpServletRequest request = ServletActionContext.getRequest();
String responseText = request.getParameter("responseText");
System.out.println("后台得到的数据:"+responseText);
responseText = "网页不刷新,我变了";
// 获取response对象
HttpServletResponse response = ServletActionContext.getResponse();
// 设置字符集
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
// 直接输出响应的内容
out.print(responseText);
//out.write(responseText);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;// 不需要跳转某个视图 因为上面已经有了直接输出的响应结果
}
}
index.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!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>123</title>
<script language="javascript">
//定义一个变量用于存放XMLHttpRequest对象
var xmlHttp;
//改函数用于创建一个XMLHttpRequest对象
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
//这是一个启动AJAX异步通信的方法
function ajaxLogin(){
var ln = document.getElementById("returnMes").value;
//创建一个XMLHttpRequest对象
createXMLHttpRequest();
//将状态绑定到一个函数
xmlHttp.onreadystatechange=processAjaxLogin;
//通过GET方法向指定的URL建立服务器的调用
var url="ajaxLogin.action?responseText="+encodeURI(ln);
xmlHttp.open("GET",url,true);
//发送请求
xmlHttp.send(null);
}
//这是一个用来处理状态改变的函数
function processAjaxLogin(){
//定义一个变量用于存放 从服务器返回的响应结果
var responseContext="";
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
responseContext = xmlHttp.responseText;
document.getElementById("returnMes").value=responseContext;
}
}
}
</script>
</head>
<body>
<input type="text" id="returnMes">
<input type="button" value="异步请求" οnclick="ajaxLogin();">
<table>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
</table>
</body>
</html>