Programming with Designers in Microsoft Visual J# .NET
Introduction
What Is Microsoft Visual J# .NET?
Microsoft Visual J# .NET™ is a development tool that developers who are familiar with the Java-language syntax can use to build applications and services on the .NET Framework. It integrates the Java-language syntax into the Visual Studio .NET shell. Visual J# .NET also supports most of the functionality found in Microsoft Visual J++ 6.0, including Microsoft extensions. Microsoft Visual J# .NET is not a tool for developing applications intended to run on a Java Virtual Machine. Applications and services built with Visual J# .NET will run only on the .NET Framework. Visual J# .NET has been independently developed by Microsoft. It is neither endorsed nor approved by Sun Microsystems, Inc.
Since Visual J# .NET integrates with the Visual Studio .NET integrated development environment (IDE), J# programmers can use Visual Studio .NET designers to create XML Web services, Web Forms, and Windows Forms applications. This document introduces rapid application development using Visual J# .NET.
Shared Integrated Development Environment
Microsoft Visual Studio .NET is the shared integrated development environment (IDE) for all the languages within it. It was designed to help developers build their solutions faster and with all of the available tools regardless of which language you use in the IDE. With Visual J# .NET, the Java-language joins the other languages supported in Visual Studio .NET. For an introduction to the features of Visual Studio .NET, see the Microsoft Visual Studio .NET documentation.
Designers in Visual J# .NET
Web Forms Designer
Today's developers need an intuitive way to create complex Web pages. Visual Studio .NET includes the shared Visual Studio Web Forms Designer, a graphical way to develop HTML pages, Active Server Pages (ASP), and ASP.NET Web Forms without delving into HTML or script code.
Windows Forms Designer
Windows Forms provide a concise, object-oriented, extensible set of classes that enable developers to quickly develop Windows-based applications. Using the Windows Forms Designer, developers can rapidly develop solutions for use in Windows-based applications. By simply adding a new form to a project, the developer has a basis from which to quickly create applications with rich, intuitive user interfaces (UI). Once a Windows Form has been added to a Visual Studio .NET solution, the developer can set form properties, add controls from the Toolbox, and write code behind the form.
Component Designer
In the same way that form designers enable the rapid creation of client applications, Visual Studio .NET provides a way to build server-side components quickly and graphically. The Component Designer applies the concepts of rapid application development (RAD), form-based programming to building middle-tier objects; a visual way for building non-visual objects. Instead of writing lots of server-based code, developers can drag and drop server components to a design surface that will run on the server. Just double-click a server component on the Component Designer and the code for that object is opened.
XML Designer
The XML Designer provides intuitive tools for working with XML and XML Schema Definition (XSD) files. Within the designer, there are three views: one for creating and editing XSD schemas, one for structured editing of XML data files, and one for editing XML source code. For more information on XML Designer, see the Visual Studio .NET documentation.
Designers and Components
What Is a Designer?
Generally, a designer refers to any .NET object responsible for managing the design time behavior of a component or family of components. But sometimes "designer" refers more globally to the design time UI in Microsoft
All of the designers that are included in the .NET Framework SDK derive from the default implementation in System.ComponentModel.Design.ComponentDesigner. Any object that implements IComponent (which usually means it derives from Component) will automatically get ComponentDesigner as its designer.
In addition to modifying the sets of Properties, Attributes, and Events exposed by the component being designed, most designers allow creating or modifying the design time UI of the component being designed.
What Is a Designable Component?
Microsoft .NET components are written in managed code and built upon the Common Language Runtime (CLR). A class is a component when it conforms to a standard for component interaction. This standard is provided through the System.ComponentModel.IComponent interface. Any class that implements the IComponent interface is a component. IComponent allows a component to keep track of design-time information, such as its container component or its name, or to access services that the designer may expose. System.ComponentModel.Component is a default implementation of IComponent and any class that inherits from Component is a designable component that can be manipulated at design time using an associated designer.
How Does a Component Get a Designer?
Every component can have one or more designers associated with it. Designers are associated with components using the System.ComponentModel.DesignerAttribute attribute. This attribute's constructor can take an assembly-qualified string type name, or an actual type reference.
At design time, the
Persistence Through Code
Windows Forms and other
The mechanism used to generate and output source code for persistence is known as Code Document Object Model (CodeDOM). The CodeDOM provides classes, interfaces, and architecture that can be used to represent the structure of source code, independent of a specific programming language. The CodeDOM provides the ability to output each representation as source code in a programming language that the CodeDOM supports, which can be selected at run time.
Designers and Java-language Support
Microsoft
The Microsoft
Windows Applications in Microsoft Visual J# .NET
Using Microsoft
Let's create a simple Windows-based application:
- From File, select New, then select Project.
- Click Windows Application under
Visual J# Projects in the New Project dialog box.
Figure 1. New Windows application
The Windows Application project template adds a Form class to the new project. This class extends System.Windows.Forms.Form. When the project is created, this Form is displayed in design view by default. You can drag and drop Windows Forms controls from the toolbox to the form. Let us add a button control and a label control. You can change the properties of the form, and the controls, using the Properties browser window. For example, change the text property of the button to read “Click Here”.
Figure 2. Using the Properties browser to change the property of a control
Double-clicking a control takes you to the code view for the Form, and the cursor is positioned at an appropriate event-handling method for the control. For example, double clicking the button takes you to the Button1_Click method, where you can add event-handling code for the click event on Button1. Let's add some code to change the text property of the label: type in the following:
Label1.set_Text(“Button clicked”);
Figure 3. Adding event handling code
Press F5 to run the project. Click the button on the form. The click event results in the code in Button1_Click being executed, and the label text is changed to “Button clicked”.
You can add any event handling code that you want for the controls you add to the Form. For more information about syntax for programming with the .NET Framework classes, refer to the online help for Visual J# .NET
XML Web Services in Microsoft J# .NET
An XML Web service is a way to access server functionality remotely. Using XML Web services, businesses can expose programmatic interfaces to their data or business logic, which in turn can be obtained and manipulated by client and server applications. XML Web services enable the exchange of data in client/server or server/server scenarios, using standards like HTTP and XML messaging to move data across firewalls. XML Web services are not tied to a particular component technology or object-calling convention. As a result, programs written in any language, using any component model, and running on any operating system, can access XML Web services.
You can use Microsoft
- From File, select New, then select Project.
- Click ASP.NET Web Service under
Visual J# Projects in the New Project dialog box.
Figure 4. New ASP.NET Web service
A new Web service project is created on your Web server, and the project template adds a file called Service1.asmx to your project, and a code-behind file, Service1.asmx.jsl. Using the designer, you can add Data and Server components to your Web service, and add event-handling code for them. The designer generates code for the components that you add, using the CodeDOM mechanism described earlier. The code-behind file also includes an example for a Web service method. You can switch to code view to see the code in the code-behind file.
Create a simple HelloWorld Web service by uncommenting the commented example. Notice the WebMethodAttribute declaration on the method:
/** @attribute System.Web.Services.WebMethodAttribute() */ public System.String HelloWorld() { return "Hello World"; }
This attribute indicates that the HelloWorld method is to be exposed as a Web method. Press F5 to run test the application. This builds the project and runs Microsoft Internet Explorer with the URL of the Web service, which is http://localhost/WebService/Service1.asmx for the example above.
The deep integration of ASP.NET Web services with Microsoft Internet Information Services (IIS) ensures that you can see and test the Web services that you have created with little effort. All the public Web methods are listed in the page returned by the URL above — in our case a method called HelloWorld. Click this method's name to see a test page for it where you can invoke the method. For methods taking input parameters, there will be one input box for each parameter to the method. The service description of the Web service, in WSDL format, can be seen by invoking the URL with a “?wsdl” appended to it, that is,http://localhost/WebService/Service1.asmx?wsdl in our example.
Click the Invoke button on the test page. This results in a page being opened with the result of invoking the method – it returns an XML document containing the string “Hello World”.
The Web service you have created above can be consumed in any application, say a Windows Forms application, or a Web Forms application, in any language. You can create more complex Web services which take parameters and return data. For example, you can implement a search function as a Web service. The function accepts a keyword and returns the search results. For a more complex example, see the Microsoft Visual J# .NET Web services sample accompanying the Visual J# .NET documentation.
Consuming Web Services
Now that we have created a Web service, let's try consuming the Web service programmatically in an application. Microsoft Visual J# .NET lets you do this easily.
Open the Windows Forms application that we created earlier. We will add the Web service reference to the project, invoke the method by clicking the button, and display the returned string in the label.
Let us add a Web reference to the project.
- Right-click on the References node in the Solution Explorer window and select Add Web Reference.
Figure 5. Adding Web Reference
- In the Add Web Reference dialog box, type the URL of the service in the Address box: http://localhost/webservice/Service1.asmx.
The available Web services are listed in the box on the right side and the test page content on the left side. - Click Add Reference to complete the action.
Figure 6. Add Web Reference dialog box
When Add Reference is clicked, Microsoft
Let us add code to invoke the Web service method. Go to the button1_Click method, and add the following code:
WindowsApplication1.localhost.Service1 s = new WindowsApplication1.localhost.Service1(); label1.set_Text(s.HelloWorld());
Note: The proxy for the Web service is created within the default package name of the current project, appended with the server name. (WindowsApplication1.localhost in this case). Hence the fully qualified name of the Service class is WindowsApplication1.localhost.Service1
Press F5 to run the application. On the Form, click the button and the label text is changed to “Hello World”. It's that simple!
Web Forms Application in Microsoft Visual J# .NET
Web Forms are an ASP.NET-based technology that you can use to create programmable Web pages. They can present information, using any markup language, to the user in any browser, and use code on the server to implement application logic. Microsoft
Web Forms consists of two components, the visual elements and the code. With Microsoft Visual J# .NET, the visual elements are created in a .aspx file that acts as a container for HTML elements and Web Forms controls. The code is stored in a separate file, within a .aspx.jsl file. You can use RAD development techniques using the Web Forms designer and write programs with the event-based programming model.
Although you create Web Forms from separate components, they form a unit. When the Web Form is compiled, ASP.NET parses the page and its code, generates a new class dynamically, and then compiles the new class. The dynamically generated class is derived from the ASP.NET Page class, but is extended with controls, your code, and the static HTML text in the .aspx file. The dynamic code generation, and compilation of the resultant code, is done using the Microsoft
This new derived Page class becomes a single executable file that is executed on the server whenever the Web Forms page is requested. At run time, the Page class processes incoming requests and responds by dynamically creating HTML and streaming it back to the browser.
Let us create a simple Web Forms application using Microsoft
- From File, select Project and choose ASP.NET Web Application project under Visual J# Projects in the New Project dialog box.
Figure 7. New ASP.NET Web Application
The project adds a template file WebForm1.aspx to the project—this is the default file that is opened in Design view. The default template contains a Form control. A code-behind file is created, WebForm1.aspx.jsl. This has one class, WebForm1, which extends System.Web.UI.Page. The designer will generate code into this file based on the controls that we add to the design surface. This code generation is done using the Microsoft Visual J# .NET CodeDOM.
You can add either Web Forms controls or HTML controls to the design surface. The Web Forms controls are server-side controls, for which event handler code can be added in the code-behind file. The server-side code will be compiled into an assembly that will render the controls as HTML when a client requests the Web Forms page. HTML controls exist only on the client side; hence our code-behind cannot be used to manipulate these controls.
Let us add a Web Forms button and a Web Forms label to the page. Use the property browser to change the button's text property to “Click here”.
Figure 8. Web controls in Web Forms designer
Now we can add event handling code for clicking on the button. Double-click the button in design view to take you to the method Button1_Click in class WebForm1. This method will be invoked when the button is clicked at run time. Add the following code to the method:
private void Button1_Click (System.Object sender, System.EventArgs e) { Label1.set_Text("Button clicked"); }
Press F5 to run the application. The project is built, and Internet Explorer is opened with the URL for the application, http://localhost/WebApplication/WebForm1.aspx in our example. Click the button on the page and see the label's text change to “Button clicked”.
Figure 9. Web application run in browser
When the project is built, the code-behind file is compiled to create WebApplication.dll. This assembly contains the event-handling code we added. When the page is accessed using Internet Explorer, ASP.NET processes WebForm1.aspx. A temporary file is generated for the page, containing a class WebForm1_aspx, which extends our code-behind class WebForm1. Code generation is done using the Microsoft Visual J# .NET CodeDOM mechanism. This generated code is the glue between our event-handling code and the rendering of the page. ASP.NET compiles the generated code using the Microsoft Visual J# compiler, again through the CodeDOM mechanism. This assembly renders the HTML for the form that is seen in the client browser.
Now let us try to consume the Web service that we created earlier, in section X, in this Web Forms application.
Add a Web reference to the Web service that you created earlier by using the Add Web Reference dialog box. Now add another Web Form label control (Label2) to the Form. Add the following code to Button1_Click to invoke the Web service method HelloWorld and set the returned string value to the text property of Label2.
WebApplication.localhost.Service1 s = new WebApplication.localhost.Service1(); Label2.set_Text(s.HelloWorld());
Press F5 to run the application. Click the button and see the Label2 text change to “Hello World”.
Figure 10. Result of invoking a Web service from a Web application
Deploying Web Applications Created with Visual J# .NET
Both Web services and Web Forms applications created using Microsoft
Microsoft, Visual Studio, Visual Basic, Visual J# .NET, Visual C#, Visual J++, J/Direct, ActiveX, FrontPage and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. References in this document to the JDK class libraries and the JDK packages are to specification level 1.1.4. References to the Java-language are to specification level 2.0. This product is not approved or endorsed by Sun Microsystems, Inc.
Tidak ada komentar:
Posting Komentar