first.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<html> 
%>
<head>
<title>JSP for FirstForm form</title>
</head>
<body>
<html:form action="/first" method="post">
添加分类:<html:text property="fl"/><html:errors property="fl"/>
<html:submit/>
</html:form>
  <table>
<logic:present name="fls" scope="request">
<logic:iterate id="fl" name="fls" scope="request"> 
<tr>
  <td><bean:write name="fl" property="fl"/></td>
<td><html:link action="/deleteFl.do" paramId="fl" paramName="fl" paramProperty="fl">删除</html:link></td>
</tr> 
  </logic:iterate>
</logic:present> 
</table>
</body>
</html>
FirstA
package com.yourcompany.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.FirstForm;
import dao.FlDao;
import java.util.ArrayList;
import vo.Fl;
public class FirstAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
FirstForm firstForm = (FirstForm) form;// TODO Auto-generated method stub
String fl=firstForm.getFl();
fl = new String(fl.getBytes("ISO-8859-1"));
FlDao flDao=new FlDao();
Fl fll=new Fl();
fll.setFl(fl);
flDao.insertFl(fll);
ArrayList fls=flDao.getAllFl();
request.setAttribute("fls",fls);
return new ActionForward("/first.jsp");
   }
}我做的JSP是向数据库提交一个东西  然后在本页显示 奇怪的是刚进入页面只显示提交表单  在提交一次后才显示数据库表中的所有内容  为什么啊?

解决方案 »

  1.   

    应为你的fls是空的,你第一次并没有进入action
      

  2.   

    因为没有提交,就没有经过action,就没有数据,所以只显示提交表单!
      

  3.   

    没提交前,没进入action,所以没有数据!因为没有执行action里的程序!
      

  4.   

    >>>>><logic:present name="fls" scope="request">
    初次进入时,Request中的fls肯定是不存在的,所以页面也就不能显示了
      

  5.   

    如果进入.do的话  会显示java.lang.NullPointerException
    com.yourcompany.struts.action.FirstAction.execute(FirstAction.java:25)

    FirstAction
    package com.yourcompany.struts.action; import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import org.apache.struts.action.Action; 
    import org.apache.struts.action.ActionForm; 
    import org.apache.struts.action.ActionForward; 
    import org.apache.struts.action.ActionMapping; 
    import com.yourcompany.struts.form.FirstForm; 
    import dao.FlDao; 
    import java.util.ArrayList; 
    import vo.Fl; 
    public class FirstAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response)throws Exception { 
    FirstForm firstForm = (FirstForm) form;// TODO Auto-generated method stub 
    String fl=firstForm.getFl(); 
    fl = new String(fl.getBytes("ISO-8859-1")); 
    FlDao flDao=new FlDao(); 
    Fl fll=new Fl(); 
    fll.setFl(fl); 
    flDao.insertFl(fll); 
    ArrayList fls=flDao.getAllFl(); 
    request.setAttribute("fls",fls); 
    return new ActionForward("/first.jsp"); 
      } 

      

  6.   

    初次进入时Request中的fls是不存在的,页面是不能显示的
      

  7.   

    String fl=firstForm.getFl(); 
    fl = new String(fl.getBytes("ISO-8859-1"));  // 我认为应该是这一句出异常,因为 fl 可能是Null
    FlDao flDao=new FlDao(); 
    Fl fll=new Fl(); 
    fll.setFl(fl);