2017-04-24 73 views
1

我是SpringMVC和AngularJS的新手。我正嘗試使用ngResource從我的Mysql數據庫中的jsp signUp頁面發佈一些用戶的數據,但控制檯顯示以下異常。Spring MVC,AngularJS錯誤500

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) 

下面我會附上我的代碼: 角控制器

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(); 
}; 



}]); 

角服務user.js的

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

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> 

春季控制器

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); 
     } 


} 

模型

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; 
    } 



} 

我的數據庫

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; 

我的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> 

我看來-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> 

有什麼不對? 預先感謝您

回答

0

這裏的錯誤

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

應該

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

哦,是的!你說得對!非常感謝你!這個問題由PARTH Ghiya回答! – asimakis

+0

@asimakis:歡迎您,很高興幫助 –