Detailed description of Struts2 single-choice buttons and sample code for converting enumeration types.

Source: Internet
Author: User

Detailed description of Struts2 single-choice buttons and sample code for converting enumeration types.

This article mainly describes the Struts2 framework radio button details and examples of Enumeration type conversion, as follows.

To use the struts2 tag, you must first introduce the tag Library:

<%@ taglib prefix="s" uri="/struts-tags"%> 

Assume that the List Value in radio single orders is a Map set:

<S: radio list = "# {'man ': 'mal', 'women ': female} "name =" gender "listKey =" key "listValue =" value "value =" 'man' "theme =" simple "/>

Note:

1. the name attribute still corresponds to the name attribute of the HTML code.
2. The listKey attribute corresponds to the value attribute of HTML code.
3. The listValue attribute corresponds to the label attribute of the HTML code.
4. The value attribute is selected when the value matches the value in the Set (if the value is a string, a single quotation mark is required ).

After execution, the code is parsed as html code,

The following is a case study:

When you modify a user, you can jump to the modify interface to display the user's gender. Here, gender is represented by enumeration.

The initial user interface:

The implementation code is as follows !!!!

Project Structure:

User Interface:

<Body> <form action = "enumAction_test.action" method = "post"> select gender: <s: radio list = "# {'man ': 'male ', 'women ': female} "name =" gender "listKey =" key "listValue =" value "value =" 'man' "theme =" simple "/> <input type =" submit "value =" Submit "> </form> </body>

Background action code:

Public class EnumAction {/* defines Gender. The default value is male */private gender Gender = Gender. MAN; public Gender getGender () {return gender;} public void setGender (Gender gender) {this. gender = gender;}/* test Method */public String test () {return "success ";}}

Note:Because we use the enumeration type, we need to convert the type (the specific conversion method is mentioned in the previous blog ).

Enumeration type conversion code:

Public class EnumTypeConverter extends DefaultTypeConverter {@ SuppressWarnings ("rawtypes") @ Override public Object convertValue (Map <String, Object> context, Object value, Class toType) {/* convert string to Enumeration type from front-end to backend */if (toType = Gender. class) {/* convert value to array type */String [] params = (String []) value; return Gender. valueOf (params [0]);} else if (toType = String. class) {/* upload the action from the background to the foreground, that is, convert the Gender type to the String type * // convert the value to Gender */Gender gender = (Gender) value; return gender. toString () ;}return null ;}}

Attribute files defining global type conversion:

com.lixue.bean.Gender=com.lixue.converter.EnumTypeConverter 

Result page after jump (User modification Interface ):

<Body> your gender: <s: radio list = "# {'man ': 'male', 'women ': female} "name =" gender "listKey =" key "listValue =" value "value =" gender "theme =" simple "/> </body>

Note:On the modify interface, how does one display the currently selected gender? The key is to look at the value. I wrote a gender for the value in the above Code. In fact, this gender is an attribute defined in action. We mentioned it when talking about OGNL in the previous article, the attributes in the action are saved in the value stack. The attributes in the value stack can be obtained directly using the attribute name without the # namespace. After obtaining the information, it matches with the previous map set to determine whether or not to check the information.

Appendix:

1. Some people may wonder why the listKey can be directly written into the key and the listValue can be directly written into the value.

A: When struts2 parses the list set in the <s: radio> tag, it uses Iterator. The iterator function is to obtain the result (each time an Entry object is obtained) in each loop) stored on the top of the value stack. Therefore, when you use an OGNL expression to take a value, you can directly use the object attribute (Entry attribute name is key and value) name to take the value, you do not need to use the # namespace method. If you do not understand the OGNL expression, you can read the previous article.

2. we will find that we always write the theme attribute when writing tags. This attribute is used to prevent struts2 from generating unnecessary code when parsing tags, but every time we write it, it's not very boring. In fact, we can use struts. you can configure a constant in the xml file to achieve the following results:

<! -- Struts2 tag topic style --> <constant name = "struts. ui. theme" value = "simple"/>
Summary

The learning of these Struts frameworks is a bit confusing. I have a clear understanding of the Java framework, such as spring and mybatis, wan fa is connected...

The above is all the details about Struts2 single-choice buttons and the sample code for converting enumeration types. I hope this will help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.