Use jbuilder9 to develop struts instances

Source: Internet
Author: User
This article will introduce how to use Struts framework to develop Web applications in the integrated development environment of JBuilder 9. The jbuilder9 mentioned here refers to the JBuilder 9 developer and enterprise versions.

I. JBuilder's support for struts

JBuilder provides graphical user interfaces to simplify the editing and setting of component File struts-config.xml. In addition, JBuilder provides four web pages in the object gallery dialog box to simplify the struts development process.

Struts converter Wizard:
The specified HTML or common JSP file automatically generates a JSP file that uses the struts tag libraries.

Actionform Wizard:
You can import JSP files to generate inherited from actionform, or custom properties (property) generate inherited from actionform and so on, after the class is generated, you can choose whether to automatically complete the struts-config.xml corresponding settings.

Action Wizard:
Can generate inherited from action and so on, set the JSP file with the effective range (scope) and automatically complete the struts-config.xml corresponding settings.

JSP from actionform Wizard:
The JSP file using struts tag libraries can be automatically generated by the specified actionform.

In addition to the above genie, JBuilder can help developers automatically set the Web. xml deployment description file so that jsp can use the struts custom tag libraries.

Ii. Use JBuilder to develop struts instances

Here is an example of developing a simple web application to help you understand the Struts framework. This web application allows users to enter two independent strings and connect them to the web page. The processing logic is too simple. In this instance, we intend to directly complete the two string connection actions in the action that assumes the Controller role. Therefore, we did not use the JavaBean or class that assumes the model role to process string connections.

Step 1: create a web application

First click to select the main menu file of the integrated development environment of JBuilder | new project... Create a project named strutsdemo. File | new... Activate the object gallery dialog box, and click Select Web Application Wizard on the Web page to create a web application.

The Web Application Wizard prepares the deployment description file web. xml required to generate war for this web application. War is a standard file format used to simplify Web Application Deployment complexity in J2EE specifications. We name this web application as demo, and a demo will be generated later when this project file is compiled. war files are easy to deploy. Because struts tag libraries is used in this web application, select struts 1.0 in JSP/servlet frameworks, and JBuilder automatically sets the web. XML.

Step 2: create a custom actionform class

Struts framework does not limit the development sequence of MVC. Because we have not prepared HTML or JSP that can be entered with two strings, we should first create the actionform class for storing information, the action class that plays the Controller role is created, and the actionform class generates JSP files. You can adjust the order of the files to adapt to the actual case. Click actionformwizard in the web page of the select object gallery dialog box to generate a custom actionform class. actionformwizard can be divided into three steps: Step 1, the wizard automatically detects the web application demo that has been created previously, we changed the custom actionform name from the default untitled1actionform to myactionform, and then pressed the next button to go to step 2. Step 2 Add the fields to be displayed and entered on the future JSP page to the field definition list box as required. If the full-time staff in charge of the webpage artist has prepared the JSP page for input, you can also press add from JSP... Button to import directly. JBuilder will parse this JSP file to generate the field definition table. In this example, because the JSP file is not prepared for input, you must add the required fields. For this web application, we have prepared three fields in advance. fielda and fieldb can store input strings, and fieldresult can store the results after two strings are connected. After the setting is complete, press the next button to go to step 3.

The dialog box prompts whether to add myactionform to the struts-config.xml component file? Do you need to generate a reset method? Press the finish button by default.

At this time, actionform wizard has automatically generated the myactionform. Java file inherited from actionform. If we do not intend to implement the data verification validate method, we do not need to write any program code at all.

Step 3: create a custom action class

Click "Action wizard" in the web page of the select object gallery dialog box to generate a custom action class. Action wizard consists of two steps: step 1 the wizard automatically detects the web application demo and struts-config.xml component files that were previously created. We changed the custom action name from the default untitled1action to myaction, and then press the next button to go to step 2.

In the second step dialog box, we can set which actionform will be passed in the perform method of myaction, In the formbean name combo box, all the actionform records in the struts-config.xml will be listed for selection, in addition, we set the scope of myaction as session and specify/input. all input operations in JSP are handled by myaction.

