码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2对ajax的支持

时间:2014-05-05 11:51:57      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   ext   

使用stream类型的result实现ajax

bubuko.com,布布扣
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;
    }

}
bubuko.com,布布扣
bubuko.com,布布扣
<%@ 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>
bubuko.com,布布扣
bubuko.com,布布扣
        <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>
bubuko.com,布布扣

 

Struts2对ajax的支持,布布扣,bubuko.com

Struts2对ajax的支持

标签:style   class   blog   code   java   ext   

原文地址:http://www.cnblogs.com/harryV/p/3706084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!