This article will introduce you to how to use Struts Framework to develop web applications in the JBuilder 9 integrated development environment. We will refer to JBuilder 9 Developer and Enterprise.
First, JBuilder support for Struts
JBuilder provides a graphical user interface to simplify the editing and setting of component file Struts-Config.xml. In addition, JBuilder provides four elves to simplify Struts development processes in the web page of the Object Gallery dialog.
Struts Converter Wizard: Specifies the HTML or a general JSP file to automatically generate JSP files that use the Struts Tag Libraries.
ActionForm Wizard: You can import JSP file generation inheritance from Actionform, or Custom Attributes Generate Inherited Self-ActionForm, and you can select whether to complete Struts-Config.xml before setting.
Action Wizard: The JSP file that is inherited from Action, setting the match, the valid range (scope), and automatically completing the struts-config.xml corresponding settings.
JSP from ActionForm Wizard: The JSP file that can be automatically generated by the specified ActionForm is automatically generated by the Struts Tag Libraries.
In addition to the above elves, JBuilder assists that developers automatically set the Web.xml deployment description file so that JSP uses Struts to customize Tag Libraries.
Second, develop Struts instances using JBuilder
We will have an example of developing a simple web app to help you understand the struts framework. This web application allows the user to enter two independent strings and display two strings after the web page. Since the processing logic is too simple, we intend to complete the two-string connection action directly in the action of playing the Controller role, so you do not use the JavaBean or classes that play the Model role to handle string connections.
Step 1: Create a web application
First click Select JBuilder Integration Development Environment Main Menu File | New Project ... Create a project called StrutsDemo. The Object Gallery dialog is then activated by the menu file | New ... and click on the Web Application Elf in Select Web Page.
The Web Application Elf will prepare the WAR for this web application to generate WAR's deployment description web.xml, WAR is the standard file format for simplifying Web program deployment complexity in J2EE specifications. We named Demo for this web application, which will be named Demo.war files when compiling this project file to facilitate deployment, in which the Struts Tag Libraries will be used in this Web application, so in JSP Struts 1.0 is checked in / servlet frameworks, and JBuilder automatically sets Web.xml.
Step 2: Establish a custom Actionform class
Struts framework does not limit the MVC three development order, because we are not ready to properly enter both strings HTML or JSP, first establish the Actionform class that stores information, then generates the Action of the Controller role, finally The JSP file is generated by the ActionForm class, and you can adjust the order in order to adapt to the actual case. Click to select the Object Gallery dialog web page time ActionformWizard can generate custom Actionform classes, ActionformWizard can be divided into three steps: the first step wizard automatically detects the previously established web application Demo, we will customize ACTIONFORM The name is modified by the preset UnTitled1ActionForm to MyActionform, then press the Next button to enter the second step. The second step adds the fields required to display the future JSP page in the future JSP page. In the case of the pages of the artist's full-time personnel have been prepared to enter the JSP page, you can also press the Add from JSP ... button directly, JBuilder will resolve this JSP file to generate the field definition table. In this case, since the JSP file is not prepared, it must be added to the desired field. We prepare three fields in advance for this web application, and Fielda and FieldB can store input strings, and FieldResult can place two string after the result. After the setting is complete, press the Next button to enter the third step. The dialog prompts whether to add myActionform to the Struts-Config.xml component file? Do you need a reset method? You can press the finish button in accordance with the default.
At this point, Actionform Wizard has automatically generated the MyActionform.java file from Actionform, and if we don't intend to implement the validate method of the data, we do not need to write any program code.
Step 3: Establish a custom Action class
Click to select the Object Gallery dialog web page Action Wizard can generate a custom Action class, Action Wizard is divided into two steps: the first step wizard automatically detects the previously established web application Demo and Struts-config.xml components Set, we will change the custom Action name by the preset un-titled1action to myAction, then press the Next button to enter the second step.
In the second dialog box, we can set a MyAction's Perform method to get in which ActionForm is in the FormBean Name Combination block, all Actionform recorded in Struts-Config.xml will be listed, so we Set the valid range of MyAction (Scope) for session, and specify all input actions for /input.jsp, will be configured with myAction.
After pressing the finish button, it produces a MyAction.java file. In MyAction.java, JBuilder has automatically generated the Perform method and the exception code that has not been implemented. We do the following modifications in the PERFORM, and the action of the string connection:
myActionForm myActionForm = (myActionForm) ActionForm; String sA = myActionForm.getFieldA (); String sB = myActionForm.getFieldB (); myActionForm.setFieldResult (sA sB); return (actionMapping.findForward ( "input")); perform method since It will pass the parameter actionFrom to know the string of users in the web browser, which is completed in this JBuilder to complete the ActionForm transformation (Type Cast) program code for MyActionForm, and developers only need to know with getfielda and getfieldb. The user entered the string and the result of the connection after the result is returned with the fieldResult property. Return will determine the JSP web page responsible for display, using the Perform method to pass the FindForward method for parameter actionMapping, which can be used to specify the JSP web page displayed. Before the Wizard dialog box, the preset /put.jsp is the JSP web page responsible for the input, and the Struts allows input and display as a JSP page, so we need to find the INPUT forward tags in Struts-Config.xml. So the Findforward method can use this tag to learn the true JSP web name /INput.jsp.
Step 4: Set the Forward tag
Click to select Struts-Config.xml in the top left of JBuilder. At this time, the integrated development environment can edit struts-config.xml content, and the Structure Pane will be edited. Will be divided into the Struts-Config.xml structure, divided into DataSources, Form Beans, Global Forwards and Action Mappings four-class tag items.
When you click Select Global Forwards Project, the Content PANE will appear as shown in the picture, the developer can press the Add button to add a mark named INPUT, and indicate this mark corresponding to the path to / input .
For developers who are already familiar with Struts Framework, if you want to directly modify the Struts-Config.xml content, you can click the Source page in the Select Content pane. At this time, the content pane will display XML content in the text editor as shown. We freely choose the habitual manner to maintain struts-config.xml content.
Step 5: Generate JSP
Next, we will use the existing MyActionform.java to generate /input.jsp, click to select the Object Gallery dialog web page JSP from Actionform Wizard, will appear in three steps to generate JSP wizard:
In the first step dialog, we can change the JSP file name from the preset to /input.jsp, and you can press the [...] button to select ActionForm. The Actionform selection dialog has two lookup mode, we can browse the Java Package tree structure directly to select MyActionform.
Or Type the Java class prefix name, JBuilder will automatically find the match. For example, we type myActionForm's prefix myActionform's prefix MY, and you can find the dialog box to two match. This convenient selection of categories can be used at any time in the JBuilder editor. Ctrl-alt-h at any time. After selecting myActionform, press the Next button to enter the second step dialog. JBuilder will analyze myActionform.java content to display its fields, we can make a combination of square to determine this field will be displayed in the struts tag match. In this example, the three fields can be entered, the preset Text type, press the next NEXT button. That is, enter the third step. The third step dialog provides a variety of options for the Struts Tag Libraries feature, and asks to import all Struts Tag Libraries in the JSP file. In this case, we use the default value and press the finish button after jbuilder to generate /input.jsp.
Step Six: Set the execution component
Then we can set the execution component to test this program. Click to select JBuilder Integration Development Environment Main Menu Project | Project Prosperities ... Putting the Project Properties Settings, Click Select RUN Page Preparation to set the execution component. JBuilder allows multiple execution components to be implemented in a single project, performing Java Application, Java Applet, J2ME MIDLET, and J2EE various Server applications.
This example will establish an execution component of a server type. Press the Dialog Right to the NEW ... button will appear to perform the Component Properties Settings dialog box to add an execution component.
Executing the Component Properties Settings dialog box, we can type a name of the identification use in the Name field. In this example, we type Chinese identification name "Execute Input.jsp", select the Server type application in the RUN page Type combination block. Since this example we use the JBuilder built-in Tomcat 4.0 as JSP / Servlet Container, so the Server pull-down boxes choose Tomcat 4.0. If there is no appropriate option in our Server combination, you can check in JBuilder Main Menu Tools | Configure Server ... Then we must select this web application to activate the URI, and we can press the Launch URI field to the right of the [...] button, you can select the URI dialog box, we can click on the left INPUT.JSP directly with the mouse click, press OK The button completes the selection and returns the execution component properties setting dialog.
At this point, the main items in the Component Properties setting dialog have been set, and press the OK button to return to the project properties setting dialog. The execution components set by the previous steps in the project properties dialog box will be listed in Runtime Configurations, and press the OK button to complete the execution component settings.
Step 7: Execute a web application
Next, you can click to select the green triangle symbol in the JBuilder menu RUN or the tool column to perform this web application. JBuilder will activate Tomcat 4.0 Server in an integrated development environment, and we can test through the JBuilder's built-in web browser, or type in the Microsoft Internet Explorer URL field:
Http: // localhost: 8080 / demo / input.jsp
Test. In a web browser, we typed AAA and BBB Press the Submit button to get the expected operations AAABBB.
If the first field is changed in traditional Chinese, it is found that there is garbled, and the string connection result cannot be properly identified. Struts framework does not have a Chinese compatibility, but Tomcat 4.0 is required to set request coding when using a Chinese request in JSP processing. Traditional Chinese Environment Coding is BIG5 and MS950, MS950 encoding, including BIG5 encoding, and additional increasing. For example, under the Microsoft Windows platform, you want to make Tomcat 4.0 correctly process traditional Chinese, usually add: in the JSP start:
<% @ Page ContentType = "Text / HTML; Charset = MS950"%> <% Request.SetChacterencoding ("MS950");%>
The setting web page content encoding is encoded as MS950 encoding; Similarly, for Simplified Chinese (commonly coded as GB2312), the above code should be:
<% @ Page contenttype = "text / html; charset = GB2312"%> <% Request.setCharacterencoding ("GB2312");%>
However, Struts Framework is actually received by the ActionServlet to receive a web browser, and then transferred to a specific Action class, so the request coding is set directly in the JSP and there is no effect.
Third, handle Chinese code issues (take traditional Chinese as an example)
There are a variety of ways to process Struts framework Chinese encoding problems, where we inherit the ActionServlet class and override the Process method to set request coding in subclass processing methods. Thereby, the ActionServlet is displayed to play the traits of the front-end Controller.
First, click Select JBuilder Main Menu File | New Classes to prepare a new class. And inherit from org.apache.struts.Action.ActionServlet and change the preset class name to myActionServlet. The program code is generated after pressing the OK button. Click to select JBuilder main menu Wizards | Override Methods ... Activate Coverage Elf, Coverage Wizard automatically lists all methods of overworked by parent class ActionServlet. We can select the Process (HttpServletResponse response method in the Available Methods clearance, and press the OK button after completion.
At this point we can modify the procedure code code code as follows:
Request.setCharacterencoding ("MS950"); Super.Process (Request, Response);
Remarks: This is the case, in China, in China, we will use Simplified Chinese in China, so the above code can be written in the Simplified Chinese Environment:
Request.setCharacterencoding ("GB2312"); Super.Process (Request, Response);
The first line program code sets the parameter request from Process to MS950 encoding, and the rest of the behavior is the same parent class. Next we will modify the web.xml deployment description file to specify the entry point of the servlet, first click to select Web.xml in the upper left project pane. At this time, the upper left side structure pane will appear a tree structure in the XML tag in Web.xml. We can click on the Action project in Select Servlets.
At this point, you can use the graphic user interface editor of the right content pane to modify the servlet entry point, and the current preset entry is the ActionServlet for the Struts Framework.
We can press the servlet class project Right Party [...] button to select the new entry replacement preset ActionServlet, which is the strutsdemo.myactionServlet we wrote. If we are very familiar with the web.xml structure, you can directly click on the Sources page in the Select Content Pane, and the contents of the
After completing, recompile this item and execute it. After again tested, we will find that traditional Chinese has been able to properly handle it.