【问题标题】:Simple Web Service adding two numbers添加两个数字的简单 Web 服务
【发布时间】:2011-02-12 13:25:31
【问题描述】:

我创建了一个简单的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);
}
}

我创建了一个非常简单的 gui,它允许用户输入 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


    【解决方案1】:

    您应该将 add 声明为 web 方法。 尝试以下:

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

    【讨论】:

      【解决方案2】:

      我的错!我忘了启动应用服务器

      【讨论】:

        猜你喜欢
        • 2012-10-27
        • 2010-09-16
        • 1970-01-01
        • 2013-01-08
        • 2016-05-19
        • 2014-01-23
        • 1970-01-01
        • 2014-10-22
        • 1970-01-01
        相关资源
        最近更新 更多