sum可以是你当action继程的父类属性,也可能是某对象的属性

解决方案 »

  1.   

    你这里的sum不是属性只是你在action中取的一个参数的别名而已
      

  2.   

    肯定定义过的 不是在action中就是在jsp中 不然怎么可能凭空出现这么个单词
      

  3.   


    但是我extends ActionSupport,我没有看到在哪里有这个sum属性啊。请其他人再解答一下
      

  4.   

    代码全部在URL中,能否指出是哪个参数的别名?
      

  5.   

    你把你的Action类和jsp代码全贴上来好么 不然真看不懂
      

  6.   


    package action;
    import com.opensymphony.xwork2.ActionSupport;
    public class FirstAction extends ActionSupport
    {
        private int operand1;
        private int operand2;
        public String execute() throws Exception
        {
            if (getSum() >= 0)  // 如果代码数和是非负整数,跳到positive.jsp页面
            {
                return "positive";
            }
            else  // 如果代码数和是负整数,跳到negative.jsp页面
            {
                return "negative";
            }
        }
        public int getOperand1()
        {
            return operand1;
        }
        public void setOperand1(int operand1)
        {
            System.out.println(operand1);
              this.operand1 = operand1;
        }
        public int getOperand2()
        {
            return operand2;
        }  
        public void setOperand2(int operand2)
        {
            System.out.println(operand2);
            this.operand2 = operand2;
        }
        public int getSum()
        {
            return operand1 + operand2;  // 计算两个整数的代码数和
        }
    }
    本文来自编程入门网:http://www.bianceng.cn/Programming/Java/201107/27511_2.htm
    sum.jsp<%@ page language="java" import="java.util.*" pageEncoding="GBK" %>
      <%@ taglib prefix="s" uri="/struts-tags"%>
      <html>
          <head>
              <title>输入操作数</title>
          </head>
          <body>
               求代数和
              <br/>
              <s:form action="mystruts/sum.action" >
                  <s:textfield name="operand1" label=" 操作数1"/>
                  <s:textfield name="operand2"  label=" 操作数2" />
                  <s:submit value="代数和" />
              </s:form>
          </body>
      </html>
    本文来自编程入门网:http://www.bianceng.cn/Programming/Java/201107/27511_4.htm
      

  7.   

    2.positive.jsp<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
      <%@ taglib prefix="s" uri="/struts-tags" %>
      <html>
        <head>
          <title>显示代数和</title>
        </head>
        <body>
          代数和为非负整数<h1><s:property value="sum" /></h1>
        </body>
      </html>
    本文来自编程入门网:http://www.bianceng.cn/Programming/Java/201107/27511_4.htm
      

  8.   

    <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
      <struts>
          <package name="struts2" namespace="/mystruts"
              extends="struts-default">
              <action name="sum" class="action.FirstAction">
                  <result name="positive">/positive.jsp</result>
                  <result name="negative">/negative.jsp</result>
              </action>
          </package>
      </struts>
    本文来自编程入门网:http://www.bianceng.cn/Programming/Java/201107/27511_3.htm
      

  9.   

    因为你有getSum()这个方法,其实struts2的标签<s:property value="XXX"/>就是调用getXXX方法来实现的,我也是偶然看到这个帖子的,但我不保证对的,具体怎么样最好去看struts2<s:property value=""/>标签的源码
      

  10.   


    应该不是你说的getxxx得到的,仔细研究了一下: public String execute() throws Exception {
       
    if (getSum() >= 0) // 如果代码数和是非负整数,跳到positive.jsp页面
    {
     request = ServletActionContext.getRequest();
     com.opensymphony.xwork2.util.OgnlValueStack ovs =
     (com.opensymphony.xwork2.util.OgnlValueStack)request.getAttribute("struts.valueStack");
     double dblSum= Double.parseDouble(ovs.findString("sum"));

     response = ServletActionContext.getResponse();
     
    return "positive";
    } else // 如果代码数和是负整数,跳到negative.jsp页面
    {
    return "negative";
    }
    }发现是可以找到这个sum的字符串。但是我也不知道是什么原理了
    一个高人的解释,参考(http://terryjs.iteye.com/blog/767699)
    ValueStack 对象,该对象封装了 Action 全部的输出信息。该对象是 Struts 2 使用的一个 ValueStack对象,可以通过 OGNL 表达式非常方便的访问该对象封装的信息。      ValueStack 有点类似于 Map 结构,但它比 Map 结构更加强大(因为它可以根据表达式来查询值)。Action 所以的属性都被封装到了 ValueStack 对象中,Action 中的属性名可以理解为 ValueStack 中 value 的名字 
      

  11.   

    15楼的说法是正确的,是因为你的action类中有getNum()方法,
      

  12.   

    具体就是Action把所有的属性封装到了ValueStack 对象,但是为什么sum我们没有定义在action类中,确有这个属性呢?我也不知道了,不知道后面有人能回答否????
      

  13.   


    我不理解的是getSum()就为何能对应sum啊?
      

  14.   


    我找到答案了,我看了李刚老师的书(struct2权威指南2-p93页):
    action类中可以不包含属性,因为系统是通过对应的getter和setter方法来处理请求参数的,而不是通过属性名处理请求参数的。即此sum类中有否sum属性没有关系。重要的是有一个getsum()的方法。就是这个道理。