Struts2 form echo principle, struts2 form echo
Question: How does Struts2 help us with form Echo? Solution:
First, write a simple test program.
1) JSP page content is as follows (JSP page name is form-tag.jsp ):
<S: debug> </s: debug> <s: form action = "userAction_save" method = "post"> <s: textfield name = "name" label = "name"> </s: textfield> <s: password name = "password" label = "password"> </s: password> <s: textfield name = "desc" label = ""> </s: textfield> <s: checkbox name = "married" label = "no"> </s: checkbox> <s: submit> </s: form>
2) the Java code is as follows:
package com.mystudy.struts.formtag;public class UserAction {private String name;private String password;private String desc;private boolean married;//private boolean married = true;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}public boolean isMarried() {return married;}public void setMarried(boolean married) {this.married = married;}public String save() throws Exception{System.out.println(this);return "form";}@Overridepublic String toString() {return "UserAction [name=" + name + ", password=" + password+ ", desc=" + desc + ", married=" + married + "]";}}
3) The main configuration of struts. xml is as follows:
<action name="userAction_*" class="com.mystudy.struts.formtag.UserAction" method="{1}"><result name="form">/form-tag.jsp</result></action>
Next, deploy and run it in tomcat, as shown in the following figure:
In this case, open [Debug] to view the content of the object stack in the value stack ValueStack, as shown in. We can see that there is no Action object, because we directly access this JSP page through a link without going through the Action.
After filling out the form and submitting it, you can view the content of [Debug] Again, as shown in. At this time, we can see in the value stack that the Action object has been put into the value stack, and its attributes and attribute values correspond to the JSP form.
==="
Therefore, based on some understanding of the value stack, we can infer that the form echo method of Struts2 is:
Conclusion:
Struts2 searches for the matching attributes of the JSP page from the stack top object of the value stack, and assigns the configured attribute values to the value of the corresponding tag, if the objects at the top of the stack do not have the corresponding attributes, search for the corresponding attributes in sequence.
No Delete reply show all replies show Star reply show score reply questions about form and Action data transmission in Struts2
Unify the encoding of all your locations .. Otherwise, you will encounter this problem in the future ..
Solve this problem separately .. Assume that your variable is named name.
Use String str = new String (name. getBytes ("ISO8859-1"), "encoding method on your page ")
Struts2 get the foreground form value? How does it work?
In struts2, declare a variable private string in the ACTION; then set the get/set Method of the variable name;
Struts2 is automatically obtained during running.
For example, the jsp page contains a text box <input type = 'text' name = 'userid'>
Then declare the variable private string userId in the ACTION; set the get/set method;
At runtime, struts2 automatically obtains the value of the tag named userId in the name attribute and assigns a value to the variable userId in the ACTION. You can use it directly.