This paper mainly studies the detailed explanation of the radio buttons of the Struts2 framework and related examples of the conversion of enumeration types, as follows.
Using struts2 tags, there is no doubt that you need to introduce the tag library first:
<%@ taglib prefix="s" uri="/struts-tags"%>
Assume that the value of List in the radio radio box is a Map collection:
<s:radio list="#{'MAN':'Male','WOMEN':'Female'}" name="gender" listKey="key" listValue="value" value="'MAN'" theme="simple"/> Note:
1. The name attribute is also the name attribute corresponding to the HTML code.
2. The listKey attribute corresponds to the value attribute of the HTML code.
3. The listValue attribute corresponds to the HTML code label attribute.
4. The value attribute refers to: when the value of the value matches the value in the set, it is selected (if the value of the value is a string, you need to add single quotes).
After execution, it will be parsed into html code, as shown in the figure:
Let's explain it through a case:
When modifying the user, jump to the modification interface and echo the user's gender in the modification interface. Here we use enumeration to represent the gender.
The initial user interface:
Project structure:
user interface:
<body> <form action="enumAction_test.action" method="post"> Please 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 {/*Define gender, default 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 are using enum types, we need to perform type conversion (the specific conversion method was mentioned in the previous blog).
Enumerate type conversion class code:
public class EnumTypeConverter extends DefaultTypeConverter {@SuppressWarnings("rawtypes") @Override public Object convertValue(Map<String, Object> context, Object value, Class toType) {/*Convert from the front to the backend, from string to enum type*/if (toType == Gender.class){/*Forcing value to array type*/String[] params = (String[]) value;return Gender.valueOf(params[0]);} else if (toType == String.class){/*Passes from the background action to the foreground, that is, converting the Gender type to String type*//*Firmly converting the value to Gender*/Gender gender = (Gender) value;return gender.toString();}return null;}}Define the properties file for global type conversion:
com.lixue.bean.Gender=com.lixue.converter.EnumTypeConverter
The 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: How to echo the currently selected gender in the modification interface? The key is to look at the value value. I wrote a gender in the above code. In fact, this gender is a property defined in the action. When we talked about OGNL in the previous article, we said that the properties in the action will be saved in the value stack. We take the properties in the value stack and can be obtained directly through the attribute name, without obtaining them through the form of #namespace. After obtaining it, match it with the previous map collection to determine whether to check it.
1. Some people may have questions, why can listKey be written directly on the key, and listValue be written directly on the value.
Answer: When struts2 parses the list collection in the <s:radio> tag, it uses Iterator. The function of iterator is to save the result (each time an Entry object) at the top of the stack of the value stack every time the loop is to obtain the value. Therefore, when using the OGNL expression, you can directly use the object's attribute (the attribute names of the Entry are key and value) names to get the value. There is no need to use the # namespace. Students who do not understand the OGNL expression can read the previous article.
2. We will find that when writing tags, we always write the attribute theme. The function of this attribute is to avoid struts2 generating some unnecessary code when parsing tags, but it is necessary to write it every time. Isn't it a scam? In fact, we can configure a constant in struts.xml file to achieve the effect:
<!--struts2 tag theme style--> <constant name="struts.ui.theme" value="simple" />
The learning of these Struts frameworks is quite a bit like seeing the blue sky through the clouds and mist. I have a clearer understanding of the Java frameworks, spring, mybatis, etc., and all things are connected. . .
The above is all the content of this article about the detailed explanation of the Struts2 radio button and the conversion code example of the enumeration type. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!