栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从Struts中的angular 2发送和接收htttp post数据

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从Struts中的angular 2发送和接收htttp post数据

如果要以

data
字符串形式获取,则应尝试如下操作:

    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: '<h1>Dukes</h1>',  // 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-urlenpred' });  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-urlenpred');    const req = this.http.post('http://localhost:8080/SampleProject/getTutorial', this.data, options)      .subscribe(      res => {        console.log(res);      },      err => {        console.log("Error occured");      }      );

但我认为您想通过json将它们全部设置为不同的动作属性,如果是,请定义的setter

title
body
然后
userId
在您的动作中继续使用Struts JSON插件-
JSON拦截器

另一个例子

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: '<h1>Dukes</h1>',  // 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), 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;     }

<package name="default" namespace="/" extends="struts-default,json-default">        <action name="getTutorial" method="execute" > <interceptor-ref name="json"></interceptor-ref>        </action></package>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/440728.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号