当前位置: 代码迷 >> J2EE >> Strut2 + Jquery 数据传输到前台网页的有关问题
  详细解决方案

Strut2 + Jquery 数据传输到前台网页的有关问题

热度:479   发布时间:2016-04-22 01:30:06.0
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: -200px;    height: auto;    z-index: 100;    background-color: #fff;    border: 1px #8FA4F5 solid;    padding: 1px;}#DialogDiv h2 {    height: 25px;    font-size: 14px;    background-color: #8FA4F5;    position: relative;    padding-left: 10px;    line-height: 25px;}#DialogDiv h2 a {    position: absolute;    right: 5px;    font-size: 12px;    color: #000000}#DialogDiv .form {    padding: 10px;}#DialogDiv2 {    position: absolute;    width: 400px;    left: 50%;    top: 50%;    margin-left: -200px;    height: auto;    z-index: 100;    background-color: #fff;    border: 1px #8FA4F5 solid;    padding: 1px;}#DialogDiv2 h2 {    height: 25px;    font-size: 14px;    background-color: #8FA4F5;    position: relative;    padding-left: 10px;    line-height: 25px;}#DialogDiv2 h2 a {    position: absolute;    right: 5px;    font-size: 12px;    color: #000000}#DialogDiv2 .form {    padding: 10px;}#DialogDiv3 {    position: absolute;    width: 400px;    left: 50%;    top: 50%;    margin-left: -200px;    height: auto;    z-index: 100;    background-color: #fff;    border: 1px #8FA4F5 solid;    padding: 1px;}#DialogDiv3 h2 {    height: 25px;    font-size: 14px;    background-color: #8FA4F5;    position: relative;    padding-left: 10px;    line-height: 25px;}#DialogDiv3 h2 a {    position: absolute;    right: 5px;    font-size: 12px;    color: #000000}#DialogDiv3 .form {    padding: 10px;}</style>        <script language="javascript" type="text/javascript"            src="js/jquery-1.4.2.js"></script>        <script language="javascript" type="text/javascript">    function ShowDIV(thisObjID) {        var entity = {            "$userName" : val(),            "$userPassword" : val()        };        $.ajax({            url : 'test.action',            method : 'post',            data : entity,            success : function(data) {                $('#name').val(entity.userName);                $('#password').val(entity.userPassword);            },            error : function() {            }        });        $("#BgDiv").css({            display : "block",            height : $(document).height()        });        var yscroll = document.documentElement.scrollTop;        $("#" + thisObjID).css("top", "100px");        $("#" + thisObjID).css("display", "block");        document.documentElement.scrollTop = 0;    }    function closeDiv(thisObjID) {        $("#BgDiv").css("display", "none");        $("#" + thisObjID).css("display", "none");    }</script>    </head>    <body>        <div id="BgDiv"></div>        <!--遮罩层显示的DIV1-->        <div id="DialogDiv" style="display: none">            <h2>                弹出层                <a href="#" id="btnClose" onclick="closeDiv('DialogDiv')">关闭</a>            </h2>            <div class="form">                我是弹出对话框111111!!                <br>                <form action="test.method.action">                    <input type="text" name="entity.name" id="name">                    <br>                    <input type="text" name="entity.password" id="password">                    <br>                    <input type="submit">                </form>            </div>        </div>        <!--遮罩层显示的DIV2-->        <div id="DialogDiv2" style="display: none">            <h2>                弹出层                <a href="#" id="btnClose2" onclick="closeDiv('DialogDiv2')">关闭</a>            </h2>            <div class="form">                我是弹出对话框2222!!                <br>                <input type="text" id="name">            </div>        </div>        <!--遮罩层显示的DIV3-->        <div id="DialogDiv3" style="display: none">            <h2>                弹出层                <a href="#" id="btnClose3" onclick="closeDiv('DialogDiv3')">关闭</a>            </h2>            <div class="form">                我是弹出对话框3333333!!                <s:property value="name" />            </div>        </div>        <p>            <input value="弹出遮罩层1" type="button" id="btnShow1"                onclick="ShowDIV('DialogDiv')" />            <!--      <input value="弹出遮罩层2" type="button" id="btnShow2"                onclick="ShowDIV('DialogDiv2')" />               <input value="弹出遮罩层3" type="button" id="btnShow3"  onclick="ShowDIV('DialogDiv3')" />          <form action="testAction.action">            <input id="button" type="submit" >        </form>        <a href="#" onclick="ShowDIV('DialogDiv3')"> 打开对话框 </a> -->    </body></html>
  相关解决方案