If we fill in the numbers on the Web form, do we need to manually convert the strings into numbers in the background?
We write the following procedure:
1. Create the file login.jsp, the core content is as follows:
<formAction= "Login.action">Username:<inputtype= "text"name= "username"> <BR>Password:<inputtype= "Password"name= "Password"><BR>Age :<inputtype= "text"name= "Age"> <BR>Date:<inputtype= "text"name= "Date"> <BR> <inputtype= "Submit"value= "Submit"> </form>
2. Create the file login_result.jsp core content is as follows:
Username: ${requestscope.username}<br> Password: ${ Requestscope.password}<br>age : ${requestscope.age}< br> Date: ${requestscope.date}<BR >
Print out the contents of four variables.
3. The Struts.xml configuration is as follows:
<name= "Login" class= "com.test.action.LoginAction"> <name= "Success">/login_result.jsp</ result> </action>
4. Finally we write the Loginaction class.
Because we wrote four of the content on the form, the class corresponds to four variables.
Packagecom.test.action;Importjava.util.Date; Public classloginaction{PrivateString username; PrivateString password; Private intAge ; PrivateDate date; //set and get method PublicString Execute () {return"Success"; }}
Then we re-deploy the program, found that the operation is normal, the struts2 automatically help us to complete the conversion, of course, if we fill in the data is not legal, such as the age of the string is filled, then throws an exception.
In fact, for Java built-in data types, STRUTS2 will automatically help us complete the conversion. But for our custom types, we have to convert them manually.
STRUTS2 data type conversion (i)