首页 > 代码库 > struts2学习笔记(8)-------struts2的ajax支持

struts2学习笔记(8)-------struts2的ajax支持

     

          struts2支持一种stream类型的Result,这样的类型的Result能够直接向client浏览器响应二进制,文本等。

     我们能够再action里面生成文本响应,然后在client页面动态载入该响应就可以。

   直接看样例:

   

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="http://www.mamicode.com/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="http://www.mamicode.com/">
    
    <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="http://www.mamicode.com/styles.css">
	-->
	<script	type="text/javascript" src="http://www.mamicode.com/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>username:<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="http://www.mamicode.com/登录" onclick="login();" ></div>
  </body>
</html>

执行结果例如以下:

技术分享


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

struts2学习笔记(8)-------struts2的ajax支持