当前位置:编程学习 > JAVA >>

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

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); 才可以,请高人解答一下!! --------------------编程问答-------------------- 没人知道原因吗??? --------------------编程问答-------------------- struts1属性是不会帮你放到request,而struts2会! --------------------编程问答-------------------- 没有获取到对象当然报错! --------------------编程问答-------------------- <html:options collection="mlist" property="label" labelProperty="value"/>
如果是 form 里面, 就要加 collection="${###formName###.mlist}"// 不知道要不要加EL, 你试试 --------------------编程问答--------------------
引用 4 楼 zhongxingxuan 的回复:
<html:options collection="mlist" property="label" labelProperty="value"/>
如果是 form 里面, 就要加 collection="${###formName###.mlist}"// 不知道要不要加EL, 你试试

还是不行!谢谢你 --------------------编程问答-------------------- 是要加 form 名, form名是你在 struts.config.xml里面配置的名字,
我们都是再另建个form, 你这里DynaActionForm, 应该也差不多,
因为你这个LIST本身是个集合, 还是放在 form 里面的, 遍历是要从form开始写名称咯

要不, 你就先直接用 ${###formName###.mlist} 放在JSP里面, 看出来有结果不? --------------------编程问答-------------------- 实在不行你可以用<c:foreach>啊 这样试试 --------------------编程问答-------------------- request.setAttribute("mlist", li);
这几话一定要有的,这个就是在Action里面set下你的list,页面这个下拉框就可以取到li值了,你如果不set页面不知道从什么地方取值的,你明白吗?加油多看点,貌似新手! --------------------编程问答-------------------- 我现在是有时间,我想把struts的标签联系联系!以前都是用java代码在jsp实现的,感觉页面不干净,现在想用struts标签完全实现页面的流程,让页面干净一点! --------------------编程问答-------------------- 你在DynaActionForm中定义一个List,比如 xlList.在action中daf.setXlList(li);看看行不 --------------------编程问答-------------------- collection指定存放在request范围内的集合,好像不能用form进行set
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,