日期:2014-05-19  浏览次数:20703 次

Strut2 + Jquery 数据传输到前台网页的问题
我把我的配置和代码贴出来,大家看看,有什么问题。

Strut2的Action
Java code

package com.struts2.action.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

public class TestAction {
    private Entity entity = new Entity();

    public TestAction() {

    }

    public String execute() {
        entity.setName("显示信息OK!");
        entity.setPassword("Helllo");
        // HttpServletResponse response = ServletActionContext.getResponse();
        System.out.println("执行了Action");
        Map map = new HashMap();
        map.put("name", entity.getName());
        map.put("password", entity.getPassword());

        JSONObject jsonObject = JSONObject.fromObject(map);

        System.out.println(jsonObject.get("name"));
        System.out.println(jsonObject.get("password"));
        return "SUCCESS";
    }

    // try {
    // response.setCharacterEncoding("utf-8");
    // PrintWriter out = response.getWriter();
    // out.print(entity);
    // } catch (IOException e) {
    // e.printStackTrace();
    // }
    public Entity getEntity() {
        return entity;
    }

    public void setEntity(Entity entity) {
        this.entity = entity;
    }
}

class Entity {
    private String name;
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    // ////////////////////
    public static void main(String[] args) {
        new TestAction().getSName();
    }
}




struts.xml配置文件
XML code

<struts>
    <package name="struts2" extends="json-default">
        <action name="test" class="com.struts2.action.test.TestAction">
            <result type="json">
                <param name="OK">
                    jsonObject
                </param>
            </result>
        </action>
    </package>
</struts>    




JSP页面
HTML code

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
    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>
        <title>Jquery遮罩层</title>

        <style type="text/css">
#BgDiv {
    background-color: #e3e3e3;
    position: absolute;
    z-index: 99;
    left: 0;
    top: 0;
    display: none;
    width: 100%;
    height: 1000px;
    opacity: 0.5;
    filter: alpha(opacity =                 50);
    -moz-opacity: 0.5;
}

#DialogDiv {
    position: absolute;
    width: 400px;
    left: 50%;
    top: 50%;
    margin-left: -200