Struts 2 $, #,% detailed explanation and example code

I've been learning SSH recently. I haven't been able to understand the difference between $,%, # and. I've done a little practice. I've learned a little slowly. Record what I've learned.

The following entity exists:

In the action of struts 2, the following code is written:

Store a person object in application, session, request, ServletContext and ValueStack respectively, then we can get it in JSP in the following way:

The person information obtained from the above code is Xiaoming, that is, actioncontext getContext(). According to the information stored in getcontextmap(), by querying the usage of $, it is found that there are ways for $to obtain objects, that is

ActionContext. getContext(). getContextMap() > ActionContext. getContext() >ActionContext. getContext(). getSession() >ActionContext. getContext(). Getapplication(). When there are objects with the same name in different scopes (ranges), the search method of $will follow 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. If it does not exist at the top, it will output null.

Then the usage of $is ${scope. Object. Attribute}

The attribute values of scope are request, session and application. When it is not written by default, it will be searched according to the above scheme, and the relevant attribute values will be output.

In the struts tag, save the following:

It can be seen that the # sign is used at this time. Personally, the usage of # and $is exactly the same. As long as you load the objects to be output into maps (ServletContext, request, session and application) in different ranges, the use of < s: property value = "#scope. Object. Attribute" > in view is exactly the same as the understanding of $. But when you use struts tags, for example:

It can be understood as

That is, the struts tag has encapsulated < s: property value = "#target. Name" / > in HTML text, which can save me a lot of code. Similarly, the usage of # is: < s: property value = "#scope. Object. Attribute" / >

Of course, we can use the tags defined by struts 2, which can completely save the trouble of writing too much duplicate code. In fact # there are other uses, such as to construct objects such as map, but I personally think that the era of writing too much code in the view has passed, and this use has no more meaning. Besides, I only write the process displayed in the view this time, so I won't talk about other places.

Finally, the usage of% {} is simply a string calculation expression. For example, there is a link in the view. Generally, crud and other basic functions exist. For the add and update functions, they can be completed on the same page. The difference is that the address we submit is different. For example, it may only be like this: for the add method, the address is user_ add. Action. For udpate method, the address is user_ update. Action, then% can be used to judge in the form:

Hehe, in this way, the previous two pages can be solved by one page.

Similarly,% is used more in combination with if, ifelse and other judgment tags in struts. After all, is it comparative....

Finally, let's talk about this% very useful practice. Suppose there is a list showing all students' passing grades (that is, the failing grades will not be displayed above). If you use% it will be very simple. No, code first:

Now, find the students' scores 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 score system output is qualified or not:

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>