In fact, I didn’t have a specific understanding of Java’s Struts framework before. Through this article, I really understood the uses and benefits of the framework.
The drop-down box is often used in web development. Below we use the struts2 tag to implement some functions of the drop-down box.
Let’s take a look at some parameters of <s:select> :
Note: listKey is equivalent to the value value in HTML, and we interact with the background through its value.
Case: The user selects his idol, and then needs to modify it. After jumping to the modification interface, he needs to echo the idol the user originally selected.
User idol interface (assuming that the user's current choice is "Xi Jinping"):
<form action="selectAction.action" method="post"> Your idol is: <s:select list="#{1:'Deng Xiaoping',2:'Hu Jintao',3:'Xi Jinping',4:'Li Keqiang'}" name="id" listKey="key" listValue="value"/> <input type="submit" value="I want to modify"> </form> </body>Note: list uses OGNL to create Map collections.
action code:
public class SelectAction extends ActionSupport {private static final long serialVersionUID = 1L;/*id of the idol selected by the user*/private String id;/*idol collection*/private List<User> users;public String getId() {return id;}public void setId(String id) {this.id = id;}public List<User> getUsers() {return users;}public void setUsers(List<User> users) {this.users = users;}public String execute(){/*Create a collection simulation to find all users from the database*/users = new ArrayList<User>();users.add(new User(1, "Deng Xiaoping"));users.add(new User(2, "Hu Jintao"));users.add(new User(3, "Xi Jinping"));users.add(new User(4, "Li Keqiang"));/*Save all users in the OGNL Context context*/ActionContext.getContext().put("users", users);//Note: There is no need to save the value selected by the user here, because our id attribute is the value selected by the user. //As an action attribute, it has been saved to the value stack. You can directly obtain the return this.SUCCESS according to the attribute name id;}}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;}}Modify the interface:
<body> Your idol is: <s:select list="#users" listKey="uid" listValue="uname" value="id" /> </body>
Note: The value value only writes the attribute name of the id (id of the idol selected by the user) because it is an attribute in action. The attribute in action will be saved to the value stack, so it can be obtained directly.
I realized the benefits of frameworks for the first time and don’t blame me for being shallow. .
The above is all the content of this article about the analysis of the Struts2 drop-down box example, 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!