吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:struts2stream类实现Ajax

寵の児 提交于 2020-02-26 15:13:40
<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
    <property name="dist" value="classes"/>
    <property name="src" value="src"/>
    
    <path id="classpath">
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${dist}"/>
    </path>

    <target name="compile" description="Compile all source code">
        <delete dir="${dist}"/>
        <mkdir dir="${dist}"/>
        <copy todir="${dist}">
            <fileset dir="${src}">
                <exclude name="**/*.java"/>
            </fileset>        
        </copy>
        <javac destdir="classes" debug="true" includeantruntime="yes"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${src}"/>
            <classpath refid="classpath"/>
        </javac>
    </target>

</project>
<?xml version="1.0" encoding="GBK"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">


    <!-- 定义Struts 2的FilterDispathcer的Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用stream结果实现Ajax</title>
    <script src="${pageContext.request.contextPath}/jquery-1.11.1.js" 
    type="text/javascript">
    </script>
</head>
<body>
<s:form id="loginForm">
    <s:textfield name="user" label="用户名"/>
    <s:textfield name="pass" label="密码"/>
    <tr><td colspan="2">
    <input id="loginBn" type="button" value="提交"/>
    </td></tr>
</s:form>
<div id="show" style="display:none;">
</div>
<script type="text/javascript">
    // 为id为loginBn的按钮绑定事件处理函数
    $("#loginBn").click(function()
    {
        $("#show").hide();
        // 指定向login发送请求,以id为loginForm表单里各表单控件作为请求参数
        $.get("login" , $("#loginForm").serializeArray() , 
            // 指定回调函数
            function(data , statusText)
            {
                $("#show").height(80)
                    .width(240)
                    .css("border" , "1px solid black")
                    .css("border-radius" , "10px")
                    .css("background-color" , "#efef99")
                    .css("color" , "#ff0000")
                    .css("padding" , "20px")
                    .empty();
                $("#show").append("登录结果:" + data + "<br />");
                $("#show").show(2000);
            },
            // 指定服务器响应为html
            "html");
    });
</script>
</body>
</html>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <package name="lee"  extends="struts-default">
        <action name="login" class="org.crazyit.app.action.LoginAction">
            <result type="stream">
                <!-- 指定下载文件的文件类型 -->
                <param name="contentType">text/html</param>
                <!-- 指定由getResult()方法返回输出结果的InputStream -->
                <param name="inputName">result</param>
            </result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
</struts>
package org.crazyit.app.action;

import com.opensymphony.xwork2.Action;

import java.io.*;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class LoginAction implements Action
{
    // 封装请求参数的两个成员变量
    private String user;
    private String pass;
    // 封装输出结果的二进制流
    private InputStream inputStream;

    // user的setter和getter方法
    public void setUser(String user)
    {
        this.user = user;
    }
    public String getUser()
    {
        return this.user;
    }

    // pass的setter和getter方法
    public void setPass(String pass)
    {
        this.pass = pass;
    }
    public String getPass()
    {
        return this.pass;
    }

    public InputStream getResult()
    {
        return inputStream;
    }

    public String execute()
        throws Exception
    {
        // 判断用户名、密码,生成对应的响应
        inputStream = user.equals("crazyit.org") && pass.equals("leegang")
            ? new ByteArrayInputStream("恭喜你,登录成功!"
                .getBytes("UTF-8"))
            : new ByteArrayInputStream("对不起,用户名、密码不匹配!"
                .getBytes("UTF-8"));
        return SUCCESS;
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!