如何将action中的值传给jsp页面啊,我现在想在action中定义一个num变量,值为1,传给jsp页面,但不知为何在jsp页面输出num却是0我是这样定义的:package com.cec.document;import com.opensymphony.xwork2.ActionContext;public class DOCLoginAction {
private int num;
public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
} @SuppressWarnings("unchecked")
public String execute() throws Exception{
ActionContext.getContext().getSession().put("num",1);
return "success";
}
}
在jsp页面,输出却为0<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="com.opensymphony.xwork2.util.*"%><html>
<head>
<title>公文列表界面</title>
</head>
<body>
<s:form action="DOCLogin" theme="simple">
<%
ValueStack vs = (ValueStack)request.getAttribute("struts.valueStack");
int num = (Integer)vs.findValue("num");
%>
<table width="100%">
<tr>
<td width="50%"><%=num%></td>
</tr> </table>
</s:form>
</body>
</html>
private int num;
public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
} @SuppressWarnings("unchecked")
public String execute() throws Exception{
ActionContext.getContext().getSession().put("num",1);
return "success";
}
}
在jsp页面,输出却为0<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="com.opensymphony.xwork2.util.*"%><html>
<head>
<title>公文列表界面</title>
</head>
<body>
<s:form action="DOCLogin" theme="simple">
<%
ValueStack vs = (ValueStack)request.getAttribute("struts.valueStack");
int num = (Integer)vs.findValue("num");
%>
<table width="100%">
<tr>
<td width="50%"><%=num%></td>
</tr> </table>
</s:form>
</body>
</html>
为什么不放在request中呢?这样在jsp中不就直接可以用request获得了吗?
2>.jsp显示,是显示request中的num。解决:<s:property value="#session.num" />
ActionContext.getContext().getSession().put("num",1);页面接收 ${sessionScope.num}
<s:iterator value="result" status="index">
怎么写能实现让num每次显示的值加1啊,就是说比如result数组长度为3,那num依次显示1,2,3
int num = (Integer)vs.findValue("num");
这个获得的是你的action里的成员变量“num”的值,所以是0
因为action是采取的转发FindFowward()方法
<s:property value="#index.index" />
</s:iterator>
当然不一样啊。
struts2里的session是一个Map。