STRUTS2 data type conversion (i)

Source: Internet
Author: User

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)

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.