Press the finish button to generate the myaction. Java file. In myaction. Java, JBuilder has automatically generated the perform method and the unimplemented exception program code. We modify the program code in the perform to complete the string connection:


     
      myActionForm myActionForm=(myActionForm)ActionForm ; String sA = myActionForm.getFieldA ( ) ; String sB = myActionForm.getFieldB ( ) ; myActionForm.setFieldResult (sA+sB) ; return ( actionMapping.findForward ( "input" ) ) ;
     

Since the perform method passes in the actionfrom parameter to obtain the string entered by the user in the web browser, the JBuilder has generated the code of converting actionform (Type Cast) to myactionform, developers only need to use the getfielda and getfieldb methods to learn the user input string, and save the connection result back to the fieldresult attribute using the setfieldresult method. Return determines the JSP webpage to be displayed. The findforward method of the actionmapping parameter is passed in using the perform method to specify the JSP webpage to be displayed. In the previous wizard dialog box,/input has been preset. JSP is the JSP page responsible for the input, Struts allows the input and display of the same JSP page, so we need to set in the struts-config.xml named input forward tag, so that the findforward method can use this tag to learn the real JSP page name/input. JSP.

Step 4: Set the forward flag

Click the struts-config.xml in the project pane (Project pane) in the upper-left corner of JBuilder, and the integrated development environment allows you to edit the struts-config.xml content in a graphical user interface, structure pane in the lower left) according to the struts-config.xml structure, there are four types of tag items: datasources, form beans, global forwards, and Action mappings.

After clicking the global forwards project, the content pane on the Right of JBuilder will display the editing screen. The developer can press the Add button to add a tag named input, and specify the corresponding path of the mark as/input. JSP.

For developers who are already familiar with Struts framework, to directly modify the struts-config.xml content, you can click the source page in the selection content pane, where the content pane displays the XML content in a text editor, we are free to choose a way to maintain struts-config.xml content.

Step 5: Generate JSP

Next we will use the existing myactionform. Java to generate/input. jsp, and click jsp from actionform wizard in the web page of the select object gallery dialog box. The JSP Wizard will appear in three steps:

In the Step 1 dialog box, you can change the name of the JSP file to/input. jsp from the preset untitledjsp1.jsp file, and press [... ] Button to select actionform. The actionform selection dialog box has two search modes. You can browse the Java package tree structure and select myactionform directly.

Or enter the Java class prefix name, JBuilder will automatically find the matching and so on. For example, enter the prefix "my" for "myactionform", and the two matching items are displayed in the dialog box. This convenient select category dialog box can be activated at any time by pressing Ctrl-alt-H in the JBuilder editor.

After selecting myactionform, click Next to go to the next step dialog box. JBuilder will analyze myactionform. java content shows its fields. We can combine the square to determine which struts tag the field will be displayed with. In this example, all three fields use the text type that can be input and preset text, press the next button to go to step 3. The Step 3 dialog box provides multiple options for the struts tag libraries feature, and asks whether to import all struts tag libraries to the JSP file. In this example, the default value is used. After you press finish, JBuilder generates/input. jsp.

Step 6: Set the Execution Component

Then we can set the Execution Component to test the program. Click to select the main menu project of JBuilder integrated development environment | project prosperities... The project property Setting dialog box appears. Click to select the run page to prepare the execution component. JBuilder allows multiple execution components in a single project to execute Java application, Java Applet, j2_midlet, and various J2EE server applications.

In this example, an execution component of the server type is created. Press new… on the right of the dialog box... To add an execution component.

In the execute Component Property Settings dialog box, you can type a name for the purpose of recognition in the Name field. In this example, we enter the Chinese recognition name "Run input. JSP ", you can select the server type application in the" run "Page type combo box. In this example, we use JBuilder's built-in Tomcat 4.0 as JSP/servlet container, therefore, select Tomcat 4.0 in the server drop-down box. If no proper options are available in our server combo box, you can choose JBuilder Main Menu tools | configure Server... . Then we must select this web application to activate the URI. We can press [... ] Button to display the select URI dialog box. You can click and select input. jsp on the left side, press OK to complete the selection, and return to the Execution Component Property Setting dialog box.

