The similarities and differences between the building apps that ASP.net hit JSF

Source: Internet
Author: User
Tags odbc mysql mysql connection string object model sort tostring mysql database connectionstrings
asp.net|js| program Ideally, ASP. NET and JSF Web pages should contain very little code and simply contain the necessary HTML and tags to generate the components of the page, and the event logic for one page resides in the code file. In asp.net, each Web page is associated with the. NET class file of a corresponding subclass page ASP.net class. Sometimes, these files are referred to as "code-behind" files. In JSF, each Web page has an associated support JavaBean class. Asp. NET's code-behind files and JSF support beans contain page properties, such as labels and input fields. JSF beans are written in Java, and ASP.net code-behind files can be used by any. NET language (such as vb.net or C #). The ASP.net Code-behind class is responsible for handling associated page events. This Java class that handles a component event does not have to be a page support JavaBean. We'll talk about it later. It is noteworthy that code-behind files that are separated in asp.net may be unnecessary-it is possible to implement event handling in the code of the page itself. In general, however, this is considered a bad coding practice, since it can lead to confusing HTML and code issues.

Here are two snapshots of our sample asp.net and JSF applications. They look slightly different because of the intrinsic characteristics of both components and I have no visual styles to match them. In two pages, a meeting room form is displayed, a View button to learn more about the room, and a reserve button to reserve the room.


Asp. NET program Snapshots

JSF program Snapshots

These components are added by dragging, and then customize their appearance and behavior by modifying a property panel. Of course, I can also customize these components by editing their tags in the HTML source code. Do not dwell on this. Below, we will focus on analyzing the code files behind these Web pages to further analyze the event code.

I think that displaying data in tabular form is a good start because it is common in applications. To do this, ASP. NET and JSF have not ignored this implementation, and both classes of displayed components provide built-in functionality to implement such effects as sorting and paging display. Prior to ASP.net 2.0, ASP. NET already provides a number of data display components, where the DataGrid component is the most widely used. In addition, a new GridView component was introduced into the ASP.net 2.0 release. In this case, I use this component because it adds some new and useful features. Asp. NET component utilizes Ado.net technology, which is also the data access technology for the entire. NET Framework-ado.net provides a robust object model for manipulating various types of data sources. For example, a DataSet object allows you to use data in a disconnected way. This means that in order to use the data, your application does not need to connect to the database continuously. A dataset also allows you to hide the details of the database behind its interface, allowing you to switch databases with minimal impact on other parts of the application. The JSF component I use in this example is a tabular component that is released with the Java Studio Creator. It uses a Dataprovider object-it allows you to use JDBC Rowset technology. JDBC rowset also allows you to use the database in an easy-to-use way when disconnected.

Each of these startup pages contains a single tab component that implements the page header. The page component can be accessed and modified in the event handler of a page. By simply double-clicking the component in the visual editor, you can hook up most of the events to a component. Also, this will take you to the code file where you can add code. As we have noted earlier, the ASP.net page class associated with a Web page is where your event code will reside. In JSF, it's not as simple as that. JSF events follow the observer design pattern. Objects that need to be notified of certain events have to register themselves as the corresponding listener for the event (listener). There are two types of events in JSF: The Value changed event and the action event. Typically, the Value changed event occurs in an action such as a list box selection, and the action event produces a user behavior such as a button click. Any Java class can respond to events in a Web form. However, the support bean for the JSF page is a convenient place to implement the event method, such as Sun Java Studio Creator, assuming you put the corresponding implementation here.

When a asp.net application is started, the configuration information located in a file Web.config is parsed and applied. Each asp.net application has a web.config. I use this file by storing the connection string in the MySQL database. Web.config is often used to store database connection strings to maintain this connection outside the code. The following shows a section of the code snippet for the Web.config file:

