struts2的ajax支持

struts2支持一种stream类型的Result,这种类型的Result可以直接向客户端浏览器响应二进制,文本等,

     我们可以再action里面生成文本响应,然后在客户端页面动态加载该响应即可。

   直接看例子:

   

public class LoginAction {
	private String user;
	private String pass;
	private InputStream inputStream;
	
	public InputStream getInput(){
		return inputStream;
	}
	
	
	public String getUser() {
		return user;
	}



	public void setUser(String user) {
		this.user = user;
	}



	public String getPass() {
		return pass;
	}



	public void setPass(String pass) {
		this.pass = pass;
	}



	public String login() throws UnsupportedEncodingException{
		inputStream = user.equals("zhangsan")&&pass.equals("123")?
				new ByteArrayInputStream("恭喜你,登录成功".getBytes("utf-8")):
					new ByteArrayInputStream("登录失败".getBytes("utf-8"));
		return "success";		
	}
}

这个Action的核心主要是getInput方法,它返回的二进制流将直接输出给浏览者,这将使用stream类型的result来完成,注意:这个getInput方法名可以随便换,但必须是这种类型get+"XXX"(应该是反射机制所用吧),这个“XXX”必须跟param为inputName的值对应!!!!

接下来看struts.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
  <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
  	<package name="user" extends="struts-default">
		
		<action name="loginAction" class="com.sinosoft.epicc.action.LoginAction" >
			<result name="success" type="stream">
				<param name="contentType">text/html</param>
				<param name="inputName">input</param>
			</result>
			
		</action>
</package>
</struts>

然后使用jquery封装的ajax调用:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 
<%
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 ‘login.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="js/jquery-1.7.1.js"></script>
	<script type="text/javascript">
		function login(){
		
			var userName = $("#user").val();
			var password = $("#pwd").val();
			$.ajax({
			
				url : ‘loginAction!login‘,
				data : {
					user:userName,
					pass:password
					},
				type : ‘post‘,
				success : function(data){
				
					$("#label").html(data);
				},
				error : function(){}
			
			});	
		
		}
	</script>
  </head>
  
  <body>
	<div>用户名:<input type="text" name="userName" id="user"/><span id="label"></span></div>
	<div>密码:<input type="password" name="userPwd" id="pwd"></div>
	<div><input type="button" value="登录" onclick="login();" ></div>
  </body>
</html>

运行结果如下:

技术分享

比较简单~~~~还用过一种ajax返回的方法,是直接response.write,这个也比较常用

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。