小弟我刚学完SSH,做了一个留言管理程序,但出现了以下问题:1)  我用到了动态Form和验证框架,用来验证在添加时非空。但实际上无效
2)  方法和Action中的方法都正确,但是插不进去数据,也没报任何异常。(根本没执行Action中的方法)配置文件没错哪位大哥有空,帮我瞧瞧代码吧,看问题是出在哪?不能发附件,知道的QQ叫我吧516121830,不胜感激!

解决方案 »

  1.   

    贴下代码
    网上动态Form例子挺好使的
    楼主照着做做
      

  2.   

    Action:
    private NoteDAO notedao ;
    public NoteDAO getNotedao() {
    return notedao;
    }
    public void setNotedao(NoteDAO notedao) {
    this.notedao = notedao;
    }
    /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward insert(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    DynaValidatorForm noteForm = (DynaValidatorForm) form;
    // TODO Auto-generated method stub
    System.out.println("--------------------->");
    Note note = new Note() ;
    note.setTitle(noteForm.getString("title")) ;
    note.setContent(noteForm.getString("content")) ;
    note.setAuthor(noteForm.getString("author")) ;
    try {
    this.notedao.insert(note) ;
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return mapping.findForward("insertdo");
    }struts-config.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
      <data-sources />
      <form-beans >
        <form-bean name="noteForm" type="org.apache.struts.validator.DynaValidatorForm">
          <form-property name="title" type="java.lang.String" />
          <form-property name="content" type="java.lang.String" />
          <form-property name="author" type="java.lang.String" />
        </form-bean>  </form-beans>  <global-exceptions />
      <global-forwards />
      <action-mappings >
        <action
          attribute="noteForm"
          input="/add_note.jsp"
          name="noteForm"
          parameter="status"
          path="/note"
          scope="request"
          type="org.zgf.note.struts.action.NoteAction" validate="true">
          <forward name="insertdo" path="/insert_do.jsp"></forward>
          </action>  </action-mappings>
    <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
      <message-resources parameter="org.zgf.note.struts.ApplicationResources" />
      
      <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames"
    value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
      </plug-in>    <plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"
    value="/WEB-INF/classes/applicationContext.xml" />
    </plug-in>
    </struts-config>applicationContext.xml
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="org.gjt.mm.mysql.Driver">
    </property>
    <property name="url" value="jdbc:mysql://localhost:3306/mldn"></property>
    <property name="username" value="root"></property>
    <property name="password" value="zhugfily"></property>
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <property name="mappingResources">
    <list>
    <value>org/zgf/note/model/Note.hbm.xml</value></list>
    </property></bean>
    <bean id="hibernatetemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>
    <bean id="notedao" class="org.zgf.note.dao.NoteDAO" abstract="true"></bean>
    <bean id="notedaoimpl" class="org.zgf.note.impl.NoteDAOImpl" parent="notedao">
    <property name="hibernateTemplate">
    <ref bean="hibernatetemplate"/>
    </property>
    </bean>
    <bean name="/note" class="org.zgf.note.struts.action.NoteAction">
    <property name="notedao">
    <ref bean="notedaoimpl"/>
    </property>
    </bean>
    </beans>
    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
          <param-name>debug</param-name>
          <param-value>3</param-value>
        </init-param>
        <init-param>
          <param-name>detail</param-name>
          <param-value>3</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
      </servlet>
      <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/classes/applicationContext.xml
    </param-value>
    </context-param>
      <servlet>
       <servlet-name>context</servlet-name>
       <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>add_note.jsp
    <%@ page language="java" pageEncoding="gbk"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %><html:html lang="true">
      <head>
    <title>添加留言</title>
      </head>
      
      <body>
        <center>
    <h3>欢迎进入添加留言页面</h3><hr>
    <html:form action="note.do" method="post" onsubmit="return validateNoteForm(this)">
    标题:<html:text property="title"></html:text><br>
    内容: <html:textarea property="content"></html:textarea><br>
    作者: <html:text property="author"></html:text><br>
    <input type="hidden" name="status" value="insert"/>
    <html:submit value="提交"></html:submit><html:reset value="重置"></html:reset>
    </html:form>
    <html:javascript formName="noteForm"/>
    </center>
      </body>
    </html:html>
      

  3.   

    既然用spring管理action 那么 type="org.zgf.note.struts.action.NoteAction"应该改成spring
      

  4.   

       我在struts-config.xml中加入了
    <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>这和你那的效果是一样的
      

  5.   

    用spring管理action type="org.zgf.note.struts.action.NoteAction
    改成type=org.springframework.web.struts.DelegatingActionProxy
    试下
      

  6.   

       进都没有进action    只有两种情况    在之前你的程序已经报错了  所以根本不运行了。  第二种你的路径错了    你好好看看你的路径  写错了没有。
      设置双断点 看看。
      
      

  7.   

    public ActionForward insert。这个方法是无效的!
    必须执行execute()方法。
      

  8.   

    你把你插入操作的代码放到execute方法里面就可以了
      

  9.   

    你把你插入操作的代码放到execute方法里面就可以了试试这个
      

  10.   

    你的action会不会是继承了Action而不是DispatchAction?检查一下。
      

  11.   

    如果使用了DispatchAction,那么action中有execute方法,那么就会执行execute方法,而不会执行传过来的那个参数执行的方法!