<connectionStrings>
<add name= "myConnectionString"
connectionstring= "Driver={mysql ODBC 3.51 Driver};server=localhost;database=test;uid=testuser;pwd=testpassword"
Providername= "SYSTEM.DATA.ODBC"/>
</connectionStrings>
JSF applications rely on a typical servlet-based Java application architecture. An ' Web-inf ' folder has subfolders and a file web.xml containing information about the application settings (very much like asp.net web.config). Note that the MySQL connection string here is not saved in the Web.xml file for this JSF application. Instead, Studio Creator automatically adds the connection to the server's configuration file, and our application accesses it through Jndi (Java naming and directory interfaces)-a common way to search for an Java service. Each JSF application's Web.xml file specifies a facesservlet-type Java Servlet. This facesservlet is responsible for configuring the application's settings corresponding to its JSF usage. In a JSF application, all requests are "flowing through" the Facesservlet. This control servlet controls the flow between application pages based on component event results. This is called the "Front Controller" design pattern. There is no indirect way to use this level in ASP.net, which controls page flow by itself, called the page Controller design pattern. This facesservlet knows how to route a request by referencing the page to the page map stored in a configuration file faces-config.xml. This configuration file also contains some JSF configuration information that we will emphasize.

In addition, ASP. NET and JSF pages have very similar life cycles. Earlier, we discussed how components generate events based on user behavior, and events are only part of this lifecycle. Look at a page's lifecycle from an advanced view perspective, the approximate process is that the user sends a request and initializes the page, and then the entire component tree of the page is read and the component's state is restored, the event is processed according to the user's behavior, and the component value is updated, and the resulting page is generated to the client. Of course, the actual situation may be more complicated than this, and there are some important phases (such as data validation) doped in these steps. A key difference between JSF and asp.net is how the component is generated to the user. Asp. NET components generate themselves on the page, and in JSF, components can generate themselves, but more often, they send a build agent to a specific renderer object (which can be used in a renderkit). Provides a different type of renderkit for each of the different kinds of descriptive media. Each JSF implementation will provide a default HTML Renderkit. This means that the same JSF component may be generated in different forms to a Web browser or wireless device-a fairly strong build capability by simply adding a different render object.

A handy time to add code without relying on any particular type of user request is when the page object is initialized. Asp. NET provides a Page_Load event-it is invoked when a user makes any type of page request. In the JSF page, you can use the Class builder method to implement the same page initialization logic. The following are the event-handling code for both applications that correspond to the actions that the user does on the data display component.

Conferencerooms.java Event code:
Public String btnviewreservations_action () {
Save the corresponding room ID and name of the selected row ...
Object id = getValue ("#{currentrow.value[' conference_rooms.room_id ']}");
Object name = GetValue ("#{currentrow.value[' Conference_rooms.room_name ']}");
Reservationssessionbean Resbean = (Reservationssessionbean) this.getbean ("Reservationssessionbean");
Resbean.setroomid (Id.tostring ());
Resbean.setroomname (Name.tostring ());
return "View";
}
Public String btnmakereservation_action () {
Object id = getValue ("#{currentrow.value[' conference_rooms.room_id ']}");
Reservationssessionbean Resbean = (Reservationssessionbean) this.getbean ("Reservationssessionbean");
Resbean.setroomid (Id.tostring ());
Return "reserve";
}
ConferenceRooms.cs event code
protected void Grdvwrooms_rowcommand (object sender, Gridviewcommandeventargs e)
{
if (E.commandname.equals ("Sort")) {
SortDirection SD;
if ((sortdirection) session["Sortrooms"]). Equals (sortdirection.ascending)) {
SD = sortdirection.descending;
}
else {
SD = sortdirection.ascending;
}
Session.add ("Sortrooms", SD);
This. Grdvwrooms.sort (e.commandargument.tostring (), SD);
}
Else
{
DataKey data = Grdvwrooms.datakeys[convert.toint32 (e.commandargument)];
Session.add ("Roomid", data. values["room_id"]. ToString ());
Session.add ("Roomname", data. values["Room_name"]. ToString ());
if (E.commandname.equals ("Reserve")) {
Server.Transfer ("reserve.aspx");
}
Else
{
if (E.commandname.equals ("View")) {
Server.Transfer ("roomreservations.aspx");
}
}
}
}
The code here is a little different, and the Java class is divided into two methods. These two Java events are action events. I incorporated all the event logic from this asp.net application into a single Rowcommand event and queried the parameters of the event to determine which user event occurred. In this particular event handler, I implemented the View and reserve button click on the corresponding event, as well as a sort request for the ASP.net component. Both applications save the Roomid and Roomname properties to make them easier to use later in the application. Please refer to the Conferencerooms.java and ConferenceRooms.cs files in the corresponding source code of this article. Remember that most of the code in these class files is generated by tools, and you can use the code overlap features provided by the editor to make the code more readable. In these event methods, you can immediately access the values of this page component to enable reading and modification. Asp. NET and JSF to save view state-by writing it to a hidden HTML field and passing it from one request to another. Of course, traditional ASP and JSP applications that use the same method to store the state are still available. For example, in ASP.net and JSF pages, the room ID and name are placed in the user's session so that it can be retrieved later.

