1.struts中应用Ajax

这篇博客详细介绍了如何在Struts2框架下结合Ajax技术实现登录界面的交互功能。主要内容包括登录界面的HTML及jQuery脚本的编写,以及对应的Struts2 Action的配置和实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

登录界面及其jq的编写:

<%@ 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>My JSP 'MyJsp.jsp' starting page</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">
	-->
<script type="text/javascript" src="jquery/jquery-1.8.2.min.js"></script>
	<script type="text/javascript">
		
$(document).ready(function(){
		$(".asnyButton").click(function(){
			$.ajax({
				type:"POST",
				url:"ajax.action",
				data:"name=Join&location=Button",
				async:true,//使用异步交互数据
				success:function(msg){//成功后返回的界面
					alert(msg);
				},
				error:function(XMLHttpRequest,textStatus,errorThrown){
					alert(errorThrown);//若失败则打印相印的信息
				}
			});
		});
});
</script>
<style type="text/css">

button {
	width: 300px;
	height: 200px;
	position: absolute;
	left:35%;/*FF IE7*/
	top:50%;/*FF IE7*/
}

</style>
</head>

<body>
<button id="button" class="asnyButton">获取同步</button>
</body>
</html>

对应action的编写

package struct2;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import com.opensymphony.xwork2.Action;

public class Login implements Action {
	private String name;
	private String location;
	private InputStream inputStream;//返回给ajax的输入流
	
	public InputStream getResult() {
		return inputStream;
	}

	public void setResult(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	@Override
	public String execute() throws Exception {
		inputStream = new ByteArrayInputStream("Hello world".getBytes("UTF-8"));
		return SUCCESS;
	}

}

action的配置:

	 <action name="ajax" class="struct2.Login">  
           <result name="success" type="stream">
           		<param name="contentType">text/html</param>//类型
           		<param name="inputName">result</param>//参数
           </result>      
         </action> 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值