sum可以是你当action继程的父类属性,也可能是某对象的属性
解决方案 »
- SSH 搭建完之后,出现上述情况
- 财付通:请问如何通过<meta name="TENCENT_ONLINE_PAYMENT" content="China TENCENT">告诉财付通处理成功?
- 存储过程!
- 请问Struts Action中如何直接调用Response.redirect()?
- jsp当中el表达式不起作用!
- 关于session的奇怪问题!!!
- web service基本问题
- 我的为什么会这样?的问题继续!哥哥到这儿看!
- 不是吧,Jbuilder里面居然没有timer控件??
- 很简单的问题!
- 用过OAuth(weibo4j)的进来看看帮忙解决点小问题
- 菜鸟问个SSH整合时候遇到的一个问题。
但是我extends ActionSupport,我没有看到在哪里有这个sum属性啊。请其他人再解答一下
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
<%@ 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
<!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
应该不是你说的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 的名字
我不理解的是getSum()就为何能对应sum啊?
我找到答案了,我看了李刚老师的书(struct2权威指南2-p93页):
action类中可以不包含属性,因为系统是通过对应的getter和setter方法来处理请求参数的,而不是通过属性名处理请求参数的。即此sum类中有否sum属性没有关系。重要的是有一个getsum()的方法。就是这个道理。