模型值不会自动从客户端作为json数据

时间:2014-01-10 15:27:15

标签: jquery json spring

我编写了一些接受来自用户的数据的代码,用jquery验证它,然后以json的形式发送给服务器。直到客户端验证每件事情都很完美。但是当我移动到服务器端时,我使用spring控制器来获取json值。所有模型属性值均为null。即使我在客户端开发模式中看到数据正朝向服务器但未填充到模型属性中。

我的jquery代码如下:

$('#login-form').submit(function(event){
        var username = $('#id_username').val();  
        var password = $('#id_password').val();
        alert(username + password)
        var json = { "loginId" : username, "password" : password};

      $.ajax({  
        type: "POST",  
        url: $("#login-form").attr("action"),
        data: JSON.stringify(json),
        beforeSend: function(xhr){
            var mess = validateForm();
            if(mess.length != 0){
                $('#error-mes').show();
                $('#error-mes').html(mess);
                event.preventDefault();
                return false;
            }

            xhr.setRequestHeader("Accept", "application/json");  
            xhr.setRequestHeader("Content-Type", "application/json");
            return true;
        },
        success: function(response){
             $('#error-mes').html(response);
             $('#error-mes').show();
        },  
        error: function(e){  
          alert('Error: ' + e);  
        }  
      });  
      event.preventDefault();
      $('#userName').hide();
      $('#spn_password').hide();
});

春季控制器看起来像:

@RequestMapping(value = "/signin", method = RequestMethod.POST )
    public @ResponseBody String submitCustSignInForm(HttpServletRequest request, @ModelAttribute("model") Person model,
            HttpSession sess) {

        String response = "";
        Person person = null;

        if (sess.getAttribute("USER_INFO") == null) {
            person = tsService.login(model);
            if (person == null) {
                response = "User name or password does not match.";
            } else {
                response = "success";
                sess.setAttribute("USER_INFO", person);
            }
        }
        return response;
    }

和html页面看起来像:

<form class="aui login-form"
     method="post" id="login-form"></form>

不发布整个html表单,假设用户名文本框和密码id与我在jquery中使用的相同。

有人可以告诉我这段代码有什么问题。

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.server.controller" />
    <mvc:annotation-driven />

    <bean id="bDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="username" value="postgres" />
        <property name="password" value="postgres" />
        <property name="url" value="jdbc:postgresql://localhost:5432/test" />
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="hibernateProperties">
            <value>
                hibernate.cache.use_second_level_cache=true
                hibernate.hibernate.cache.use_query_cache=false
                hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
                hibernate.current_session_context_class=thread
                hibernate.show_sql=false
                hibernate.format_sql=true
            </value>
        </property>
        <property name="mappingResources">
            <value>objectmodel.hbm.xml</value>
        </property>
        <property name="dataSource" ref="bDataSource" />
    </bean>


    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
        <property name="order" value="1" />
    </bean>



    <bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>



    <bean id="tsService" class="com.server.service.TsServiceImpl">
        <property name="dao" ref="dao" />

    </bean>

    <bean id="dao" class="com.server.dao.DaoSupportImpl" scope="singleton">
        <property name="hibernateTemplate" ref="hibernateTemplateBean" />
    </bean>



    <bean id="dispatcherDao" class="com.server.servlet.UploadVideoServlet">
        <property name="disDao" ref="dao" />
    </bean>


    <mvc:resources location="./css/" mapping="/css/**"/>
    <mvc:resources location="./js/" mapping="/js/**" />
    <mvc:resources location="./images/" mapping="/images/**" />
    <mvc:resources location="./content/" mapping="/content/**" />

</beans>

0 个答案:

没有答案