简体   繁体   English

Spring MVC,AngularJS错误500

[英]Spring MVC ,AngularJS error 500

I am new to SpringMVC and AngularJS. 我是SpringMVC和AngularJS的新手。 I am trying to post some user's data from my jsp signUp page in my Mysql database using ngResource but the console displays the following exception. 我试图使用ngResource从我的Mysql数据库中的jsp signUp页面发布一些用户的数据,但控制台显示以下异常。

    SEVERE: Servlet.service() for servlet [view] in context with path [/FindTheLoc] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: could not execute statement; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'password' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

Below I will attach my code: Angular Controller 下面我将附上我的代码:Angular Controller

app.controller('PersonController', ['$scope', 'Person', function($scope, Person) {

var ob = this;
ob.persons=[];
ob.person = new Person(); 
ob.fetchAllPersons = function(){
    ob.persons = Person.query();
};
ob.fetchAllPersons();
ob.addPerson = function(){

  ob.person.$save(function(person){
     console.log(person); 
     ob.flag= 'created';    
     ob.reset();    
     ob.fetchAllPersons();
  }

      )};



ob.reset = function(){
    ob.person = new Person();
    $scope.personForm.$setPristine();
};  



}]);

Angular Service user.js Angular Service user.js

    app.factory('Person', ['$resource', function ($resource) {
    return $resource('http://localhost:8080/FindTheLoc/person/:personId', {personId: '@pid'},
    {
        updatePerson: {method: 'PUT'}
    }
    );
}]);

signUp.jsp signUp.jsp

<div class="div-signUp" ng-controller="PersonController as personCtrl">
        <h2>Εγγραφή</h2>

        <form id="signUp-Fomm"  name="personForm" method="POST">
            <p>Όνομα Χρήστη:</p><input type="text" name="name" ng-model="personCtrl.person.name" required /> <br>
            <p>Κωδικός:</p><input type="password" name="password" ng-model="personCtrl.person.pass" required /><br>
            <p>Τηλέφωνο:</p><input type="tel" name="telephone" ng-model="personCtrl.person.telephone" required /><br>
            <p>Εmail:</p><input type="email" name="email" ng-model="personCtrl.person.email" required /><br>
            <p>Χώρα:</p><input type="text" name="country" ng-model="personCtrl.person.country" required /><br>
            <p>Διεύθυνση:</p><input type="text" name="address" ng-model="personCtrl.person.address" required /><br>
            <button type="submit" ng-click="personCtrl.addPerson()" value="Add Person">Υποβολή</button>

        </form>

    </div>

Spring Controller 弹簧控制器

   package gr.findtheloc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.UriComponentsBuilder;

import gr.findtheloc.model.User;
import gr.findtheloc.service.UserService;



@Controller
public class UserController {

    @Autowired
    private UserService personService;

    @RequestMapping(value = "/signUp")
    public String getSignUp() {

        return "signUp";
    }

    @RequestMapping("/signIn")
    public String getSignIn() {

        return "signIn";
    }



     @RequestMapping(value = "/person", method = RequestMethod.POST)
        public ResponseEntity<Void> createUser(@RequestBody User user,    UriComponentsBuilder ucBuilder) {
            System.out.println("Creating User " + user.getUsername());


            personService.create(user);

            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
            return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
        }


}

model 模型

 package gr.findtheloc.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity

@Table(name = "user")

public class User {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")

    private Long id;

    private String username;

    private String password;

    private String telephone;

    private String email;

    private String country;

    private String address;

    public User() {
        super();
    }

    public User(Long id,String username, String password, String telephone, String email, String country,
            String address) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.telephone = telephone;
        this.email = email;
        this.country = country;
        this.address = address;
    }

    public Long getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }



}

my database 我的数据库

    CREATE TABLE  `user` (
  `id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(45),
  `password` varchar(45) NOT NULL,
  `telephone` varchar(45) NOT NULL,
  `email` varchar(45) NOT NULL,
  `country` varchar(45) NOT NULL,
  `address` varchar(45) NOT NULL
  PRIMARY KEY (`id`),
  UNIQUE KEY `email_UNIQUE` (`email`),
  UNIQUE KEY `password_UNIQUE` (`password`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

my applicationContext xml file 我的applicationContext xml文件

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

    <!-- Activate Annotations in Beans -->
    <context:annotation-config />

    <!-- Scan and Register Beans Under this Package -->
    <context:component-scan base-package="gr.findtheloc" /> 

    <!-- Configure JPA -->
    <jpa:repositories base-package="gr.findtheloc.repository" />

    <!-- Load Configurations from Files -->
    <context:property-placeholder location="classpath:properties/datasource.properties,
        classpath:properties/hibernate.properties"/>

    <!-- Hikari Connection Pool Configuration -->
    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="poolName" value="springHikariCP" />
        <property name="connectionTestQuery" value="SELECT 1" />
        <property name="dataSourceClassName" value="${datasource.classname}" />
        <property name="transactionIsolation" value="TRANSACTION_READ_COMMITTED" />                 
        <property name="dataSourceProperties">
            <props>
                <prop key="url">${datasource.url}</prop>
                <prop key="user">${datasource.username}</prop>
                <prop key="password">${datasource.password}</prop>              
            </props>
        </property>
    </bean>

    <!-- Define Hikari Bean -->
    <bean id="findthelocDataSource" class="com.zaxxer.hikari.HikariDataSource"
        destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean>

    <!-- Create the JPA Entity Manager Factory -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="findthelocDataSource" />
        <property name="packagesToScan">
            <list>
                <value>gr.findtheloc.model</value>
            </list>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>                         
                <prop key="hibernate.globally_quoted_identifiers">${hibernate.globally_quoted_identifiers}</prop>
                <prop key="hibernate.hikari.idleTimeout">${hibernate.hikari.idleTimeout}</prop>
                <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
            </props>
        </property>
        <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
        </property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
</beans>

my view-servlet.xml 我的view-servlet.xml

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

    <!-- Activate Annotations in Beans -->
    <context:annotation-config/>

    <!-- Scan and Register Controller Beans Under this Package -->
    <context:component-scan base-package="gr.findtheloc.controller"/>

    <!-- Delegate Requests to the Controllers -->
    <!-- http://thespringthing.blogspot.gr/2010/10/what-does-mvcannotation-driven-do.html -->
    <mvc:annotation-driven/>

    <!-- Define the Path for the View Resources (ex css, js) -->
    <mvc:resources mapping="/resources/**" location="/WEB-INF/views/resources/" />

    <!-- Define the Path (suffix) and the Type (suffix) of a View   -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

what is wrong? 怎么了? thank you in advance 先感谢您

Here's the error 这是错误

 <p>Κωδικός:</p><input type="password" name="password" ng-model="personCtrl.person.pass" required /><br>

It should be 它应该是

<p>Κωδικός:</p><input type="password" name="password" ng-model="personCtrl.person.password" required /><br>

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

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