添加两个数字的简单 Web 服务

问题描述 投票:0回答:2

我创建了一个简单的Webservice功能,如下所示;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ws;
import javax.jws.WebService;
/**
 *
 * @author Joe
 */
@WebService()
public class Add2Int {
public int add(int a, int b) {
    return (a+b);
}
}

我创建了一个非常简单的图形用户界面,允许用户输入 2 个数字,并且应该输出结果,但这不起作用?我在没有 gui 的情况下尝试了它,它可以工作,但是当我构建 gui 时它不起作用?这是我那方面的代码

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package myjavawsclient;
//import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Joe
 */
public class Calculator extends JFrame implements FocusListener {
    JTextField value1 = new JTextField("", 5);
    JLabel plus = new JLabel("+");
    JTextField value2 = new JTextField("",5);
    JLabel equals = new JLabel("=");
    JTextField sum = new JTextField("", 5);

    public Calculator() {
        super("The Calculator");
        setSize(350,90);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
        setLayout(flow);
        // add the listners
        value1.addFocusListener(this);
        value2.addFocusListener(this);
        // set up sum field
        sum.setEditable(true);
        //add componets
        add(value1);
        add(plus);
        add(value2);
        add(equals);
        add(sum);
        setVisible(true);
    }

    public void focusGained(FocusEvent event){
        try { // Call Web Service Operation
            ws.Add2IntService service = new ws.Add2IntService();
            ws.Add2Int port = service.getAdd2IntPort();
            // TODO initialize WS operation arguments here
            int result = 0;
            int result2 = 0;
            result = Integer.parseInt(value1.getText());
            result2 = Integer.parseInt(value2.getText());

            int total = port.add(result, result2);
            sum.setText("" +total);

            //float plusTotal = Float.parseFloat(value1.getText()) +
                Float.parseFloat(value2.getText());

        } catch (Exception ex) {
            // TODO handle custom exceptions here
            //value1.setText("0");
            //value2.setText("0");
            //sum.setText("0");
        }
    }

    public void focusLost(FocusEvent event){
      focusGained(event);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // TODO code application logic here

        Calculator frame = new Calculator();
    }
}

我没有收到任何错误,我只是没有从 2 个数字中得到任何结果,例如 1+1=2,但在我的应用程序中,它允许用户输入 1 + 1 = ?但问号处什么也没有显示。

我想知道是否有人可以帮我解决这个问题。哦,我正在使用带有 WSDL 的 NetBeans 和 GlassFish 应用服务器

java jax-ws
2个回答
0
投票

您应该将 add 声明为网络方法。 尝试以下操作:

   @WebMethod public int add(int a, int b){
    return (a+b);
    }

0
投票

我的错!我忘记启动应用程序服务器

© www.soinside.com 2019 - 2024. All rights reserved.