The main components of Java Web programming technology--struts Advanced Features

Source: Internet
Author: User
Tags html form

Reference book: "Java EE open source programming Essentials 15"

Struts support for internationalization

"Internationalization" (i18n) means that an application can display different user interfaces at run time, depending on the country/region or language in which the client request is made. To internationalize with struts, the "tag" information for various language versions is stored in a different attribute file, one for each language version. The filename suffix must be properties, the prefix name is determined by the developer, and the language Type field must be a valid ISO (international standardization Organization, International Organization for Standardization) language code, such as: Chinese-zh, English-en and so on. The filename prefix is the default (the book says "default", but I feel it should be "no") language code as the default property file. The default property file is used when the system cannot find a property file that matches the language properties requested by the client.

International processing of the login system in the introduction to struts

1) first create the English and Chinese version of the resource file

Applicationresources.properties

1 Title.login=login Page 2 Label.username=input your username 3 label.password=input Your password 4 Item.submit=submit 5 Item.reset=reset 6 Message.success=you ' re successful to login in. 7 message.failure=you fail to login
View Code

Applicationresources_zh.properties

1 title.login=\u767b\u9646\u9875\u9762 2 label.username=\u59d3\u540d 3 label.password=\u5bc6\u7801 4 item.submit=\u767b\u9646 5 item.reset=\u91cd\u7f6e 6 message.success=\u4f60\u5df2\u6210\u529f\u767b\u9646 7 Message.failure=\u4f60\u767b\u9646\u5931\u8d25
View Code

PS: Create a new applicationresources_zh.properties in MyEclipse, select the Properties window in the edit area, and after adding the key value pairs, the Chinese transcoding will be automatically

2) configuration in Struts-config.xml

<message-resources parameter= "Struts/applicationresources"/> (the parameter attribute depends on the location of the property file in the project, where the file is within the struts package)

3) Modify the page, use the <bean:message> tag to read the tag information from the resource file instead of the original character text

1 <%@ Page Language="Java"pageencoding="UTF-8"%>2 <%@ taglib URI="Http://struts.apache.org/tags-bean"prefix="Bean"%> 3 <%@ taglib URI="http://struts.apache.org/tags-html"prefix="HTML"%>4  5 <HTML> 6     <Head>7         <title><Bean:messageKey= "Title.login"/></title>8     </Head>9     <Body>Ten         <Html:formAction= "/login"> One             <Bean:messageKey= "Label.username"/>:<Html:text Property= "username"/><html:errors Property= "username"/><BR/> A             <Bean:messageKey= "Label.password"/>:<Html:password Property= "Password"/><html:errors Property= "Password"/><BR/> -             <Html:submit Property= "Submit"> -                 <Bean:messageKey= "Item.submit"/> the             </Html:submit> -         </Html:form> -     </Body> - </HTML>
View Code

When only the default resource file, the landing page is displayed in English, when the Chinese version of the resource file is added, the landing page displays Chinese

Dispatchaction class

Typically only one business operation is completed in an action Class (execute (), but in fact it is possible to invoke different methods in the Execute () method depending on the arguments passed in), creating a subclass of the extended dispatchaction without overwriting the Execute () method, Instead, you create methods that implement specific business logic operations that have the same parameters and return values as the Execute () method, such as

1  Public classMydispatchactionextendsdispatchaction {2      Publicactionforward Add (actionmapping Mapping, actionform form,3 httpservletrequest request, httpservletresponse response) {4         //TODO auto-generated Method Stub5         returnMapping.findforward ("Add");6     }7      PublicActionforward Edit (actionmapping mapping, actionform form,8 httpservletrequest request, httpservletresponse response) {9         //TODO auto-generated Method StubTen         returnMapping.findforward ("Edit"); One     } A}
View Code

When configuring the Dispatchaction class, you need to specify the parameter property as method

When writing code that triggers dispatchaction, not only does it indicate the mapping path, but it also needs to add a parameter called method, such as

Http://localhost:8080/login/users?method=add

Call the Add method for the corresponding dispatchaction

Dynamic Actionform

  If you add or remove attributes from the corresponding HTML form, the Actionfrom class may need to modify the actionform and introduce the concept of dynamic actionform (Dynaactionform). Dynamic Actionform allows users to create specific Actionform classes for each HTML form of an application.

Dynamic Actionform supports the complete configuration of actionform in the Struts profile struts-config.xml without the need to write additional Java code. Such as:

1 <Form-beans>2     <Form-beanname= "LoginForm"type= "Org.apache.struts.action.DynaActionForm" >3         <Form-propertyname= "username"type= "Java.lang.String" />4         <Form-propertyname= "Password"type= "Java.lang.String" />5     </Form-bean>6   </Form-beans>
View Code

The Type property of the <form-bean> element is set to Dynaactionform or its subclasses. <form-bean> contains the properties of the <form-property> child element tag dynamic form, the type attribute specifies the property type, the property type must be a standard Java class, and uses their wrapper class, such as Int-integer, Long-long,float-float,double-double,boolean-boolean.

When you need to add a new type, you only need to modify the configuration file (). Accessing the dynamic Actionform in action

1 dynaactionform loginform = (dynaactionform) Form; 2 string username= (String) loginform.get ("username"); 3 string password= (String) loginform.get ("password");
View Code

Validator Validation Framework

By configuring validation rules to implement validation, you do not need to write code to maximize the reuse of the same validation rule. Need to use Validator-rules.xml and validation.xml files, validator-rules.xml file contains a set of common validation rules, generally do not need to modify, directly deployed in the corresponding Web-inf folder; Validation.xml by the Open By the sender.

For details, please see:

Http://www.blogjava.net/nokiaguy/archive/2009/02/12/254421.html

Internationalization Example Simple login system: Http://pan.baidu.com/s/1bnhKAcR

The main components of Java Web programming technology--struts Advanced Features

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.