send and receive htttp post data from angular 2 in struts

后端 未结 1 1616
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 03:02

i am trying to send http post data from angular 4 code to struts action. Struts action is getting called but not able to receive the data. Giving me an error as \'java.lang.Clas

1条回答
  •  情话喂你
    2021-01-29 03:36

    If you want to get data as string then you should try like:

        import { Component, OnInit } from '@angular/core';
    import { Http, Headers, RequestOptions } from '@angular/http';
    import 'rxjs/add/operator/toPromise';
    import { Observable } from 'rxjs/Observable';
    @Component({
      selector: 'app-root',
      template: '

    Dukes

    ', // templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { title = 'here beta'; dukes = [{ name: "offline", age: 2 }]; data = "data=\"{title: 'foo',body: 'bar',userId: 1};\""; headers: Headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }); options: RequestOptions = new RequestOptions({ headers: this.headers }); constructor(private http: Http) { } ngOnInit() { let options = new RequestOptions(); options.headers = new Headers(); options.headers.append('Content-Type', 'application/x-www-form-urlencoded'); const req = this.http.post('http://localhost:8080/SampleProject/getTutorial', this.data, options) .subscribe( res => { console.log(res); }, err => { console.log("Error occured"); } );

    But I think you want to set all of them them to different properties of action by json, if so , define setter of title, body and userId in your action and continue with Struts JSON Plugin - JSON interceptor

    ANOTHER EXAMPLE

    import { Component, OnInit } from '@angular/core';
    import { Http, Headers, RequestOptions } from '@angular/http';
    import 'rxjs/add/operator/toPromise';
    import { Observable } from 'rxjs/Observable';
    @Component({
      selector: 'app-root',
      template: '

    Dukes

    ', // templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { title = 'here beta'; dukes = [{ name: "offline", age: 2 }]; data = { "title": "foo", "body": "bar", "userId": 1 }; headers: Headers = new Headers({ 'Content-Type': 'application/json' }); options: RequestOptions = new RequestOptions({ headers: this.headers }); constructor(private http: Http) { } ngOnInit() { let options = new RequestOptions(); options.headers = new Headers(); options.headers.append('Content-Type', 'application/json'); const req = this.http.post('http://localhost:8080/SampleProject/getTutorial', JSON.stringify(this.data)/*CONVERTS DATA TO STRING*/, options) .subscribe( res => { console.log(res); }, err => { console.log("Error occured"); } );

        public class SampleAction {
       private String title;
     public String getTitle() {
                return title;
            }
    
            public void setTitle(String title) {
                this.title = title;
            }
       private String body;
     public String getBody() {
                return body;
            }
    
            public void setBody(String body) {
                this.body= body;
            }
       private int userId;
     public String getUserId() {
                return userId;
            }
    
            public void setUserId(int userId) {
                this.userId= userId;
            }
    
    
        public int execute()
         {  
            try{
             actorSeqID = 3;
             System.out.println(data+"--");
            }catch(Exception e){
                e.printStackTrace();
            }
    
    
          return actorSeqID;
         }
    

    
            
                
            
    
    

    0 讨论(0)
提交回复
热议问题