表格未在春季提交(SimpleController)

时间:2015-02-21 18:41:13

标签: java spring spring-mvc

我在我的控制器中扩展SimpleFormController并使用formBackingObject方法在表单加载时显示一些数据。但是当我点击我的页面上的提交按钮时表单没有提交,它的事件没有去  OnSubmit方法。我不知道出了什么问题,请帮助我。

谢谢

下面是我的代码

映射

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:component-scan base-package="river.amit.com"/> 
    <bean id="viewResolver"     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
        <props>
           <prop key="/hello.html">helloController</prop>
           <prop key="/login.html">loginController</prop>
           <prop key="/role.html">roleController</prop>
           <prop key="/person.html">personController</prop>
          </props>
       </property>
    </bean>
    <bean id="helloController" class="river.amit.com.HelloWorldController"/>
    <bean id="loginController" class="river.amit.com.LoginController"/>
    <bean id="roleController" class="river.amit.com.RoleController"/>
    <bean id="personController" class="river.amit.com.PersonController">
    <property name="formView" value="person" />
    <property name="successView" value="message" />
    </bean>
        <bean id="localeChangeInterceptor"      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="hl" />
    </bean>
    <bean id="localeResolver"       class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
        <!-- Hibernate configuration -->
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
        <property name="driverClassName"  value="com.mysql.jdbc.Driver"></property>  
        <property name="url" value="jdbc:mysql://localhost:3306/river"></property>  
        <property name="username" value="root"></property>  
        <property name="password" value=""></property>  
    </bean>  
        <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"></property>  
        <property name="mappingResources">  
        <list>  
        <value>\river\amit\com\curd\login.hbm.xml</value>  
         <value>\river\amit\com\curd\Person.hbm.xml</value> 
        <value>\river\amit\com\curd\Role.hbm.xml</value>
         </list>  
        </property>  
          <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.show_sql=true
                hibernate.hbm2ddl.auto=update
            </value>
        </property>
    </bean>  
           <tx:annotation-driven/>
      <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean> 

       <bean id="service" class="river.amit.com.service.LoginService"></bean>
       <bean id="logindao" class="river.amit.com.dao.LoginDao"> 
         <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean> 
    <bean id="roleService" class="river.amit.com.service.RoleService"></bean>
      <bean id="roleDao" class="river.amit.com.dao.RoleDao">    
             <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean>
     <bean id="personService" class="river.amit.com.service.PersonService"></bean>
      <bean id="personDao" class="river.amit.com.dao.PersonDao">    
             <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean>
</beans>

控制器

package river.amit.com;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import river.amit.com.curd.Person;
import river.amit.com.dao.PersonDao;
import river.amit.com.dao.RoleDao;
import river.amit.com.service.PersonService;
import river.amit.com.service.RoleService;

@SuppressWarnings("deprecation")

public class PersonController extends SimpleFormController {

    @Autowired
    PersonService personService;

    @Autowired
    PersonDao personDao;

    @Autowired
    RoleDao roleDao;

    @Autowired
    RoleService roleService;

    public PersonController() {
        setCommandClass(Person.class);
        setCommandName("personForm");
    }


    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map referenceData = new HashMap();
        return referenceData;
    }
    @Override
    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {

        Person cust = new Person();
        // Make "Spring MVC" as default checked value
        cust.setFirstName("java");
        return cust;
    }

    @Override
    protected ModelAndView onSubmit(/*HttpServletRequest request,
            HttpServletResponse response,*/ Object command/*, BindException errors*/)
            throws Exception {
        // TODO Auto-generated method stub=
        Person cust=(Person) command;
        System.out.println("fgng");
            return new ModelAndView("message");
        }
        // return null;

}

JSP

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<form:form  commandName="personForm" method="post">
<input type="text" name="firstName">  <br> <br> 
<form:input path="firstName"/> 
  <input type="text" name="contact"> <br><br> 
 <!--  <input type="text" name="email"><br> <br>
  <input type="text" name="address"><br><br> 
  <input type="text" name="login"><br><br> --> 
  <form:button name="submit" value="submit"></form:button>
  <input type="submit" value="Submit" />  
</form:form>
  <a href="role.html">Role</a>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

从弹簧配置文件中删除以下行。

<context:component-scan base-package="river.amit.com"/>

或从扫描中排除controller个包裹。