首页 > 代码库 > Struts2对ajax的支持
Struts2对ajax的支持
使用stream类型的result实现ajax
package com.lee.action; import java.io.ByteArrayInputStream; import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; public class AJAXLoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private String password; private InputStream inputStream; public String execute() throws Exception { inputStream = "lizhe".equals(username) && "lz123456".equals(password) ? new ByteArrayInputStream( "成功 success!".getBytes("GBK")) : new ByteArrayInputStream("失败 fail!".getBytes("GBK")); System.out.println(inputStream); return SUCCESS; } /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the inputStream */ public InputStream getInputStream() { return inputStream; } /** * @param inputStream the inputStream to set */ public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } }
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <script src="${pageContext.request.contextPath }/jquery/jquery-1.6.4.min.js" type="text/javascript"></script> <title><s:text name="loginPage"></s:text></title> </head> <body> <form action="loginPro" id="loginForm"> <s:textfield name="username" label="用户名" /> <s:password name="password" label="密码" /> <input id="loginBtn" type="button" value="提交" /> </form> <div id="showMsg" style="display: none;"></div> <script type="text/javascript"> $("#loginBtn").click( function() { $ .get("loginPro", $("#loginForm").serializeArray(), function(data, statusText) { $("#showMsg").text(""); $("#showMsg").append( "登陆结果: " + data + "<br/>"); $("#showMsg").show(2000); }, "html"); }); </script> </body> </html>
<action name="loginPro" class="com.lee.action.AJAXLoginAction"> <result name="success" type="stream"> <param name="contentType">text/html</param> <param name="inputName">inputStream</param> </result> <result name="input">/WEB-INF/content/ajaxLogin.jsp</result> </action>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。