Note that in the above event code, the ASP. NET file calls a method "Server.Transfer" implementation to pass control to another page. However, in the JSF event code, you will not find a direct reference to another page, but only return a string of "view" and "reserve." This is because, in JSF, we deal with page flow problems through the Faces-config profile we discussed earlier, and any Java developer who has used the open source framework struts should be familiar with this. There are two entries in the Faces-config.xml file of the conferencerooms.jsp file-the string "View" and "reserve". At run time, the application uses these strings to query the page address. The following is a navigation rule in the form of XML found in the Faces-config file of the conferencerooms.jsp page. I created these mappings using a visual designer in the Java Studio Creator.

<navigation-rule>
<from-view-id>/conferencerooms.jsp </from-view-id>
<navigation-case>
<from-outcome> View </from-outcome>
<to-view-id>/roomreservations.jsp </to-view-id>
</navigation-case>
<navigation-case>
<from-outcome> Reserve </from-outcome>
<to-view-id>/reserveroom.jsp </to-view-id>
</navigation-case>
</navigation-rule>
  
The visual designer shows navigation rules in the Java Studio Creator

This faces-config file is also where you enumerate all the Java beans that are associated with your JSP paging file. In fact, you can configure any Java object that your application needs to refer to in this file-these Java objects are referred to as ' managed beans '. All JSF support beans will be listed as managed beans in this configuration file, but any other objects you need can also be found here. A managed bean contains the fully qualified class name, the use of the bean (which is stored at the Request,session or application level of the application), and the name to use when referencing the bean. In the following XML, we enumerate the support beans for the initial web page, and the entry of an EJB named Reservationssessionbean (which we use to share the data in the application):

<managed-bean>
<managed-bean-name> Reservationssessionbean </managed-bean-name>
<managed-bean-class> webreservations. Reservationssessionbean </managed-bean-class>
<managed-bean-scope> Session </managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name> conferencerooms </managed-bean-name>
<managed-bean-class> webreservations. Conferencerooms </managed-bean-class>
<managed-bean-scope> Request </managed-bean-scope>
</managed-bean>
Let's go back to the previous application, and our request to view the reservation information leads us to the asp.net version of RoomReservations.cs.aspx (corresponding to the JSF version of roomreservations.jsp). In the related code files (RoomReservations.cs and Roomreservations.java), the event code retrieves the room ID stored in the previous page and uses it to filter the second table (which lists the booking for that room). The label above the data table is modified to refer to the corresponding room name.


asp.net
>
Jsf

A Delete button deletes each row of data, both of which allow you to easily modify the data in the table. Finally, each form contains a button-it directs you to another form to add a new reservation to the room. These forms appear in the figure below. Note that the calendar components that are implemented by Java Studio creator and Visual Studio in the diagram differ in their display, but achieve the same purpose.


asp.net

Jsf

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.