struts2學習筆記(8)-------struts2的ajax支援

來源:互聯網
上載者:User

標籤: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>

然後使用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">--><scripttype="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,這個也比較常用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.