标签:des style blog http java 使用
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>
<%@ 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,这个也比较常用
struts2学习笔记(8)-------struts2的ajax支持,布布扣,bubuko.com
struts2学习笔记(8)-------struts2的ajax支持
标签:des style blog http java 使用
原文地址:http://blog.csdn.net/jihaixiao8/article/details/37929027