1. struts itself can help us change the basic type> packaging type
1. Basic type: byte binary digits: 8
2. Packaging class: java. lang. Byte
3. Minimum value: Byte. MIN_VALUE =-128
4. Maximum value: Byte. MAX_VALUE = 127
5,
6. Basic type: short binary digits: 16
7. Packaging class: java. lang. Short
8. Minimum value: Short. MIN_VALUE =-32768
9. Maximum value: Short. MAX_VALUE = 32767.
10,
11. Basic type: int binary digits: 32
12. Packaging class: java. lang. Integer
13. Minimum value: Integer. MIN_VALUE =-2147483648
14. Maximum value: Integer. MAX_VALUE = 2147483647
15,
16. Basic type: long binary digits: 64
17. Packaging class: java. lang. Long
18. Minimum value: Long. MIN_VALUE =-9223372036854775808
19. Maximum value: Long. MAX_VALUE = 9223372036854775807
20,
21. Basic type: float binary digits: 32
22. Packaging class: java. lang. Float
23. Minimum value: Float. MIN_VALUE = 1.4E-45
24. Maximum value: Float. MAX_VALUE = 3.4028235E38
25,
26. Basic type: double binary digits: 64
27. Packaging class: java. lang. Double
28. Minimum value: Double. MIN_VALUE = 4.9E-324
29. Maximum value: Double. MAX_VALUE = 1.7976931348623157E308
30,
31. Basic type: char binary digits: 16
32. Packaging class: java. lang. Character
33. Minimum value: Character. MIN_VALUE = 0
34. Maximum value: Character. MAX_VALUE = 65535
2. Custom conversion packaging type
After we create a class ourselves, we want to convert the data transferred from the front-end to the class we created ourselves. We can do this.
Front-end form
The code is as follows: |
Copy code |
<S: form action = "llogin"> <S: textfield name = "user. name" label = "user name"/> <S: textfield name = "user. password" label = "password"/> <S: submit value = "conversion"/> <S: reset value = "Refill"/> </S: form> |
Submit this form to llogin and configure it here for processing by LoginAction.
The code is as follows: |
Copy code |
<Action name = "llogin" class = "org. Rudiment. action. LoginAction"> <Result>/welcom. jsp </result> </Action> |
The content of LoginAction is as follows:
The code is as follows: |
Copy code |
Package org. Rudiment. action; Public class LoginAction extends ActionSupport { Private User user; Public User getUser (){ Return user; } Public void setUser (User user ){ This. user = user; } @ Override Public String execute () throws Exception { // ServletActionContext. getServletContext (). setAttribute ("tip", "dadasddasdsdasdasdsadasd "); System. out. println (getUser (). getName () + "|" + getUser (). getPassword ()); Return SUCCESS; } } |
When we submit data from the front-end, we find that the data has one-to-one correspondence with the User. The User is just a common custom class. The custom class is as follows:
The code is as follows: |
Copy code |
Package org. Rudiment. action; Public class User { Private String name; Private String password; Public String getName (){ Return name; } Public void setName (String name ){ This. name = name; } Public String getPassword (){ Return password; } Public void setPassword (String password ){ This. password = password; } } |
One of the main points is on the form above
The code is as follows: |
Copy code |
<S: textfield name = "user. name" label = "user name"/> <S: textfield name = "user. password" label = "password"/> Name = "user. name "; Name = "user. password "; |
When the form is submitted to the backend, it is processed by the struts framework. User. name corresponds to the name attribute of the user object of LoginAction. The same applies to user. password.
So when we write the front-end form like this, struts will help us get a seat. (Assign values to objects)
3. Type conversion of the set
Most of them are the same as normal classes. If we change Action to the following format:
The code is as follows: |
Copy code |
Package org. Rudiment. action;
Public class LoginAction extends ActionSupport { Private Map <String, User> user; Public Map <String, User> getUser (){ Return user; } Public void setUser (Map <String, User> user ){ This. user = user; } @ Override Public String execute () throws Exception { // ServletActionContext. getServletContext (). setAttribute ("tip", "dadasddasdsdasdasdsadasd "); System. out. println (getUser (). get ("one "). getName () + "|" + getUser (). get ("one "). getPassword ()); Return SUCCESS; } }
|
I changed the user to a map object. At this time, we only need to modify the content submitted in the front-end form as follows:
The code is as follows: |
Copy code |
<S: form action = "llogin"> <S: textfield name = "user ['one']. name" label = "user name"/> <S: textfield name = "user ['one']. password" label = "password"/> <S: submit value = "conversion"/> <S: reset value = "Refill"/> </S: form> |
When we open the tomcat console, we will see the entered content.
This is because this sentence is added to our LoginAction.
System. out. println (getUser (). get ("one "). getName () + "|" + getUser (). get ("one "). getPassword ());
Print them out.
User ['one']. name one corresponds to the String in map <String, User>, that is, the key in Map. It must be enclosed by quotation marks.
User ['one']. password
Another point is that the type of the variable to be created in LoginAction must be Map. If HashMap is declared, it cannot be converted. This may be because the class converted by struts is larger than HashMap, so it cannot be converted successfully.
Besides Map type conversion, ArrayList can also be converted.
Modify our LoginAction to the following content:
The code is as follows: |
Copy code |
Package org. Rudiment. action; Public class LoginAction extends ActionSupport { Private ArrayList <User> user; Public ArrayList <User> getUser (){ Return user; } Public void setUser (ArrayList <User> user ){ This. user = user; } @ Override Public String execute () throws Exception { // ServletActionContext. getServletContext (). setAttribute ("tip", "dadasddasdsdasdasdsadasd "); System. out. println (getUser (). get (0). getName () + "|" + getUser (). get (0). getPassword ()); Return SUCCESS; } } |
The front-end form is submitted and modified to this
The code is as follows: |
Copy code |
<S: form action = "llogin"> <S: textfield name = "user [0]. name" label = "user name"/> <S: textfield name = "user [0]. password" label = "password"/> <S: submit value = "conversion"/> <S: reset value = "Refill"/> </S: form> |
After you redeploy the application, click Submit. The tomcat console displays the content you just submitted.
I will not talk too much. We can see this conversion idea through comparison. The usage of ognl in the foreground is user [0]. password. This is the ognl expression language.
You can use the front-end form to convert the type of the custom class. Generally, this can meet your needs. However, if there are exceptions, you need to write your own type converter.
Do not be afraid that the type converter is just a common class that inherits defatypetypeconverter.
Based on the example of one of our conversions
1. Change the foreground page
The code is as follows: |
Copy code |
<S: form action = "ltlogin"> <S: textfield name = "user" label = "user name"/> <S: submit value = "conversion"/> <S: reset value = "Refill"/> </S: form>
|
Note: user. name user. password is no longer used here
This is the time when struts cannot help you with type conversion. Then struts agrees to write a type converter by ourselves. Add a configuration file.
2. The content in our LoginAction remains unchanged.
The code is as follows: |
Copy code |
Package org. Rudiment. action; Public class LoginAction extends ActionSupport { Private User user; Public User getUser (){ Return user; } Public void setUser (User user ){ This. user = user; } @ Override Public String execute () throws Exception { System. out. println (getUser (). getName () + "|" + getUser (). getPassword ()); Return SUCCESS; } } |
3. The User class also remains unchanged:
The code is as follows: |
Copy code |
Package org. Rudiment. action; Public class User { Private String name; Private String password; Public String getName (){ Return name; } Public void setName (String name ){ This. name = name; } Public String getPassword (){ Return password; } Public void setPassword (String password ){ This. password = password; } } |
4. Added a class used as a converter.
The code is as follows: |
Copy code |
Package org. Rudiment. converter; Import java. util. Map; Import org. Rudiment. action. User; Import ognl. DefaultTypeConverter; Public class MyConverter extends DefaultTypeConverter { Package org. Rudiment. converter;
Import java. util. Map; Import org. Rudiment. action. User; Import ognl. DefaultTypeConverter; Public class MyConverter extends DefaultTypeConverter { Public Object convertValue (Map context, Object value, Class toType) { If (toType = User. class) { System. out. println ("toType = User. class "); String [] params = (String []) value; User user = new User (); String [] userValues = params [0]. split (","); User. setName (userValues [0]); User. setPassword (userValues [1]); Return user; } Else if (toType = String. class) { System. out. println ("User. class = toType "); User user = (User) value; Return "<" + user. getName () + "," + user. getPassword () + "> "; } Return null; } }
|
This converter inherits a class. This class has a method and we need to override this method. Determine the conversion direction by determining the toType.
Parameter description:
The current context of the context (I am not very clear about the context concept. Leave a message .)
Value: the parameter value on the current foreground page.
By determining this parameter, we can know the conversion direction of the processing.
5. Configure the conversion file
After the above conversion type class is completed, we need to configure a file.
Create a file named LoginAction-conversion.properties under the corresponding Action peer directory. This file is our configuration file.
The file content is as follows:
User = org. Rudiment. converter. MyConverter
6. When you enter 123,456 in the input box on the front-end page and click submit, the tomcat console displays 123 | 456
This configuration file is only valid for an Action because it is configured as a local file (starting with the Action name and placed in the same level Directory of the Action as a local configuration file LoginAction-conversion.properties. This configuration is inefficient. The configuration can only make the file used by one Action a little waste of resources. So there is also a configuration that is a global configuration file, a fixed write xword-conversion.properties for this file name
The content is slightly different from that of a local file. The configuration in our local configuration file is as follows:
User = org. Rudiment. converter. MyConverter
Resolution; configuration content format of the local file. This statement indicates that the value of name in the previous stage is handed over to Rudiment. converter. MyConverter.
Org. Rudiment. action. User = org. Rudiment. converter. MyConverter
Resolution: The configuration content format of the global file. This statement indicates that org. Rudiment. converter. MyConverter converts the content submitted by the current station when it is related to the User.
============================ Extended content ============ ========================
When our front-end form is
The code is as follows: |
Copy code |
<S: form action = "ltlogin"> <S: textfield name = "user" label = "user name"/> <S: submit value = "conversion"/> <S: reset value = "Refill"/> </S: form> |
In addition, the Action to be submitted contains the user attribute. However, no converter is configured for it. The following error message is displayed when the converter is not properly configured:
HTTP Status 404-No result defined for action org. Rudiment. action. LoginAction and result input