小编Pat*_*ick的帖子

为什么ByteArrayInputStream不返回预期的结果?

我试图通过telnet与Windows服务器中的应用程序进行交互,所以我使用的是TelnetClient()方法.我可以使用System.in.read()进行交互(发送命令和检索结果),但是我希望这个程序能够在不使用任何键盘输入的情况下自动运行.所以,我的问题是,为什么System.in.read()工作,但ByteArrayInputStream不工作?

到目前为止这是我的代码:

public class telnetExample2 implements Runnable, TelnetNotificationHandler{
 static TelnetClient tc = null;
 public static void main (String args[]) throws IOException, InterruptedException{
  tc = new TelnetClient();
   while (true){
    try{
     tc.connect("192.168.1.13", 8999);
     }
     catch (SocketException ex){
      Logger.getLogger(telnetExample2.class.getName()).log(Level.SEVERE, null,ex);
     }
    Thread reader = new Thread(new telnetExample2());
    tc.registerNotifHandler(new telnetExample2());
    String command = "getversion"; //this is the command i would like to write
    OutputStream os = tc.getOutputStream();
    InputStream is = new ByteArrayInputStream(command.getBytes("UTF-8")); //i'm using UTF-8 charset encoding here
    byte[] buff = new byte[1024];
    int …
Run Code Online (Sandbox Code Playgroud)

java telnet

2
推荐指数
1
解决办法
1万
查看次数

为什么发布最终字段安全?

我目前正在阅读Brian Goetz的Java Concurrency in Practice.在第51页.在其中一个脚注中,他说:

虽然它可能似乎在构造函数中设置字段的值写入这些字段中的第一个值,因此不存在"老"值看作为过时的值,子类的构造函数运行之前Object构造函数首先写入默认值到所有领域.因此,可以将字段的默认值视为陈旧值.

所以,最终领域的概念现在还不清楚.考虑样本类:

public class MyClass{
    private final MyImmutableClass mic;

    public MyClass(){
        mic = MyImmutableClass.empty();
    }
}
Run Code Online (Sandbox Code Playgroud)

根据上面的脚注,mic字段被分配两次,一次由Object构造函数指定,一次由构造MyClass函数本身指定.现在,假设我们MyClass不安全地发布了一个对象(例如通过public字段):

public final MyClass mc;
Run Code Online (Sandbox Code Playgroud)

谁保证mc一致状态下的任何线程都能始终观察到?为什么有些线程不会意外地观察到默认值?

据我所知,该final字段本身仅保证在对象构造之后不能分配引用.如果我们宣布mc挥发性,那将是明确的.读取该字段的任何线程都应该直接从内存中读取它.禁止从缓存中读取它.

UPD:出版示例:

public static void main(String[] args){
    class MyRunnable implements Runnable(){
        private SomeClass sc;
        public MyRunnable(SomeClass sc){
            this.sc = sc;
        }
        public void run(){
            //do some with sc
        }
    } …
Run Code Online (Sandbox Code Playgroud)

java multithreading final

2
推荐指数
1
解决办法
165
查看次数

使用checbox删除多行

我可以通过选中一个框来删除单行.当我尝试选择多个框时,当前复选框是唯一删除的行,而前面选中的复选框.以下是我的代码.请帮忙

//Service

import {Injectable} from '@angular/core';
import {Observable} from "RxJS/Rx";
import {Http, Response, Headers} from '@angular/http';

@Injectable()

export class FoodService {    

    constructor(private http:Http) {

    }

    RemoveFood(id) {
        return this.http.delete('http://example.com ' + id)
        .map((response:Response) => response.json())
    }
}
Run Code Online (Sandbox Code Playgroud)

//食物模块

export class Food (
    public count: number;
    public price: number;
    public location: string;
    public type: string;
    public selected: string;    
) { }
Run Code Online (Sandbox Code Playgroud)

//食物桌

<tr *ngFor="let food of Food">

    <td><input #{{food.count}} [(ngModel)]="food.selected" type="checkbox" (change)="checkbox(food)"></td>
    <td>{{food.count}}</td>
    <td>{{food.price}}</td>
    <td>{{food.location}}</td>
    <td>{{food.type}}</td>

</tr>
Run Code Online (Sandbox Code Playgroud)

//食物成分

export class FoodComponent …
Run Code Online (Sandbox Code Playgroud)

angular

2
推荐指数
1
解决办法
2802
查看次数

Java API - 将<int []>列出为JSON错误

我的REST API应该返回一个对象列表

public List<MyObject> getMyObjects() { ... }
Run Code Online (Sandbox Code Playgroud)

为MyObject

public class MyObject {

    private int id;
    private String name;
    private List<int[]> coordinates;

    // getters
    // setters
}
Run Code Online (Sandbox Code Playgroud)

对此方法的调用有效,并且没有错误.

问题是关于坐标列表.生成JSON时,我的int []列表被转换为此

"coordinates":["[I@409cd27c","[I@1a552b8c","[I@1af3f13d","[I@5e12856b","[I@78bba3e7", //...
Run Code Online (Sandbox Code Playgroud)

如何使JSON转换与此列表一起使用?其他变量一切正常.

java rest json jax-rs

1
推荐指数
1
解决办法
71
查看次数

Spring MVC @Controller 拦截自己的请求

想象一下我们有一个这样的控制器:

@RestController
@RequestMapping("/{parameter}")
public class MyController {

    @ExceptionHandler(SomeException.class)
    public Object handleSomeException() { /* handle */ }

    @RequestMapping("/something")
    public Object handleSomething(@PathVariable("parameter") String parameter) {
        /* handle */
    }

    @RequestMapping("/somethingElse")
    public Object handleSomethingElse(@PathVariable("parameter") String parameter) {
        /* handle */
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,如何以与@ExceptionHandler工作类似的方式为这个特定控制器实现一些常见的前\后处理?例如,我想在控制器中有一个方法,它在处理程序方法之前接收请求,但只对这个特定控制器的请求。

我知道RequestBodyAdviceResponseBodyAdvice接口,但想要控制器本地的东西。

作为使用示例 - 我想parameter在每个处理程序之前对公共变量进行一些验证。

java spring spring-mvc spring-restcontroller

1
推荐指数
1
解决办法
4047
查看次数

ngModel 只接受 Angular 2 中的一个字符更改

下图显示了我的表格,其中数据是动态设置的。例如,它可以有 2,3,4 ... 列。

在此处输入图片说明

我的 html 代码如下:<thead>包含标题值,并tbody包含可以修改的值。

<table class="table-conf">
    <thead>
       <tr>
           <th *ngFor="let data of jsonText[0]" style="text-align: center;">{{data}}</th>
       </tr>
    </thead>
    <tbody>
       <tr *ngFor="let data of jsonText; let i=index">
         <ng-container *ngIf="i!=0">
             <td class="padding-table" *ngFor="let dt of data; let j=index">
                 <input style="text-align: center;" [(ngModel)]="jsonText[i][j]">
             </td>
         </ng-container>
       </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

以下是jsonText我迭代的变量示例:

jsonText Array(6)
     0: (2) ["Task", "Hours per Day"]
     1: (2) ["Work", 11]
     2: (2) ["Eat", 2]
     3: (2) ["Commute", 2]
     4: (2) ["Watch TV", 2]
     5: …
Run Code Online (Sandbox Code Playgroud)

materialize angular-ngmodel angular

1
推荐指数
1
解决办法
1471
查看次数

WPF MVVM:设置选项卡视图的 DataContext

我遇到了一种奇怪的绑定行为,这里描述。我做了很多故障排除,我得出的结论是,最可能的问题在于我如何设置DataContext每个选项卡的视图。

我有一个TabControlItemsSource绑定到列表ViewModels

MainView:
<TabControl ItemsSource="{Binding TabList}">
    <TabControl.Resources>
        <DataTemplate DataType="{x:Type vm:Tab1ViewModel}">
            <v:Tab1 />
        </DataTemplate>
    </TabControl.Resources>
...
</TabControl>

MainViewModel:
public ObservableCollection<TabViewModelBase> TabList { get; set; }
public MainViewModel()
{
    this.TabList = new ObservableCollection<TabViewModelBase>();

    // Tab1ViewModel is derived from TabViewModelBase
    this.TabList.Add(new Tab1ViewModel()); 
}
Run Code Online (Sandbox Code Playgroud)

所以,现在MainViewModel有一个列表TabViewModelBase,我相信这是正确的 MVVM 方法。视图 ( Tab1)TabViewModelBase是使用 定义的DataTemplate

这就是问题所在:

Tab1:
<UserControl.Resources>
    <vm:Tab1ViewModel x:Key="VM" />
</UserControl.Resources>
<UserControl.DataContext>
    <StaticResourceExtension ResourceKey="VM" />
</UserControl.DataContext>
Run Code Online (Sandbox Code Playgroud)

我想大多数人也会这样做,但是......这种方法有一些 …

wpf datacontext tabcontrol datatemplate mvvm

0
推荐指数
1
解决办法
2936
查看次数