Develop Struts instances with JBuilder9

xiaoxiao2021-03-06  51

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. 1. JBuilder's support for Struts to provide a graphic user interface to simplify the editing and setting of component file struts-config.xml. In addition, JBuilder provides four elves to simplify Struts development in the web page of the Object Gallery dialog box. Process. 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, using the JBuilder Development Struts Instance We will have an example of a simple web application here 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 to select the 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: Establishing a custom ActionForm class Struts framework does not limit the three development order of the MVC. Since we are not ready to properly enter the two strings HTML or JSP, first establish the Actionform class that stored information, and then Generate an Action class that plays the Controller role, and finally generates a JSP file by the Actionform class. 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: Create a custom Action class Click to select the Object Gallery dialog web page Action Wizard can generate custom Action class, Action Wizard is divided into two steps: the first step wizard automatically detects the previously established web application Program Demo and Struts-Config.xml Component file, we use the custom Action name to modify 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 Perform method and exception code that has not been implemented. We make the following modifications in Perform, and the action has been completed: 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 the parameter actionFrom in order to know the web browser input string, this JBuilder has completed the ActionForm transformation (Type Cast) of the program code for MyActionform, the developer only needs to be with the getfielda and getfieldb method. The string of the user can be learned, and the result of the connection after the result is returned in the setfieldResult method. 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 within the top left of JBuilder. At this time, the integrated development environment can be graphically edited by the Struts-Config.xml content, lower left. Structure Pane will be divided into DataSources, Form Beans, Global Forwards and Action Mappings four-class tag items in accordance with Struts-Config.xml structure. 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 can choose the habit to maintain struts-config.xml content. Step 5: Generating JSP Next we will generate /Nput.jspa production /put.jsp, click to select Object Gallery dialog web page JSP from Actionform Wizard Will appear in three steps to generate JSP wizard: In the first dialog box, 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 category 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: Setting the execution component then we can set the execution component to test this procedure. 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 Click to select the Green Triangle symbol in the JBuilder menu RUN or tool column to perform this web application. JBuilder will activate Tomcat 4.0 Server in the integrated development environment, we can test through the JBuilder built-in web browser, or type in the Microsoft Internet Explorer URL field: http:// localhost: 8080 / demo / input.jsp for testing . 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 handle traditional Chinese, usually add: <% @ page contenttype = "text / html; charset = ms950"%> <% request.setcharacterencoding at JSP starter MS950 ");%> To set the web page content encoding and request code to encode MS950 encoding; Similarly, for Simplified Chinese (commonly encoded as GB2312), the above code should be: <% @ Page ContentType =" Text / HTML; Charset = GB2312 "%> <% request.setCharacterencoding (" GB2312 ");%> But Struts Framework is actually received by the ActionServlet request, and then transfers to a specific Action class, so it is directly located in JSP The request coding is not effective. Third, to process Chinese coding problems (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, and set request coding in subclass Process 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: 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);

转载请注明原文地址:https://www.9cbs.com/read-115850.html

New Post(0)