本人新手,弱弱的问,还请各位哥哥姐姐不要拍我。
用javascript和struts1 进行数据传输的时候,用request,resopnse等即可,返回时在response里write进json格式即可;但是我现在被要求用struts2,struts2的数据存放在valuestack中,jsp传来的数据用implements ModelDriven操作也很简单,但是前台现在依然是html+css+js(接受时也还是只接受json格式数据),这时Struts2当然也可以用ServletActionContext中的个方法来做,若是用ModelDriven来接受和返回的话,小弟就迷糊了,请教各位给予指导,或直接解答,或告诉小弟去哪儿学习也行,不胜感激。
等待中。

解决方案 »

  1.   

    OGNL表达式可以取出valuestack中存放的值
      

  2.   

    你是说让JS编写的时候用OGNL表示所需要的值?
      

  3.   


    ...
    <script type="text/javascript" src="js/mouse_active.js"></script>
    <script type="text/javascript" src="js/tools_active.js"></script>
    .....
    <td>
    <span class="redtext">05</span><span class="textall">,</span><span
    class="redtext">06</span><span class="textall">,</span><span
    class="redtext">17</span><span class="textall">,</span><span
    class="redtext">13</span><span class="textall">,</span><span
    class="redtext">15</span><span class="textall">,</span><span
    class="redtext">26</span><span class="textall">|</span><span
    class="coldtext">14</span>
    </td>
    ....
    <div id="smallson" style="position: absolute; display: none; background-color: #fff; border: 1px #000 solid">
    <form name="the_form">
    ....
    <input type="button" value="查询" onClick="getData();" />
    <br />
    查询结果:
    <br />
    距离上次出现的间隔期数:
    <span id="lastTime"></span>
    <br />
    该范围内出现次数:
    <span id="showTimes"></span>
    </form>
    </div>
    ....
    String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
    }function getData() {
    the_ball_num = $("input[name='the_number']").val();
    // alert(the_ball_num);
    if ($("[name='the_color']:checked").val() == "0") {
    if (the_ball_num <= 0 || the_ball_num > 33) {
    alert("This is a wrong number");
    return;
    }
    // alert(the_ball_num);
    } else if ($("[name='the_color']:checked").val() == "1") {
    if (the_ball_num <= 0 || the_ball_num > 16) {
    alert("This is a wrong number");
    return;
    }
    the_ball_num = "#" + the_ball_num;
    }
    // alert(the_ball_num);
    the_ball_scope = $("#scope_result").html();
    // alert(the_ball_scope);
    var start = $("#start").val();
    // alert(start+" "+the_ball_scope+" "+ the_ball_num);
    $.getJSON("lotteryTips.action?callback=?", {
    num : the_ball_num,
    start : start,
    lotteryid : "1",
    scope : the_ball_scope
    }, function(json) {
    $("#lastTime").html(json.lastTime);
    $("#showTimes").html(json.showTimes);
    });
    }$(document).ready(
    function() {
    $(".redtext,.coldtext").hover(function(e) {
    var tooltip = $("#smallson");// 获取显示层
    Mouse(e);// 计算显示层的位置
    tooltip.css( {
    top : toppos,
    left : leftpos
    });// 给显示层定义CSS--当前计算出的位置
    tooltip.fadeTo(200, 1);// 显示效果
    // $("#smallson").show("slow");
    var num = $(this).html();
    var snum = "";
    if ($(this).attr("class") == "redtext") {
    $("input[type='radio'][name='the_color']").eq(0).attr(
    "checked", true);
    snum = snum + num;
    snum = snum.trim();
    // $("the_color").val(0);
    } else {
    snum = "#" + num.trim();
    $("input[type='radio'][name='the_color']").eq(1).attr(
    "checked", true);
    // $("the_color").val(1);
    }
    $("#the_number").val(num);
    $("#scope_result").html("30");
    var start = $($(this).parent().parent()).children().html()
    .trim();
    $("#start").val(start);
    // alert(start);
    $.getJSON("lotteryTips.action?callback=?", {
    num : num,
    start : start,
    lotteryid : "1",
    scope : "30"
    }, function(json) {
    $("#lastTime").html(json.lastTime);
    $("#showTimes").html(json.showTimes);
    });
    }, function() {
    }); $("#smallson").hover(function() {
    }, function() {
    $("#smallson")[0].style.display = "none";
    }); });var divoffset = -2; // 显示层离鼠标的距离
    // 计算坐标函数
    var Mouse = function(e) {
    mouse = new MouseEvent(e);
    leftpos = mouse.x + divoffset;
    toppos = mouse.y + divoffset;
    }
    // 获取鼠标坐标函数
    var MouseEvent = function(e) {
    this.x = e.pageX
    this.y = e.pageY
    }$(document).ready(function() {
    $(".dropdown img.flag").addClass("flagvisibility");
    $(".dropdown dt a").click(function() {
    $(".dropdown dd ul").toggle();
    });
    $(".dropdown dd ul li a").click(function() {
    var text = $(this).html();
    $(".dropdown dt a span").html(text);
    $(".dropdown dd ul").hide();
    $("#scope_result").html(getSelectedValue("sample"));
    // alert(getSelectedValue("sample"));
    });
    function getSelectedValue(id) {
    return $("#" + id).find("dt a span.value").html();
    }
    $(document).bind('click', function(e) {
    var $clicked = $(e.target);
    if (!$clicked.parents().hasClass("dropdown"))
    $(".dropdown dd ul").hide();
    });
    $("#flagSwitcher").click(function() {
    $(".dropdown img.flag").toggleClass("flagvisibility");
    });
    });
    package com.vikool.lottery.controller.action;import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    import com.vikool.lottery.controller.action.vo.LotteryTipsVo;
    import com.vikool.lottery.controller.services.LotteryPhaseinfoManager;@SuppressWarnings("serial")
    public class LotteryTipsAction extends ActionSupport implements ModelDriven<LotteryTipsVo>{ private LotteryTipsVo ltVo = new LotteryTipsVo();
    public LotteryTipsVo getModel() {
    return ltVo;
    } private LotteryPhaseinfoManager lotteryService;
    //private String callback = ltVo.getCallback();
    private String num = ltVo.getNum();
    private String start = ltVo.getStart();

        /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
        public String execute(){
         //LotteryPhaseinfo lp = lotteryService.getLotteryPhaseinfo(44);

    Integer scope = Integer.parseInt(ltVo.getScope());
    Integer lotteryid = Integer.parseInt(ltVo.getLotteryid());

    //System.out.println("callback:"+ callback+" start: "+start+" scope: "+sscope+" num: "+num+" lotteryid: "+slotteryid);
    ltVo.setShowTimes(lotteryService.getTimesofOccurrences(num, start, scope, lotteryid)); //最近n期内出现次数
    ltVo.setLastTime(lotteryService.getTimesofLose(num, start, scope, lotteryid)); //最近n期内距离上次出现间隔期数

    System.out.println("需要显示的数字是:"+num);
    System.out.println("出现次数:"+ ltVo.getShowTimes()+" 间隔期数"+ ltVo.getLastTime());
    /*
    PrintWriter pwrite = response.getWriter();
    pwrite.write(callback+"({showTimes:"+showTimes+",lastTime:"+lastTime+"});"); //调用客户端jsonP 函数*/

         return SUCCESS;    
        } public void setLotteryService(LotteryPhaseinfoManager lotteryService) {
    this.lotteryService = lotteryService;
    }

    }
      

  4.   

    这个action是我想要的形式,之前的用法是用struts1:
    public class LotteryTipsAction extends Action {
    ......
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    //LotteryPhaseinfo lp = lotteryService.getLotteryPhaseinfo(44);

    String callback = request.getParameter("hjhjkhjkhkjhk");
    ..........
    PrintWriter pwrite = response.getWriter();
    pwrite.write(callback+"({showTimes:"+showTimes+",lastTime:"+lastTime+"});");
    }
    ...
    }
    需要改动些什么地方呢?如果不改JS可以吗?
      

  5.   

    ModelDriven接收不到什么数据?你需要在js中得到什么?
      

  6.   

    呵呵,怪我没说清楚,我刚请教了一下高手,有所收获:
    js中的“$.getJSON("lotteryTips.action?callback=?", {”
    所体现的是“get"方法相当于用URL进行传数据,而ModelDriven所能接收的是“form”也就是“post”方法所传递的数据,所以我上边写的ModelDriven得不到数据,也即action 得不到数据。
    不过还是要谢谢“zhangao0086 ”。