I have been learning ssh recently, and I have not understood the difference between $,%,%, and#. I have done some small exercises and gradually understood a little. I will record what I have learned.
The following entity exists:
public class Person { private int id ; private String Name ; public int getId() { return id; } public Person(int id, String name) { super(); this.id = id; Name = name; } public Person() { super(); } public void setId(int id) { this.id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } } In the action of struts2, the following code is written:
@Override public String execute() throws Exception { //application Person p = new Person(1,"zhangsan"); ActionContext.getContext().getApplication().put("person", p); //session Person p1 = new Person(3,"wangwu"); ActionContext.getContext().getSession().put("person", p1); //request Person p2 = new Person(2,"lisi"); ActionContext.getContext().put("person", p2) ; //servletContext Person p3 = new Person(5,"xiaoming"); ActionContext.getContext().getContextMap().put("person", p3); Person p4 = new Person(3,"wangwu"); ActionContext.getContext().getValueStack().push(p4); return "success"; } Store a person object in application, session, request, servletContext, valueStack, respectively. Then in JSP we can obtain it in the following way:
person: <input type="text" name="name" value="${person }" /><br /> id: <input type="text" name="name" value="${person.id }" /><br /> name: <input type="text" name="name" value="${person.name }" /><br /> <hr> The person information obtained by the above code is xiaoming, that is, the information stored in ActionContext.getContext().getContextMap(). By querying the usage of $, it is found that there is a way to get the object of $, that is,
ActionContext.getContext().getContextMap() > ActionContext.getContext() > ActionContext.getContext().getSession() > ActionContext.getContext().getApplication(). For objects with the same name exist in different scopes (scopes), the search method of $ will be carried out according to the above steps. If it is found, it will be output. If it is not found, it will continue to search at the previous level. When the top does not exist, it will output null.
Then the usage of $ is: ${scope.object.attribute}
The attribute value of scope is request, session, application. When not written by default, you will search according to the above scheme. If you find it, you will output the relevant attribute value.
In the struts tag, save a:
<s:property value="#application.person"/>
It can be seen that the # number was used at this time. I personally think that the usage of # and $ is actually the same. As long as you load the object you need to output into a map (servletContext, request, session and application) in a different range, when displaying it in the view, using <s:property value="#scope.object.attribute"> is exactly the same as $ understanding. But when you use struts tags, for example:
<s:textfield name="person.name"></s:textfield>
It can be understood completely as
<input type="text" name="persom.name" id="person.name" value="<s:property value="#person.name"/>" />
That is, the struts tag has been encapsulated for us in the HTML text <s:property value="#target.name"/>, which can save me a lot of code.
Similarly, the usage of # is: <s:property value="#scope.object.attribute" />
Of course, you can use the tags defined by struts2 to save the hassle of writing too many duplicate codes. In fact, # has other uses, such as constructing objects such as maps, but I personally think that the era of writing too much code in the view has passed, and this usage has no meaning. Moreover, this time I only wrote the process of displaying it in the view, so I won't talk about it elsewhere.
Finally, let’s talk about the usage of %. To put it simply, %{} is a string calculation expression. For example, there is a certain link in the view, which generally has basic functions such as CRUD. For the add and update functions, it can be completed on the same page. The difference is that the address we submit is different. For example, it may be just like this: For the add method, the address is user_add.action, and for the udpate method, the address is user_update.action, so in form, % can be used for judgment:
<s:form action="user_%{ id == 0 ? 'add' : 'update' }"></form> Haha, so the previous two pages can now be solved by one page.
Similarly, % is used more frequently with the judgment labels such as if and ifelse in struts. After all, is it a comparison? . . .
<s:if test="%{false}"> <div>Will Not Be Executed</div> </s:if> <s:elseif test="%{true}"> <div>Will Be Executed</div> </s:elseif> <s:else> <div>Will Not Be Executed</div> </s:else> Finally, let’s talk about the useful approach of this %, assuming there is a list showing all the students’ passing grades (i.e., the failed grades will not be displayed above), if the % used will be very simple. No, just put the code first:
public class Stduent implements java.io.Serializable{ private static final long serialVersionUID = -691038814755396419L; private int id ; private String name ; private int score ; private String subject ; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } /** * Here we judge whether the score passes the exam* @param socre * @return */ public boolean isPast(int socre){ return getScore() > 60 ; } } So, now look up students' grades in the database and put them in the list for temporary storage. On the JSP page, we can use the following code to control whether the grade system output passes:
<s:iterator value="#allUser"> <!-- Determine whether to pass the line, if you pass the line, it will be output, otherwise you will give it up! --> <s:if test="#session.user.isPast(score)"> name: <s:textfield name="name"></s:textfield> score: <s:textfield name="score"></s:textfield>/ subject:<s:textfield name="subject"></s:textfield> </s:if> </s:iterator>
Thank you for reading, I hope it can help you. Thank you for your support for this site!