Struts2 type conversion continued

Source: Internet
Author: User

For the previous blog, if multiple points are entered, how can this problem be solved:

1. The input page is as follows. Assume that the names of the three input points are the same.

  1. <% @ Page contenttype = "text/html; charset = GBK" %>
  2. <% @ Taglib prefix = "S" uri = "/Struts-tags" %>
  3. <S: Form Action = "converter" method = "Post">
  4. <S: textfield name = "point" label = "point1"/>
  5. <S: textfield name = "point" label = "point2"/>
  6. <S: textfield name = "point" label = "point3"/>
  7. <S: textfield name = "Age" label = "Age"/>
  8. <S: textfield name = "username" label = "username"/>
  9. <S: textfield name = "date" label = "date"/>
  10. <S: Submit label = "Submit"/>
  11. </S: Form>

2. The coverter class has been modified.

  1. Package edu. HUST. Common;
  2. Import java. util. arraylist;
  3. Import java. util. List;
  4. Import java. util. Map;
  5. Import org. Apache. struts2.util. strutstypeconverter;
  6. Import edu. HUST. Bean. Point;
  7. Public class converter extends strutstypeconverter {
  8. @ Suppresswarnings ("unchecked ")
  9. @ Override
  10. Public object convertfromstring (MAP context, string [] values, class toclass ){
  11. List <point> pointlist = new arraylist <point> ();
  12. For (string value: values ){
  13. Point point = new point ();
  14. String [] paramvalues = value. Split (",");
  15. Int x = integer. parseint (paramvalues [0]);
  16. Int y = integer. parseint (paramvalues [1]);
  17. Point. setx (X );
  18. Point. sety (y );
  19. Pointlist. Add (point );
  20. }
  21. Return pointlist;
  22. }
  23. @ Suppresswarnings ("unchecked ")
  24. @ Override
  25. Public String converttostring (MAP context, object O ){
  26. List <point> pointlist = (list <point>) O;
  27. Stringbuilder sb = new stringbuilder ();
  28. For (point: pointlist ){
  29. SB = sb. append (point. getx (). append (","). append (point. Gety (). append ("<br> ");
  30. }
  31. Return sb. tostring ();
  32. }
  33. }

3. Only one change is allowed to the converteraction class.

  1. /*
  2. * It turns out to be a point. Here we change it to list <point>. I haven't tried to use arrays, but using list is a common practice.
  3. */
  4. Public class converteraction extends actionsupport {
  5. Private list <point> point;
  6. Public list <point> getpoint (){
  7. Return Point;
  8. }
  9. Public void setpoint (list <point> point ){
  10. This. Point = point;
  11. }
  12. }

The other parts are not changed, so that multiple forms with the same name can be submitted simultaneously.

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.