Creating JSP files and actionform beans

Source: Internet
Author: User
Tags html form key range reset resource return tld client
js| Create This example, the view includes two components:
• One JSP file: hello.jsp
• A actionform bean:helloform Bean
How to create these two components is described below.

Create a JSP file

HELLO.JSP provides a user interface that accepts user-entered names. In addition, all output from this Web application is displayed to the user by hello.jsp. Figure 2-1 shows the Web page provided by hello.jsp.


Figure 2-1 Hello.jsp's Web page

In Figure 2-1, after the user enters the name "Weiqin", by submitting the form, the application returns "Hello weiqin!", see Figure 2-2.


Figure 2-2 hello.jsp A Web page that is normally returned after user input is accepted

Routine 2-1 is the source code for the hello.jsp file.

      
       
Routine 2-1 hello.jsp <%@ page contenttype= "Text/html;charset=utf-8" language= "java"%><%@ taglib "uri=" Struts-bean.tld "prefix=" Bean%><%@ taglib uri= "/web-inf/struts-html.tld" prefix= "html"%><%@ taglib uri= "/web-inf/struts-logic.tld" prefix= "logic"%>


The above JSP files based on the Struts framework have the following characteristics:
• No Java program code
• The use of many of the client labels for struts, such as Without directly providing textual content, instead <bean:message> tags, output to the page text content are made up of <bean:message>tag to generate the. For example:

<bean:message key= "Hello.jsp.prompt.person"/>

The Struts client label is the link between the view component and other components in the Struts framework. These tags can access or display data from the controller and model components. In this book, chapters 12th through 16 are devoted to the use of the Struts label, this section introduces briefly several important struts tags.

The first few lines of hello.jsp are used to declare and load the Struts tag library:

     
          
           
      <%@ taglib uri= "/web-inf/struts-bean.tld" prefix= "Bean"%><%@ taglib uri= "/web-inf/struts-html.tld" prefix= " HTML "%><%@ taglib uri="/web-inf/struts-logic.tld prefix= "logic"%>
     
          


The above code indicates that the JSP file uses a struts Bean, HTML, and logic tag library, which is the standard JSP syntax for loading a custom tag library.

tags from the Struts HTML tag library are used in hello.jsp, including · · ·
Two tags from the Struts Bean tag library are used in hello.jsp <bean:message> and <bean:write>:
<bean:message>: Used to output localized textual content, its key attribute specifies the message key, and the text content that matches the message key comes from a dedicated resource Bundle, the concept of resource Bundle See Chapter 9th of this book (Internationalization of Struts application).
<bean:write>: The property value used to output JavaBean. In this case, it is used to output the value of the Username property of the Personbean object:
<bean:write name= "Personbean" property= "UserName"/>

Hello.jsp used a logic tag library from the Struts <logic:present> tags. <logic:present>tags are used to determine whether JavaBean exists within a specific range, and only when JavaBean exists will the contents of the label body be executed:

     
                     
                      
      <logic:present name= "Personbean" scope= "request" >       


In this case, <logic:present> tags are used to determine whether there are Personbean objects in the request scope, and if so, to output Personbean Username property values. and <logic:present> tags are relative to <logic:notPresent>Label, which means that the contents of the label body are executed only if the JavaBean does not exist within a specific range.

Create a message resource file

hello.jsp use <bean:message>Label to output text content. The text comes from Resource Bundle, where each resource Bundle corresponds to one or more localized message resource files, in this case the resource file is application.properties and routine 2-2 is the content of the message resource file.

     
                        
                         
      Routine  2-2 application.properties file #application resources for the "Hello" sample Applicationhello.jsp.title=hello-a The Programhello.jsp.page.heading=hello world! A applicationhello.jsp.prompt.person=please Enter a UserName to say hello To:hello.jsp.page.hello=hello#val  Idation and error messages for Helloform.java and helloaction.javahello.dont.talk.to.monster=we don ' t want to-say hello to Monster!!! Hello.no.username.error=please Enter a <i>UserName</i> to say hello to!
     
                        


The above file holds the data in the format "message key/message text," followed by the comment line in the file "#". For the following JSP code:

<bean:message key= "Hello.jsp.title"/>

<bean:message>The key attribute of the label is "Hello.jsp.tilte", and the matching content in resource bundle is: Hello.jsp.title=hello-a-A-Struts program

Therefore, the above <bean:message>The label will output the "Hello-a-A-Struts program" to the Web page.

Creating Actionform Beans

When the user submits an HTML form, the Struts framework automatically assembles the form data into the Actionform bean. The properties in the Actionform bean correspond to field one by one in the HTML form. The Actionform Bean also provides data validation methods and methods to reset the properties to their default values. The Actionform class defined in the struts framework is abstract and must be created in the application to contain the specific HTML form data. Routine 2-3 is a Helloform.java source program that handles the form data in hello.jsp.

                            
                             
Routine 2-3 Helloform.javapackage hello;import javax.servlet.http.httpservletrequest;import Org.apache.struts.action.actionmessage;import Org.apache.struts.action.actionerrors;import Org.apache.struts.action.actionform;import Org.apache.struts.action.actionmapping;public Final Class HelloForm    Extends Actionform {private String userName = null;    Public String GetUserName () {return (this.username);    } public void Setusername (String userName) {this.username = UserName;     /** * Reset All properties to their default values.    * * public void Reset (actionmapping mapping, HttpServletRequest request) {this.username = null; }/** * Validate The properties posted in this request.     IF validation errors are * found, return a <code>ActionErrors</code> object containing the errors. * If No validation errors occur, return <code>null</code> or a empty * <code>actionerrors</code& Gt     Object.   */ Public actionerrors Validate (actionmapping mapping, HttpServletRequest request) {        Actionerrors errors = new Actionerrors (); if ((UserName = null) | |            (Username.length () < 1))        Errors.add ("username", New Actionmessage ("Hello.no.username.error"));    return errors; }}
                            


As you can see from the code above, the Actionform bean is essentially a javabean, but it has two special methods in addition to the usual method of JavaBean:
Validate (): for form validation.
Reset (): Reset the property to its default value.

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.