如题,请高手帮帮忙,原来写了好多类,增加一个,修改一个,这样太麻烦了,但是弄到一个里面老出错,你们看下代码,只写了2个类,帮我合并一下,让我举一反三一下,谢谢! 增加的
package action;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import actionform.*;
import model.*;public class SaveNewsAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
{
NewsForm saveNewsForm=(NewsForm)form;
try
{
News news= new News(saveNewsForm);
news.save();
request.setAttribute("info","保存成功!");
}
catch(Exception e)
{
request.setAttribute("info",e.getMessage());
}
return mapping.findForward("save");
}
}
修改的package action;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import actionform.*;import java.util.*;
public class SearchNewsAllAction  extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
{
NewsForm searchNewsAllForm=(NewsForm)form;
try
{
SearchNews searchNewsAll = new SearchNews(searchNewsAllForm);
List<String[]>result=searchNewsAll.search();
if(result.size()>0)
{
request.setAttribute("result",result);
request.setAttribute("info_all", "总记录数:" + String.valueOf(result.size()));
}
            else  
                request.setAttribute("info_all", "没有符合要求的记录!"); }
catch(Exception e)
{
request.setAttribute("info_all",e.getMessage());
}
return mapping.findForward("searchAll");
}}

解决方案 »

  1.   

    可以在ACTION 接受一个 REQUEST 过来的一个变量~~
    然后跟据这个操作符 来走不同的业务逻辑~~
      

  2.   

    LZ说的对,你可以用DispatchAction   只要把你的Action改为继承
    public class xxAction extends DispatchAction {  
    public ActionForward  aa(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception {
    setNaviStringToScope(form, request);
    return this.list(mapping, form, request, response);
    } public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    HttpServletResponse response) {
    return null;
    }
    }   
    2.在页面 <form action = "xx.do?method=aaa"/>  // method 是DispatchAction 的一个参数来区分方法的  可任意取名
    3.struts-config.xml里配置要这样<action path="/xx" attribute="af" name="fullLazyForm" type="xxxx" scope="request"
    validate="false" parameter="method(加个)">
    大概就这样,在有什么不明白的 网上找找关于DispatchAction
      

  3.   

           setNaviStringToScope(form, request);
    这句我没看懂 - -
      

  4.   


    你看一下,我这样合并的,只有添加可以执行,不知道为什么。点查询没有反应。不报错。
    这是action代码
    public class AllNewsAction  extends DispatchAction
    {
    public ActionForward addnews(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
    {
    NewsForm saveNewsForm=(NewsForm)form;
    try
    {
    ActionNews news= new ActionNews(saveNewsForm);
    news.save();
    request.setAttribute("info","保存成功!");
    }
    catch(Exception e)
    {
    request.setAttribute("info",e.getMessage());
    }
    return mapping.findForward("save");
    }
    public ActionForward showall(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
    {
    NewsForm searchNewsAllForm=(NewsForm)form;
    try
    {
    ActionNews searchNewsAll = new ActionNews(searchNewsAllForm);
    List<String[]>result=searchNewsAll.search();
    if(result.size()>0)
    {
    request.setAttribute("result",result);
    request.setAttribute("info_all", "总记录数:" + String.valueOf(result.size()));
    }
                else  
                    request.setAttribute("info_all", "没有符合要求的记录!"); }
    catch(Exception e)
    {
    request.setAttribute("info_all",e.getMessage());
    }
    return mapping.findForward("searchAll");
    }
    这是JSP页面添加的代码:
    <html:form><td align="center">
    <html:submit value="提交" /><input type=hidden name="method" value="addnews">
    &nbsp;&nbsp;
    <html:reset value="重置" /></html:form>这是查询的代码,都在一个JSP页面: <html:form action="searchNewsAll?method=showall" >
    <html:submit  value="查询所有记录 " /><input type=hidden name="method" value="showall">
    </html:form> XML:
      <action name="searchNewsAllForm" path="/searchNewsAll" scope="request" type="action.AllNewsAction" parameter="method">
        <forward name="searchAll" path="/index.jsp" />
    </action>  <action name="saveNewsForm" path="/saveNews" scope="request" type="action.AllNewsAction" parameter="method">
      <forward name="save" path="/message.jsp" />
      </action>高手们看看哪里错了
      

  5.   


    无法实现 showall 这个方法,<html:form action="searchNewsAll?method=showall"> 这条 语句没有效果,不报错只能执行save 。 是不是我配置错了?还是什么麻烦看一下啦
      

  6.   

     1. <html:form action=" AllNews.do?method=showall" >
        2.  <input type=hidden name="method" value="showall">
         <html:submit  value="查询所有记录 " />
            </html:form> 
    1和2 两句意思是一样的  只要一个增加 这样写
    <html:form><td align="center">
      <input type="button" name="add" id="add" value=" 添 加 " onclick="location.href=' AllNews.do?method=addnews" /> &nbsp;&nbsp;
     <html:submit value="提交" /> <html:reset value="重置" /></html:form>1. <html:form action=" AllNews.do?method=showall" >里的 AllNews.do 中的 AllNews对应你的Action名字  不要弄错
    2.   AllNews.do 中的.do在web.xml中配置的 <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>do不是唯一的可以任意换
      

  7.   


    <html:form action="searchNewsAll.do" >
            <html:submit  value="查询所有记录 " /><input type=hidden name="method" value="showall">
            </html:form> 
      

  8.   

    <action name="searchNewsAllForm" path="/searchNewsAll" scope="request" type="action.AllNewsAction" parameter="method">
        <forward name="searchAll" path="/index.jsp" />
    </action>  <action name="saveNewsForm" path="/saveNews" scope="request" type="action.AllNewsAction" parameter="method">
      <forward name="save" path="/message.jsp" />
      </action>
    这个不用配置2遍的
    只要你提交的时候提交一个   "method"  的参数就可以,不用配置2遍,他会根据method参数的值自己去找方法
      

  9.   

    我的增加可以实现,不修改也可以增加,但是就这个查询不可以代码分开写的时候可以,合并以后就查不到了,程序不报错。。你说要把AllNews.do这个AllNews改成我的public class AllNewsAction 这个类得名字么?  那么 struts-config.xml怎么配?<action name="AllNewsActionForm" path="AllNewsAction" scope="request" type="action.AllNewsAction" parameter="method">
        <forward name="searchAll" path="/index.jsp" />
    </action>这个forward怎么配?是不是所有的form名字都是AllNewsAction,然后<forward name=...>这里输入多个forward?
      

  10.   

    你的问题16楼已经给你回答了 自己上网看看
    **AllNews.do这个AllNews当然要改成public class AllNewsAction 这个类得名字
      

  11.   

    哎,我疯了,反正就是不可以改成这个类得名字,那forward就报错了: Unable to find 'delete' forward.反正我是郁闷了,增加就是不报错,哎,求进一步指导啊
      

  12.   


    不好意思,我说错了, 不报错, 可以forward,但结果是和查询一样的,不执行该方法
      

  13.   


    恩,我这样子都配好了,而且类也合到一起了,但是就是有一个方法不能实现:showall能帮我看下哪里错了吗?? 不报错,就是点击查询所有信息 页面是空白,但是输入编号查一条就可以。
    请看代码:
    public ActionForward showall(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
    {
    NewsForm AllNewsActionForm=(NewsForm)form;
    try
    {
    ActionNews actionNews = new ActionNews(AllNewsActionForm);
    List<String[]>result=actionNews.search();
    if(result.size()>0)
    {
    request.setAttribute("result",result);
    request.setAttribute("info_all", "总记录数:" + String.valueOf(result.size()));
    }
                else  
                    request.setAttribute("info_all", "没有符合要求的记录!"); }
    catch(Exception e)
    {
    request.setAttribute("info_all",e.getMessage());
    }
    return mapping.findForward("searchAll");
    }具体操作方法代码:public List<String[]>search() throws Exception
     {
     List<String[]> result = new LinkedList<String[]>();
     String sql="select * from s_news";
     PreparedStatement pstmt= conn.prepareStatement(sql);
     ResultSet rs =pstmt.executeQuery();
     while(rs.next())
     {
     String[] row=new String[6];
       row[0] = rs.getString(1);
               row[1] = rs.getString(2);
               row[2] = rs.getString(3);
               row[3] = rs.getString(4);
               row[4] = rs.getString(5);
               row[5] = rs.getString(6);
               result.add(row);
     }
     rs.close();
     conn.close();
     return result;
     }xml:
     <forward name="updateNews" path="/message.jsp"/>
       <forward name="AllNewsAction" path="/index.jsp"/>
      </global-forwards>
      <action-mappings>
      <action name="AllNewsActionForm" path="/AllNewsAction" scope="request" type="action.AllNewsAction" parameter="method">
      <forward name="save" path="/message.jsp" />
      <forward name="searchAll" path="/index.jsp" />
      <forward name="search" path="/index.jsp" />
      <forward name="update" path="/message.jsp" />
      </action>
      </action-mappings>
    JSP代码:<c:set var="result" value="${requestScope.result}" />
       <table width="100%">
    <tr align="center">
    <td>
     ${requestScope.info_all}
    </td>
    </tr>
    <tr align="center">
    <td>
    <logic:present name="result">
     <table border="1">
                                  <tr align="center">
               <td> 编号 <input type="hidden" name="news_detail" value="${row[4]}"></td>
                                      <td> 主题 </td>
                                      <td> 作者</td>
                                      <td> 日期</td>
                                      <td align="center" colspan=4>操作</td>
                                  </tr>
                <logic:iterate id="row" name="result">
                              <tr align="center">
                        <td> ${row[0]} </td>
                        <td> ${row[1]} </td>
                         <td> ${row[2]} </td>
                        <td> ${row[3]}</td>                                       
                               </tr>
                                  </logic:iterate>
                                  </table>
                                  </logic:present>
                                  </td>                              
    </tr>
    </table>
      

  14.   


    首先你要确定你的action配置的名称  <action name="AllNewsActionForm" path="/AllNewsAction" scope="request" type="action.AllNewsAction" parameter="method">
      <forward name="save" path="/message.jsp" />
      <forward name="searchAll" path="/index.jsp" />
      <forward name="search" path="/index.jsp" />
      <forward name="update" path="/message.jsp" />
      </action>这里给你配置叫“AllNewsAction”然后你不论做什么操作,肯定是提交到AllNewsAction.do的,但是你要指定一个你要执行的方法名称
    所以你必须要提交一个参数,这个参数名在这里配置成了“method”你检查一下你都传了method参数了么,参数的值确定是类里面的方法名么?
      

  15.   


    我配置肯定是没问题的,要不那些增,改,删就实现不了。
    但是就这个 showall 方法没用, 上面也列出代码了。。哎,悲剧。对了不是页面空白,而是还是forward到主页面了,只是没有select出来结果,可是我写的方法没错呀。
      

  16.   

      <forward name="searchAll" path="/index.jsp" />你这里是配置的跳到主页面啦?<html:form action="searchNewsAll.do" >   你这里的action是不是写的对的
    你断点调试下,看有没有进showall方法,如果进了却没有结果,那就是查询的问题