In this case, all the main projects in the Execution Component Property Setting dialog box are set. Click OK to return to the project property Setting dialog box. In the Project Properties dialog box, the execution components set in the preceding steps are listed in the runtime invocations list. Click OK to complete the Execution Component settings.

Step 7: run the Web Application

Next, you can click Run in the JBuilder menu or green triangle symbol in the tool column to execute this web application. JBuilder will activate Tomcat 4.0 Server in the integrated development environment. We can test it through JBuilder's built-in Web browser, or type in the Microsoft Internet Explorer URL field:

HTTP: /localhost: 8080/demo/input. jsp

For testing. In the web browser, type AAA and BBB and press the submit button to obtain the expected calculation result aaabbb.

If you type the first field in Traditional Chinese, you will find garbled characters, and the string connection results cannot be correctly identified. Struts framework does not have compatibility issues with Chinese characters, but Tomcat 4.0 requires Request Encoding when using JSP to process requests containing Chinese characters. Commonly used encoding formats include big5 and ms950 in the traditional Chinese environment. The ms950 encoding formats include big5 encoding, and an additional ?? , Heng, Li, makeup, Ghost, rust, wall and other seven words.

For example, on the Microsoft Windows platform, to make Tomcat 4.0 correctly process traditional Chinese characters, add:

<% @ Page contenttype = "text/html; charset = ms950" %>
<% Request. setcharacterencoding ("ms950"); %>

Set the webpage content encoding and Request Encoding to ms950. Similarly, for simplified Chinese (commonly used encoding: gb2312), the above Code should be:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% Request. setcharacterencoding ("gb2312"); %>

However, in Struts framework, the actionservlet actually receives a web browser request and transfers it to a specific action class for processing. Therefore, setting the Request Encoding directly in JSP is ineffective.

Iii. Process Chinese encoding (take traditional Chinese as an example)

There are multiple ways to handle the Struts framework Chinese encoding problem. Here we inherit the actionservlet class and overwrite the process method, and set the Request Encoding in the subclass process method. This shows that actionservlet acts as a front-end controller.

First, click Select JBuilder main menu file | new classes to prepare a new class. Inherit from org. Apache. Struts. Action. actionservlet and change the default class name from untitled1 to myactionservlet. Press the OK button to generate the program code. Click to select the JBuilder Main Menu wizards | override methods... The override method wizard automatically lists all methods that can be overwritten by the parent actionservlet. In the available methods list, select the process (httpservletrequest request, httpservletresponse response) method, and then press OK.

In this case, we can modify the overwrite process method program code as follows:


     
      request.setCharacterEncoding ("MS950") ; super.process (request, response) ;
     

Note: here we use traditional Chinese as an example. In China, we generally use simplified Chinese. Therefore, you can write the above Code in the simplified Chinese environment as follows:


     
      request.setCharacterEncoding ("GB2312") ; super.process (request, response) ;
     

The first line of program code sets the request parameter passed in by process to ms950 encoding, and other behaviors are the same as the parent class. Next, we will modify the Web. xml deployment description file to specify the servlet entry point. First, click Select Web. XML in the project pane in the upper-left corner.

In this case, the tree structure marked by XML in Web. xml appears in the lower-left structure pane. We can click to select the Action Project in servlets.

In this case, you can use the graphic user interface editor in the right content pane to modify the servlet entry point. Currently, the preset entry point is the Struts framework actionservlet.

We can press the right side of the servlet class project [... ] Select a new entry point to replace the preset actionservlet, which is the strutsdemo. myactionservlet we have written. If we are very familiar with the Web. XML structure, you can also directly click the sources page in the content pane and modify the content marked by <servlet-class> In the text editor.

Recompile the project and execute it. After testing again, we will find that traditional Chinese can be correctly processed.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.