第一步、配置Struts(注意,返回的数据类型为json数据,)
<?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>
<package name="json" extends="json-default" namespace="/">
<action name="UserTestAction" class="com.aisino.development.UserTestAction" method="Test">
<result name="success" type="json">
<param name="root">name</param> //默认name为root
</result>
</action>
</package>
</struts>
第二步、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
<span style="white-space:pre"> </span>xmlns="http://java.sun.com/xml/ns/javaee"
<span style="white-space:pre"> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre"> </span>xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
<span style="white-space:pre"> </span>http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<span style="white-space:pre"> </span>
<filter> <!--配置struts2过滤器 -->
<span style="white-space:pre"> </span><!--过滤器名称 -->
<span style="white-space:pre"> </span><filter-name>struts2</filter-name>
<span style="white-space:pre"> </span><!--过滤器类 -->
<span style="white-space:pre"> </span><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<span style="white-space:pre"> </span><filter-name>struts2</filter-name> <!--过滤器名称 -->
<span style="white-space:pre"> </span><url-pattern>/*</url-pattern> <!--过滤器映射 -->
</filter-mapping>
<welcome-file-list>
<welcome-file>Test.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、第三步、配置jQuery
在WebRoot下面新建一个JS文件夹,将jQuery库复制到其中:jquery-1.8.3.min.js
4、写jsp文件,
注意配置jQuery语句
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>jQuery测试</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
//配置jQuery语句
<span style="color:#ff6666;"> </span><span style="font-size:18px;color:#ff0000;"><script src="js/jquery-1.8.3.min.js"></script></span>
<script type="text/javascript">
//写一个函数
<span style="font-size:18px;color:#ff0000;">function test(){
$.ajax(
{</span>
<span style="font-size:18px;color:#ff0000;"> url:"/Login_Struts_demo1/UserTestAction", //url地址,action地址
type:"get", //get/post两种类型,一般用get
dataType:"json", //返回前台的数据类型
success:function(data) //访问成功后执行弹窗,data为访问的url地址中action返回的数据
{
alert(data);
}
}
);
}</span>
</script>
<body>
<center>
<input type="button" value="jQuery" οnclick="test()">
</center>
</body>
</html>
5、第五步,写Action
package com.aisino.development;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class UserTestAction extends ActionSupport {
public String Test(){
String name="张三";
return SUCCESS;
}
}
6、通过网页就可以测试该程序了,作为一个初学者,写的也是基础,希望能到需要的人