Check boxes are used a lot in web development. Now we use the check box label of struts2 to implement some problems that we often encounter in development.
Let’s take a look at the properties of this tag:
Note: listKey is equivalent to the value attribute in HTML. This value is really what we want to use in the background when interacting with the background; listValue is just a display of content.
The user has selected his favorite course and now needs to modify the course you have selected, jump to the modification interface, and then echo the selected course.
The user has selected the course interface:
Click the button and enter the course modification interface:
Note: In the modification interface, you must echo the user's initial selection.
The user has selected the course interface:
<body> Your course: <form action="checkBoxAction_test.action" method="post"> <s:checkboxlist list="{'Java','.Net','PHP','C++'}" name="courses" /></br> <input type="submit" value="I want to modify"/> </form> </body>action code:
public class CheckBoxAction extends ActionSupport {private static final long serialVersionUID = 1L;/*The course selected by the user (string, need to remove spaces)*/private String courses;public String getCourses() {return courses;}public void setCourses(String courses) {this.courses = courses;}public String test(){/*Delete spaces [The spaces must be removed in this way, trim() does not work] */String courStrs = this.courses.replaceAll(" ", "");/*Define a collection to store the courses selected by the user in the collection*/List<String> userCourses = new ArrayList<String>();/*Travel */for (String str : courStrs.split(",")){userCourses.add(str);}/*Save the courses selected by the user in the OGNL Context context*/ActionContext.getContext().put("userCourses", userCourses);/*Pass all check boxes in the form of a collection to display, and then match them with the ones selected by the user*/List<String> cous = new ArrayList<String>();cous.add("Java");cous.add(".Net");cous.add("PHP");cous.add("C++");/*Save all courses in the OGNL Context context*/ActionContext.getContext().put("cous", cous);return this.SUCCESS;}}Modify the interface:
<body> The course you selected is: <s:checkboxlist name="courses" list="#cous" value="#userCourses" /> </body>
Note: In the modification interface, it is very simple to achieve the echo of the result. In addition, we only need the # attribute name to obtain the values of list and value, because we have already placed the object in the ActionContext, that is, the OGNLContext context in the action.
Here we use a similar case to get closer to the simulation development environment:
The user selects his or her idol (a javaBean object), and then jumps to the modification interface to modify it, and echoes the data in the modification interface.
User has selected idol interface:
Modify the interface (to implement data echo)
User has selected idol interface:
<body> Your idol: <form action="checkBoxListAction.action" method="post"> <s:checkboxlist list="#{1:'Deng Xiaoping',2:'Xi Jinping',3:'Hu Jintao',4:'Li Keqiang'}" name="ids" listKey="key" listValue="value"/></br> <input type="submit" value="I want to modify"/> </form> </body> Note: list is a collection of maps created by OGNL. listKey is equivalent to the real value value of the form submitted. We pass the Map key to the background for processing.
javaBean object:
public class User {/* User id */private Integer uid;/* Username*/private String uname;/* No-argument constructor*/public User() {}/* Constructor*/public User(Integer uid, String uname) {this.uid = uid;this.uname = uname;}public Integer getUid() {return uid;}public void setUid(Integer uid) {this.uid = uid;}public String getUname() {return uname;}public void setUname(String uname) {this.uname = uname;}}action code:
public class CheckBoxListAction extends ActionSupport {private static final long serialVersionUID = 1L;/* Idol collection*/private List<User> users;/* Idol id string*/private String ids;public List<User> getUsers() {return users;}public void setUsers(List<User> users) {this.users = users;}public String getIds() {return ids;}public void setIds(String ids) {this.ids = ids;}public String execute() {/*Simulate to obtain all idols from the database, pass them to the front desk to display, and then compare them with the user's choice*/users = new ArrayList<User>();users.add(new User(1, "Deng Xiaoping"));users.add(new User(2, "Xi Jinping"));users.add(new User(3, "Hu Jintao"));users.add(new User(4, "Li Keqiang"));/*Save in OGNL Context context*/ActionContext.getContext().put("users", users);/*Define a collection to store the idol ids selected by the user*/List<Integer> userIds = new ArrayList<Integer>();/*Get the ids selected by the user and remove spaces [must be used in this way, the trim() method does not work] */ids = ids.replaceAll(" ", "");/*Iteration*/for (String str : ids.split(",")){userIds.add(Integer.valueOf(str));}/*Save ids into the OGNL context set*/ActionContext.getContext().put("userIds", userIds);return this.SUCCESS;}}Modify the interface (echo the data):
<body> Your idol is: <s:checkboxlist name="users" list="#users" listKey="uid" listValue="uname" value="#userIds"/> </body>
Note: listKey and listValue are two properties of javaBean. They will be placed in the value stack when checkboxlist iteration, so there is no need to get it in the form of #namespace. You can get it directly by using the attribute name. The value of the value is the collection of uids in javaBean, which will be matched automatically, and then select the corresponding value.
Attached:
1. If your project does not use freemaker, but the error shown in the figure below appears!
So, congratulations, it is because your struts2 tag has no name attribute! ! ! ! (A lesson from tears, I was cheated by it for two hours!)
The above is all the content of this article about learning the Struts2 checkbox instance code of Java framework, 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!