2

I am using struts2 framework with angularjs.There are two buttons one for reset the texts fields with default value and other is to submit the page. my submit is not working. My jsp page is:-

<!DOCTYPE html>
<html>
 <head>
  <link rel="stylesheet"
   href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
  <script
   src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
 </head>
 <body ng-app="postApp" ng-controller="postController">
  <div class="container">
   <div class="col-sm-8 col-sm-offset-2">
    <div class="page-header">
     <h1>
      Post data using angularJS
     </h1>
    </div>
    <form name="studentForm" novalidate >
     <div class="form-group">
      <label>
       Name
      </label>
      <input type="text" name="name" class="form-control"
       ng-model="user.name" required>
      <span ng-show="errorname">{{errorname}}</span>
     </div>
     <div class="form-group">
      <label>
       Username
      </label>
      <input type="text" name="username" class="form-control"
       ng-model="user.username" required>
      <span ng-show="errorUsername">{{errorUsername}}</span>
     </div>
     <div class="form-group">
      <label>
       Email
      </label>
      <input type="email" name="email" class="form-control"
       ng-model="user.email" required>
      <span ng-show="erroremail">{{erroremail}}</span>
     </div>
     <div><span><button ng-click="reset()" class="btn btn-primary">
      reset
     </button> </span>
     <span> <button type="submit" ng-click="submitForm()" ng-disabled = {{erroremail}}
                        class="btn btn-primary">
      Submit
     </button></span>
     </div>
    
    </form>
   </div>
  </div>


  <script>
 var postApp = angular.module('postApp', []);
 postApp.controller('postController', function($scope, $http) {
  $scope.reset = function() {
   $scope.user.name = "Mahesh";
   $scope.user.username = "Parashar";
   $scope.user.email = "MaheshParashar@gmail.com";
  }
   $scope.user = {};
   $scope.submitForm = function() {
    $http( {
     method : 'POST',
     url : '/angular/studentForm.action',
     data : $scope.user, 
     headers : {
      'Content-Type' : 'application/x-www-form-urlencoded'
     }
    }).success(function(data){
     if (data.errors) {
      $scope.errorname = data.errors.name;
      $scope.errorUsername = data.errors.username;
      $scope.erroremail = data.errors.email;
     } else {
      $scope.message = data.message;
     }
    }).error(function(data,status){
     return false;
    });
    
   };
   
  });
</script>
 </body>
</html>

here is my Action class.

package com.skilrock.Action;

import com.opensymphony.xwork2.ActionSupport;

public class StudentController extends ActionSupport {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

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

private String firstName;
private String lastName;
private String email;

public String stdController() {

    System.out.println(firstName + lastName + email);

    return SUCCESS;
}

}

my reset button is working fine but when i click on submit button having an empty form then it call the given action class's method.. Other point is that each time either the form is empty or not that action method is not getting values of the form...

amit kumar
  • 121
  • 1
  • 10
  • what you want to do with form submit? validation? But according to code logic it will make one ajax call and its working fine – brk Nov 16 '15 at 09:58
  • yes, i want to validate my form. But with that i want to get the value of the form data when i submit the filled form. – amit kumar Nov 16 '15 at 10:04
  • but you are not validating the field . in submit you are just submitting the form even if it is empty – brk Nov 16 '15 at 10:06
  • i don't know how to validate the form using angularjs. – amit kumar Nov 16 '15 at 10:12
  • This may be useful http://stackoverflow.com/questions/18125777/angularjs-form-field-validation-using-javascript-function-without-directives. You can also valiadte the fields without angular, using simple js. Once the validations is successful, then you can make ajax call – brk Nov 16 '15 at 10:15
  • i have used required keyword with each field. Is it a type of validation.. – amit kumar Nov 16 '15 at 10:59
  • related: http://stackoverflow.com/a/28341208/1654265 – Andrea Ligios Nov 16 '15 at 11:10
  • @ Andrea Ligios : bro thanks for the link but i don't want to call any url if any one of my form data is empty. it is like my submit button will act like it is disabled until i will not enter all the data in that form. – amit kumar Nov 16 '15 at 11:32
  • @ user2181397 copy my jsp text and run it into (http://plnkr.co). you will see i have used validations. – amit kumar Nov 16 '15 at 11:39
  • @amitkumar I know, and you're right, it's better to do client side validation and prevent useless posts. But remember, you MUST do also server side validation, because your controls / validations / hidden or disabled buttons can be bypassed manipulating the DOM / HTML and then they're not enough if used *alone*. I was just pointing that, not saying you don't need client side validation – Andrea Ligios Nov 16 '15 at 13:41
  • Possible duplicate of [How to pass form data from angulajs jsp to struts 2 method](http://stackoverflow.com/questions/33687105/how-to-pass-form-data-from-angulajs-jsp-to-struts-2-method) – Aleksandr M Nov 17 '15 at 09:18
  • It's validating properly but still not able to get the values to my struts action class. – amit kumar Nov 20 '15 at 05:51

0 Answers0