请教struts 1 标签的问题,比较有意思的一个问题!

z3s6e3 2011-12-16 01:31:39
action 源码
                        public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm daf = (DynaActionForm)form;
List<LabelValueBean> li = new ArrayList<LabelValueBean>();
LabelValueBean lvb1 = new LabelValueBean("xu","小学");
LabelValueBean lvb2 = new LabelValueBean("cz","初中");
LabelValueBean lvb3 = new LabelValueBean("gz","高中");
LabelValueBean lvb4 = new LabelValueBean("du","大学");
li.add(lvb1);
li.add(lvb2);
li.add(lvb3);
li.add(lvb4);
daf.set("mlist", li);
//request.setAttribute("mlist", li);
return new ActionForward("/select.jsp");
}


struts-config.xml

<form-bean name="indexForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="mlist" type="java.util.ArrayList"></form-property>
<form-property name="val" type="java.lang.String"></form-property>
</form-bean>

<action-mappings>
<action path="/demo" type="com.ccb.dsl.struts.DemoAction" name="indexForm" scope="request"/>
</action-mappings>



jsp源码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>

<%
String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<html:form action="/demo">
<html:select property="por">
<html:option value="">请选择</html:option>
<html:options collection="mlist" property="label" labelProperty="value"/>
</html:select>
</html:form>
</body>
</html>



现在问题出在jsp页面和action中,如果在action中加入request.setAttribute("mlist", li); 页面正常显示,可以出现需要的结果,
但是不加request.setAttribute("mlist", li);页面姐就出错,错误提示如下:
javax.servlet.ServletException: Cannot find bean under name mlist
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.select_jsp._jspService(org.apache.jsp.select_jsp:106)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1085)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:398)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:241)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

我在indexForm中定义了mlist这个属性,而且是List的,为什么在页面中就不能取得吗?非要使用request.setAttribute("mlist", li); 才可以,请高人解答一下!!
...全文
86 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
逍遥猫 2011-12-16
  • 打赏
  • 举报
回复
collection指定存放在request范围内的集合,好像不能用form进行set
逍遥猫 2011-12-16
  • 打赏
  • 举报
回复
你在DynaActionForm中定义一个List,比如 xlList.在action中daf.setXlList(li);看看行不
z3s6e3 2011-12-16
  • 打赏
  • 举报
回复
我现在是有时间,我想把struts的标签联系联系!以前都是用java代码在jsp实现的,感觉页面不干净,现在想用struts标签完全实现页面的流程,让页面干净一点!
zysnba 2011-12-16
  • 打赏
  • 举报
回复
request.setAttribute("mlist", li);
这几话一定要有的,这个就是在Action里面set下你的list,页面这个下拉框就可以取到li值了,你如果不set页面不知道从什么地方取值的,你明白吗?加油多看点,貌似新手!
y_0715 2011-12-16
  • 打赏
  • 举报
回复
实在不行你可以用<c:foreach>啊 这样试试
仲兴轩 2011-12-16
  • 打赏
  • 举报
回复
是要加 form 名, form名是你在 struts.config.xml里面配置的名字,
我们都是再另建个form, 你这里DynaActionForm, 应该也差不多,
因为你这个LIST本身是个集合, 还是放在 form 里面的, 遍历是要从form开始写名称咯

要不, 你就先直接用 ${###formName###.mlist} 放在JSP里面, 看出来有结果不?
z3s6e3 2011-12-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhongxingxuan 的回复:]
<html:options collection="mlist" property="label" labelProperty="value"/>
如果是 form 里面, 就要加 collection="${###formName###.mlist}"// 不知道要不要加EL, 你试试
[/Quote]
还是不行!谢谢你
仲兴轩 2011-12-16
  • 打赏
  • 举报
回复
<html:options collection="mlist" property="label" labelProperty="value"/>
如果是 form 里面, 就要加 collection="${###formName###.mlist}"// 不知道要不要加EL, 你试试
比丘 2011-12-16
  • 打赏
  • 举报
回复
没有获取到对象当然报错!
比丘 2011-12-16
  • 打赏
  • 举报
回复
struts1属性是不会帮你放到request,而struts2会!
z3s6e3 2011-12-16
  • 打赏
  • 举报
回复
没人知道原因吗???

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