使用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>
Struts2+JSON
jar包:struts2-json-plugin-2.1.8.1.jar
<%@ 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"> <title><s:text name="showNamePage"></s:text></title> <script src="${pageContext.request.contextPath }/jquery/jquery-1.6.4.min.js" type="text/javascript"></script> <script type="text/javascript"> function toSubmit() { var url = 'showName.action'; var data = $("form").serialize(); $.ajax({ url : url, dataType : 'html', data : data, onComplete : processResponse, asynchronous : true }); } function processResponse(request) { var evs = request.responseText.evalJSON(); for ( var propName in evs) { $("show").innerHTML += propName + "-->" + evs[propName] + "<br/>"; } } </script> </head> <body> <s:form method="post"> <s:textfield name="username" key="username" /> <s:textfield name="age" key="age" /> <s:textfield name="sex" key="sex" /> </s:form> <input type="button" id="" value="提交" onclick="toSubmit();" /> <div id="show"></div> </body> </html>
package com.lee.action; import java.util.HashMap; import java.util.Map; import org.apache.struts2.json.annotations.JSON; import com.opensymphony.xwork2.ActionSupport; public class ShowNameAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private String age; private String sex; private Map<String, String> map = new HashMap<String, String>(); public String execute() { map.put("username", username); map.put("age", age); map.put("sex", sex); return SUCCESS; } @JSON(name = "newName") public Map<String, String> getMap() { return this.map; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
<package name="example" extends="json-default"> <action name="showName" class="com.lee.action.ShowNameAction"> <result type="json"> <param name="noCacha">true</param> <param name="contentType">text/html</param> </result> </action> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package>