Check box and Struts2 background interactive code details, struts2 background

Source: Internet
Author: User

Check box and Struts2 background interactive code details, struts2 background

This article focuses on the Content of check boxes in the Struts framework. Check boxes are widely used in web development. The details are as follows.

Case

For example, if the currently selected fruit is "banana", click the button to go to the modification page and modify it.

After you jump to the modification page, you need to display the user's choice (banana), and then the user will check again,

Front-End Interface:

<Body> <form action = "checBoxAction_test.action" method = "post"> select your favorite fruit: <br> <input type = "checkbox" name = "fruits" value = "banana"/> banana <input type = "checkbox" name = "fruits" value = "Sydney" /> Sydney <input type = "checkbox" name = "fruits" value = "watermelon"/> watermelon </br> <input type = "submit" value = "Jump to the modification page modify "> </form> </body>

Background ChecBoxAction. java code:

Public class ChecBoxAction extends ActionSupport {private static final long serialVersionUID = 1L;/* the fruit name selected by the check box on the frontend */private String fruits; public String getFruits () {return fruits ;} public void setFruits (String fruits) {this. fruits = fruits;} public String test () {/* Before spaces are not removed */System. out. println (this. getFruits ();/* Get the string passed through from the foreground (Note: spaces must be removed here, because each value passed in contains a comma separator and a space, however, the trim () method does not remove spaces.) * // String fruitStr = this. getFruits (). trim ();/* must be like this to remove Spaces */String fruitStr = this. getFruits (). replaceAll ("", ""); System. out. println ("String after spaces are removed:" + fruitStr);/* use commas to separate the String into a String array */String [] fruit = fruitStr. split (",");/* traverse all values and save them to a collection */List <String> myFruits = new ArrayList <String> (); for (int I = 0; I <fruit. length; I ++) {myFruits. add (fruit [I]);}/* Save the check box selected by the user to Map and send it to the foreground */ActionContext. getContext (). put ("myFruits", myFruits);/* simulates all values from the database and displays them at the front end, then match with the user selected */List <String> list = new ArrayList <String> (); list. add ("banana"); list. add ("Sydney"); list. add ("watermelon"); ActionContext. getContext (). put ("list", list); return this. SUCCESS ;}}

Note: The check box is used to send a value to the backend. A character string is passed with spaces. Therefore, spaces must be removed, but the trim () method cannot be used to remove them. trim () is used () method. As follows:

, No effect! However, we can use the replaceAll () method to replace spaces. The effect is as follows:

In addition, to display all check boxes (fruits) on the modify interface, we simulate to retrieve all values from the database in the Action, and then upload them together with the check boxes selected by the user to the modify interface.

Modify interface:

<Body> <form action = "checBoxAction_test.action" method = "post"> fruit of your choice: <br> <c: forEach items = "$ {list}" var = "list"> <input type = "checkbox" value = "$ {list}" <c: forEach items = "$ {myFruits}" var = "fr" >$ {fr = list? "Checked": ""} </c: forEach>/>$ {list} </c: forEach> </br> <input type = "submit" value = "modify"/> </form> </body>

Note: The modification interface is complex. First, traverse all check boxes (fruits) and use a forEach loop in each flotation to traverse all check boxes (fruits) selected by the user ), then, use the three-object operator to determine whether the current check box is selected by the user. If yes, select the check box.

Summary

The above is all the details about the check box and Struts2 background interaction code in this article, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.