This article mainly studies the relevant content of the check boxes in the Struts framework. Check boxes are widely used in web development, as detailed below.
As shown in the figure below, the fruit currently selected by the user is "Banana". Click the button and jump to the modification interface to modify it.
After jumping to the modification interface, you must echo the user's selection (banana), and then the user checks it again, as shown in the figure:
Front desk interface:
<body> <form action="checBoxAction_test.action" method="post"> Please select your favorite fruit:<br> <input type="checkbox" name="fruits" value="banana"/> Banana<input type="checkbox" name="fruits" value="Snow Pear"/> Sydney<input type="checkbox" name="fruits" value="Waste Melon"/> Watermelon</br> <input type="submit" value="Skip to the modification interface to modify"> </form> </body>
Background ChecBoxAction.java code:
public class ChecBoxAction extends ActionSupport {private static final long serialVersionUID = 1L;/*Fruit name selected by the front desk through the check box*/private String fruits;public String getFruits() {return fruits;}public void setFruits(String fruits) {this.fruits = fruits;}public String test(){/*Before spaces are removed*/System.out.println(this.getFruits());/*Get the string that passes from the foreground (Note: spaces must be removed here, because there are spaces in addition to comma separators, but the spaces cannot be removed by trim())*///String fruitStr = this.getFruits().trim(); /*This must be done to remove spaces*/String fruitStr = this.getFruits().replaceAll(" ", "");System.out.println("String after spaces are removed:" + fruitStr);/*Separate the string into a string array by commas*/String[] fruit = fruitStr.split(",");/*Transfer all values and save them in a collection*/List<String> myFruits = new ArrayList<String>(); for (int i=0; i<fruit.length; i++){myFruits.add(fruit[i]);}/*Save the checkbox selected by the user to the Map and send it to the foreground*/ActionContext.getContext().put("myFruits", myFruits);/*Simulate to find all values from the database, display them in the foreground, and then match them with the user's selected */List<String> list = new ArrayList<String>();list.add("banana");list.add("Snow pear");list.add("watermelon");ActionContext.getContext().put("list", list);return this.SUCCESS;}}Note: The check box passes a value to the background. The passed string is a string with spaces, so the spaces must be removed, but it cannot be removed with the trim() method. The effect after using the trim() method. as follows:
As shown in the picture, there is no effect! However, we can use the replaceAll() method to replace spaces, and the effect is as follows:
In addition, in order to display all check boxes (fruits) in the modification interface, we simulate taking out all values from the database in Action, and then pass them to the modification interface together with the check boxes selected by the user.
Modify the interface:
<body> <form action="checBoxAction_test.action" method="post"> The fruit you selected:<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 quite complicated. First, it is to traverse all check boxes (fruits), and use a forEach loop in each flotation to traverse all check boxes (fruits) selected by the user, and then use the three-item operator to determine whether the current check box is selected by the user. If it matches, check it.
The above is all the detailed explanation of the checkbox and Struts2 background interaction code in this article. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!