哪位大哥帮下忙,我用struts2 在category_edit.jsp页面指定了一个action(strut.xml中没有将此action的结果和此页面category_edit.jsp绑定,而且此页面category_edit.jsp在strut.xml中和其他action进行了绑定),怎么能在category_edit.jsp中获取到这个action的值,并进行输出呢?struts.xml<struts>
<constant name="struts.i18n.encoding" value="gbk"></constant> <package name="b2cshop" extends="struts-default" namespace="/back/html">
<global-results>
<!-- 下面定义的结果对所有的Action都有效 -->
<result name="success">/back/html/category_list.jsp</result>
</global-results>
<action name="login" class="loginAction" method="checkAdminUser">
<result name="success" type="redirectAction">goodCatalog.action</result>
<result name="input">/back/html/home.jsp</result>
</action>
<action name="goodCatalog" class="goodCatalogAction">
<result name="success">/back/html/category_list.jsp</result>
<result name="input">/back/html/home.jsp</result>
</action>
<action name="updateGoodCatalog" class="UpdateGoodCatalogAction">
<result name="success">/back/html/category_edit.jsp</result>
<result name="input" type="redirectAction">goodCatalog.action</result>
</action>
</package>
</struts> category_edit.jsp<select>
<option value="">请选择</option>
<s:action name="goodCatalog"></s:action>
<s:iterator id="goodcatalogItem" value="goodcatalogList">
?
<s:if test="#goodcatalogItem.gcCatalogLevel==1">
<option value="<s:property value='%{#goodcatalogItem.gcId}'/>"><s:property value="%{#goodcatalogItem.gcCatalogName}"/></option>
</s:if>
</s:iterator>
<option value="tsyx">图书音像</option>
<option value="sssh">时尚生活</option>
<option value="smcp">数码产品</option>
<option value="xhsd">鲜花速递</option>
</select>
UpdateGoodCatalogAction.javapublic class UpdateGoodCatalogAction extends ActionSupport {
private Goodcatalog goodcatalog;
private GoodcatalogService goodcatalogService;
private Integer categoryId; public Integer getCategoryId() {
return categoryId;
} public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
} public Goodcatalog getGoodcatalog() {
return goodcatalog;
} public void setGoodcatalog(Goodcatalog goodcatalog) {
this.goodcatalog = goodcatalog;
} public GoodcatalogService getGoodcatalogService() {
return goodcatalogService;
} public void setGoodcatalogService(GoodcatalogService goodcatalogService) {
this.goodcatalogService = goodcatalogService;
} @Override
public String execute() throws Exception {
setGoodcatalog(goodcatalogService.getGoodcatalogBygcId(getCategoryId()));
System.out.println(getCategoryId());
return SUCCESS;
}
}
GoodCatalogAction.javapublic class GoodCatalogAction extends ActionSupport {
private List<Goodcatalog> goodcatalogList;
private GoodcatalogService goodcatalogService; public List<Goodcatalog> getGoodcatalogList() {
return goodcatalogList;
} public void setGoodcatalogList(List<Goodcatalog> goodcatalogList) {
this.goodcatalogList = goodcatalogList;
} public GoodcatalogService getGoodcatalogService() {
return goodcatalogService;
} public void setGoodcatalogService(GoodcatalogService goodcatalogService) {
this.goodcatalogService = goodcatalogService;
} public String execute() throws Exception {
setGoodcatalogList(goodcatalogService.getGoodCatalogList());
System.out.println(123);
return SUCCESS; }
}
<constant name="struts.i18n.encoding" value="gbk"></constant> <package name="b2cshop" extends="struts-default" namespace="/back/html">
<global-results>
<!-- 下面定义的结果对所有的Action都有效 -->
<result name="success">/back/html/category_list.jsp</result>
</global-results>
<action name="login" class="loginAction" method="checkAdminUser">
<result name="success" type="redirectAction">goodCatalog.action</result>
<result name="input">/back/html/home.jsp</result>
</action>
<action name="goodCatalog" class="goodCatalogAction">
<result name="success">/back/html/category_list.jsp</result>
<result name="input">/back/html/home.jsp</result>
</action>
<action name="updateGoodCatalog" class="UpdateGoodCatalogAction">
<result name="success">/back/html/category_edit.jsp</result>
<result name="input" type="redirectAction">goodCatalog.action</result>
</action>
</package>
</struts> category_edit.jsp<select>
<option value="">请选择</option>
<s:action name="goodCatalog"></s:action>
<s:iterator id="goodcatalogItem" value="goodcatalogList">
?
<s:if test="#goodcatalogItem.gcCatalogLevel==1">
<option value="<s:property value='%{#goodcatalogItem.gcId}'/>"><s:property value="%{#goodcatalogItem.gcCatalogName}"/></option>
</s:if>
</s:iterator>
<option value="tsyx">图书音像</option>
<option value="sssh">时尚生活</option>
<option value="smcp">数码产品</option>
<option value="xhsd">鲜花速递</option>
</select>
UpdateGoodCatalogAction.javapublic class UpdateGoodCatalogAction extends ActionSupport {
private Goodcatalog goodcatalog;
private GoodcatalogService goodcatalogService;
private Integer categoryId; public Integer getCategoryId() {
return categoryId;
} public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
} public Goodcatalog getGoodcatalog() {
return goodcatalog;
} public void setGoodcatalog(Goodcatalog goodcatalog) {
this.goodcatalog = goodcatalog;
} public GoodcatalogService getGoodcatalogService() {
return goodcatalogService;
} public void setGoodcatalogService(GoodcatalogService goodcatalogService) {
this.goodcatalogService = goodcatalogService;
} @Override
public String execute() throws Exception {
setGoodcatalog(goodcatalogService.getGoodcatalogBygcId(getCategoryId()));
System.out.println(getCategoryId());
return SUCCESS;
}
}
GoodCatalogAction.javapublic class GoodCatalogAction extends ActionSupport {
private List<Goodcatalog> goodcatalogList;
private GoodcatalogService goodcatalogService; public List<Goodcatalog> getGoodcatalogList() {
return goodcatalogList;
} public void setGoodcatalogList(List<Goodcatalog> goodcatalogList) {
this.goodcatalogList = goodcatalogList;
} public GoodcatalogService getGoodcatalogService() {
return goodcatalogService;
} public void setGoodcatalogService(GoodcatalogService goodcatalogService) {
this.goodcatalogService = goodcatalogService;
} public String execute() throws Exception {
setGoodcatalogList(goodcatalogService.getGoodCatalogList());
System.out.println(123);
return SUCCESS; }
}
意思是想用同一個form 提交到不同的action處理么?
从struts2来看,如果结合你现在直接调用action来讲,页面响应流程如下:
当程序运行到 <s:action name="goodCatalog"></s:action>时,程序将分析struts.xml找到相应的处理类,你的struts.xml中有以下语句:
<action name="goodCatalog" class="goodCatalogAction">
<result name="success">/back/html/category_list.jsp</result>
<result name="input">/back/html/home.jsp</result>
</action>
说明,此时,进入到goodCatalogAction类中处理,又从你的这个类看出,你未指定特定的方法,那么将会执行默认的execute()方法,所以最后返回SUCCESS。然后,从你的struts.xml(也就是上面的那段)看出,此响应将会返回到/back/html/category_list.jsp,也就是说你没有返回到category_edit.jsp中。你应当知道,我们要从action返回后得到Action类中返回的数据,我们可以将要发送到页面的数据放到不同的jsp对象中,如request,session,application中,然后我们再在页面中用reqeust.getAttribute("attr");
或者在struts中使用%{#request.attr}来得到这个属性。所以如果你不返回到那个页面,你无法得到你想得到的值。我认为你现在是想得到goodcatalogList这个变量的值,下如下面这段(从你上面摘录的)
<s:iterator id="goodcatalogItem" value="goodcatalogList">
?
<s:if test="#goodcatalogItem.gcCatalogLevel==1">
<option value="<s:property value='%{#goodcatalogItem.gcId}'/>">
<s:property value="%{#goodcatalogItem.gcCatalogName}"/></option>
</s:if>
</s:iterator>
你是想得到这个iterator中的value值吧,所以你可以这样来:
(1)把上面的iterator改成如下这样:
<s:iterator id="goodcatalogItem" value="%{#request.goodcatalogList}">
(2)你把goodCatalogActioin类的execute()方法改成如下:
@Override
public String execute() throws Exception {
setGoodcatalog(goodcatalogService.getGoodcatalogBygcId(getCategoryId()));
System.out.println(getCategoryId()); HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("goodcatalogList", goodcatalogList);
return "getcatalogList";
}
此外,当你用request.setAttribute("goodcatalogList", goodcatalogList);之前,我想你应当把这个goodcatalogList进行初使化,也就是赋上你想要给他的值。
(3)把你的struts.xml改成如下:
<action name="goodCatalog" class="goodCatalogAction">
<result name="success">/back/html/category_list.jsp</result>
<result name="input">/back/html/home.jsp</result>
<result name="getcatalogList">/back/html/category_edit.jsp</result>
</action>总结:你要想得到返回值,你必须得返回到该页面,为什么会返回到该页面呢,因为在返回之前,我们可以把要传递的参数放到不同的域如request,session,application或其它中,然后我们再在页面中从这些对象中取得我们传递回来的数据,其实这就是为什么我们返回时要进行页面的刷新。所以,你说的那种不返回该页面还暂时不行。
<result name="getcatalogList">/back/html/category_edit.jsp</result>
其实你是可以不用从request,session或application等对象中得到你要得到的结果 。
这种方式就是使用ajax.
如果你想用ajax那么,struts.xml中应改成:
<action name="goodCatalog" class="goodCatalogAction">
<result name="success">/back/html/category_list.jsp</result>
<result name="input">/back/html/home.jsp</result>
<result type="json"></result>
</action>
不过,还有其他要注意的地方,如果想详细的了解,可以给我发消息。
其实这种ajax方式,就是当Action类返回后,我们返回的数据将被封装成json数据格式的数据包,然后,我们就可以从页面通过javascript来得到这个json包,然后解析它,得到你想到得到的数据。
如果想要具体的了解,我们可以一起交流,你也可以到网上找点资料看看这种方式的实现方式 。
category_edit.jsp<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>B2C</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../../web/html/style.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../js/checkform.js"></script></head><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<iframe src="top.htm" frameborder="0" marginheight="0" marginwidth="0" width="770" height="130" scrolling="no"></iframe><table width="100%" border="0" cellpadding="0" cellspacing="0" class="yrh">
<tr>
<td height="17"><a href="../../web/html/home.htm">主页</a> » <a href="category_list.htm">商品目录</a>»商品目录编辑</td>
</tr>
</table>
<table width="770" border="0" cellpadding="0" cellspacing="0" class="main">
<tr valign="top">
<td><form action="" method="post" enctype="multipart/form-data" name="form1">
<table width="100%" border="0" cellpadding="2" cellspacing="1" class="inputTable">
<tr>
<td class="inputTitle">编辑商品目录</td>
</tr>
<tr>
<td>
<table width="100%" border="0" cellpadding="0" cellspacing="1" class="inputbox">
<tr>
<td width="4%" class="inputHeader"> </td>
<td width="17%" class="inputHeader">目录名称:</td>
<td width="40%" class="inputContent">
<input type="Text" size="18" name="name" onFocus="nextfield='password'" value="<s:property value='goodcatalog.gcCatalogName'/>" maxlength="25"> </td>
<td width="39%" class="inputContent"> <font color="#CC0000">* 必输项</font></td>
</tr>
<tr>
<td class="inputHeader"> </td>
<td class="inputHeader">父 目 录:</td>
<td class="inputContent"><select>
<option value="">请选择</option>
<s:action name="goodCatalog"></s:action>
<s:iterator id="goodcatalogItem" value="goodcatalogList">
<s:if test="#goodcatalogItem.gcCatalogLevel==1">
<option value="<s:property value='%{#goodcatalogItem.gcId}'/>"><s:property value="%{#goodcatalogItem.gcCatalogName}"/></option>
</s:if>
</s:iterator>
<option value="tsyx">图书音像</option>
<option value="sssh">时尚生活</option>
<option value="smcp">数码产品</option>
<option value="xhsd">鲜花速递</option>
</select>
</td>
<td class="inputContent"> </td>
</tr>
<tr>
<td class="inputHeader"> </td>
<td class="inputHeader">目录描述:</td>
<td class="inputContent"><textarea name="textarea" rows="6" cols="40"><s:property value='goodcatalog.gcCatalogDesc'/></textarea></td>
<td class="inputContent"><font color="#CC0000"> </font></td>
</tr>
<tr>
<td class="inputHeader"> </td>
<td class="inputHeader">目录图片:</td>
<td class="inputContent"><img src="<s:property value='goodcatalog.gcCatalog'/>" width="170" height="140"><br>
<input type="file" name="file" size="30">
<input type="submit" name="Submit" value="上传"> </td>
<td class="inputContent"> </td>
</tr>
</table>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="4%" class="inputContent"> </td>
<td width="17%" class="inputHeader"> </td>
<td width="39%" class="inputContent">
<input type="Reset" class="bt2" name="button1" value="重填" onClick="clear()">
<input type="Button" class="bt2" name="button2" value="提交" onClick="checkcategoryform()">
<input type="Button" class="bt2" name="button22" value="返回" onClick="javascript:window.location.href='category_list.htm'"></td>
<td width="40%" class="inputContent"> </td>
</tr>
</table></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<!-- #BeginLibraryItem "/Library/footer.lbi" -->
<iframe src="bottom.htm" frameborder="0" marginheight="0" marginwidth="0" width="770" height="130" scrolling="no"></iframe>
<!-- #EndLibraryItem --></body>
</html>
这个页面时用来修改值的,我想知道这个页面取不到下拉列表的值(非静态部分)是我写错了,还是方法不对,具体怎么解决大家给看下
也就是说<s:action>中加上executeResult="true", 把你这个action返回的视图搞成一个空页面就可以了
,不然你那个页面的内容也会显示出来!
所以LZ你应该 把你那一段动态的单独放在一个jsp里面作为那个action的视图,然后在你的edit界面上涌<s:action>引入
你可以在action 里面得到一个你想过滤的List集合.输出:
<s:select list=" " value="" name="" listValue="" listKey="" emptyOption="true" />list 属性就是那个集合,name ,value 这些都不用说了的吧。
?
<s:if test="#goodcatalogItem.gcCatalogLevel==1">
<option value="<s:property value='%{#goodcatalogItem.gcId}'/>"><s:property value="%{#goodcatalogItem.gcCatalogName}"/></option>
</s:if>
</s:iterator>
显示不出来。。就是不用标签,里面的 “?”也不会输出啊。。
<action name="goodCatalog" class="goodCatalogAction">
<result name="success">/back/html/category_list.jsp</result>
<result name="input">/back/html/home.jsp</result>
</action>
这里面的class="goodCatalogAction"应该是class="GoodCatalogAction"吧。
你先看下这里有没有错。
1.struts.xml中:
<action name="goodCatalog" class="goodCatalogAction">
<result name="success">/back/html/category_list.jsp</result>
<result name="input">/back/html/home.jsp</result>
</action>
改成
<action name="goodCatalog" class="com.other.GoodCatalogAction">
<result name="success">/back/html/category_list.jsp</result>
<result name="input">/back/html/home.jsp</result>
</action>
我做时,把你的GoodCatalogAction放到了包com.other中,不过你也可以不这么放,只要找得到就行
2.GoodCatalogAction改成如下:package com.other;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class GoodCatalogAction extends ActionSupport {
private Collection goodcatalogList;
// private GoodcatalogService goodcatalogService; public Collection getGoodcatalogList() {
return goodcatalogList;
} public void setGoodcatalogList(Collection goodcatalogList) {
this.goodcatalogList = goodcatalogList;
} /* public GoodcatalogService getGoodcatalogService() {
return goodcatalogService;
} public void setGoodcatalogService(GoodcatalogService goodcatalogService) {
this.goodcatalogService = goodcatalogService;
}
*/
public String execute() throws Exception {
// setGoodcatalogList(goodcatalogService.getGoodCatalogList());
List list=new ArrayList();
People p=new People();
p.setName("小明");
list.add(p);
this.setGoodcatalogList((Collection)list);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("goodCatalogList", this.getGoodcatalogList());
return "goodCatalogList"; }
}其中有个一People类,我是用于把它加入到list中,这和你用goodcatalogService.getGoodCatalogList();得到你一个包含所有实体的list是一样的,只不过我现在只有一个实体而已。在execute中,我初始化了一个People类的实例,并把它放到我的List中。我现在得到的这个list和你原来那个GoodCatalogAction中的list性质是一样的,只不过我的list里面只有一个实体。3.我简化了一下你的category_edit.jsp,只把那个<select>写进去了,如下:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<form action="">
<select>
<option value="">请选择</option>
<s:action name="goodCatalog" namespace="/test"></s:action>
<s:append id="myAppendIterator">
<s:param value="#request.goodCatalogList" />
</s:append>
<s:iterator id="goodcatalogItem" value="%{#myAppendIterator}">
<option value="<s:property value='%{name}'/>">
<s:property value="%{name}"/>
</option>
</s:iterator>
<option value="tsyx">图书音像</option>
<option value="sssh">时尚生活</option>
<option value="smcp">数码产品</option>
<option value="xhsd">鲜花速递</option>
</select>
</form>
</body>
</html>
我也删除你的if判断,这并不影响,到时候你懂了原理后,你再加上去就可以了。(4)最终的结果是,下当你进入category_edit.jsp时,下拉菜单中出现了 "小明" 这一项。说明,我们读出来了。你应当注意:(1)我们Action中的goodcatalogList应当是Collection类型。因为<iteraator>标签的说明有这么一句说明:"可以对java.util.Collection,java.util.Iterator类型的值进行迭代"。
并且,我们把这个goodcatalogList放到内置对象request中保存起来,以便在页面中取得。
(2)在category_edit.jsp中用我上面写的那样首先从request中得到我们的goodcatalogList,并把它放到<append>标签中,然后<iterator>又得到这个<append>(通过它的id,这个你应当知道)。在<append>标签的说明中有如下描述:"常和Iterator标签一起使用, 功能就是将不同的迭代器组合在一起,使一个迭代器迭代完成后转移到下一个迭代器中继续迭代."
而我们这里正是在组命使用它们。
之后的取值这些,我想楼主你应当是明白的,<s:property value="%{name}"/>,其实就是调用这个迭代器中实体的getXXX()方法来得到该实体(此时是People)的属性值。所以也可以写成这样:
<s:property value="getName()"/>楼主你可以把代码复制下来,然后运行,看看效果后,再看看代码,相信你就明白是怎么回事了。