在struts中,通过Ajax,利用jQuery,将action中的数据转到前台页面

第一步、配置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、通过网页就可以测试该程序了,作为一个初学者,写的也是基础,希望能到需要的人



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值