我正在尝试将 http post 数据从 Angular 4 代码发送到 struts 操作。 Struts 操作正在被调用,但无法接收数据。给我一个错误“java.lang.ClassCastException:java.lang.Integer无法转换为java.lang.String”
建议会有很大帮助
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', this.data, options)
.subscribe(
res => {
console.log(res);
},
err => {
console.log("Error occured");
}
);
struts Action 类是
public class SampleAction {
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int execute()
{
try{
actorSeqID = 3;
System.out.println(data+"--");
}catch(Exception e){
e.printStackTrace();
}
return "success";
}
最佳答案
如果您想以字符串形式获取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-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");
}
);
但是我认为你想通过json将它们全部设置为action的不同属性,如果是这样,请定义title
,body
和userId的setter
在您的操作中并继续 Struts JSON Plugin - JSON interceptor
另一个例子
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)/*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;
}
<小时/>
<package name="default" namespace="/" extends="struts-default,json-default">
<action name="getTutorial" method="execute" class="SampleAction">
<interceptor-ref name="json"></interceptor-ref>
</action>
</package>
关于java - 在struts中从Angular 2发送和接收http post数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45954402/