简体   繁体   English

AngularJS $ http.post数据未与后端pojo绑定

[英]Angularjs $http.post data not binding with backend pojo

I have searched the net and found few solution,but still i am facing the same problem. 我已经在网上搜索并找到了很少的解决方案,但是我仍然面临着同样的问题。

I am trying to create a web application with Angularjs as frontend end spring rest as back end. 我正在尝试使用Angularjs作为前端创建一个Web应用程序,而将Spring Rest作为后端创建一个Web应用程序。 I am able to access the url resource through $http.post methos, but while the binding of data to pojo doesnt happen. 我可以通过$ http.post方法访问url资源,但是不会将数据绑定到pojo。 The values are always null. 该值始终为空。

Web.xml: web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-  app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>login</display-name>
<servlet>
    <servlet-name>springws</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springws</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

My dispatcher servlet configuration 我的调度程序servlet配置

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

<context:component-scan base-package="com.junaid.spring.webservice" />

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

 <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

 <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
    p:password="${jdbc.password}" />

 <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <!-- Drop and re-create the database schema on startup -->
            <prop key="hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>   
</beans>

Angualrjs controller file Angualr.js控制器文件

login.controller('RegisterController',['$scope','userFactory', function($scope, userFactory) {

$scope.user;

$scope.saveUser = function(){
    userFactory.saveUser($scope.user);
};
}]);

Angularjs factory js file AngularJS工厂JS文件

angular.module('login')
.factory('userFactory' , ['$http', function($http){

var userFactory= {};

userFactory.authenticate = function(user){
    console.log(user.name);
    console.log(user.password);
};

userFactory.saveUser = function(user){
    console.log(user.name);
    console.log(user.password);
    console.log(user.phone);
    console.log(user.email);
     $http.post('rest/register', user).success(console.log("registered"));
};

userFactory.forgotPassword = function(user){
    console.log(user.name);
};

return userFactory;
}]);

Jsp page invokes userFactory.saveUser() function which in turns call my rest service, Jsp页面会调用userFactory.saveUser()函数,该函数依次调用我的rest服务,

my controller class 我的控制器班

@Controller
public class UserController {

@RequestMapping(value = "/authenticate", method = RequestMethod.GET)
public  @ResponseBody String getState(UserData ud) {
    System.out.println(ud.getPassword());
    return "true";
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public  @ResponseBody String registerUser(Users ud) {
    System.out.println(ud.getPassword());
    return "true";
}
}

The println statements always prints null. println语句始终输出null。

Can anyone tell me where i am going wrong. 谁能告诉我我要去哪里错了。

You need the @RequestBody in your getState and registerUser methods before the Argument. 在参数之前,您需要在getStateregisterUser方法中使用@RequestBody Furthermore getState must be a POST Method. 此外, getState必须是POST方法。

@Controller
public class UserController {

@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
public  @ResponseBody String getState(@RequestBody UserData ud) {
    System.out.println(ud.getPassword());
    return "true";
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public  @ResponseBody String registerUser(@RequestBody Users ud) {
    System.out.println(ud.getPassword());
    return "true";
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM