最近在學ssh,一直搞不懂$,%,#的區別,做了點小練習,慢慢也懂了一點,將自己所學的也記錄下來吧。
存在一下一個實體entity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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; } } |
在struts2的Action中,寫了如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@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" ; } |
分別在application,session,request,servletContext,valueStack中存入一個person對象,那么在JSP中我們可以按照一下方式獲取:
1
2
3
4
|
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> |
以上代碼所得出的person信息時xiaoming的,即ActionContext.getContext().getContextMap()中存放的信息,通過查詢$的用法,發現$獲取對象的方式是有方式的,即
ActionContext.getContext().getContextMap() > ActionContext.getContext() >ActionContext.getContext().getSession() >ActionContext.getContext().getApplication(),對于不同的scope(范圍)中存在同名對象時,$的查找方式將會按照以上步驟進行,找到即輸出,沒有找到繼續上一級查找,到頂不存在時將輸出null。
那么$的用法為:${scope.object.attribute}
scope的屬性值為request,session,application,默認不寫時將按照上述所說的方案查找,找到即輸出相關屬性值。
在struts標簽中,存一個這樣的:
<s:property value="#application.person"/>
可以看出,此時用到了#號,個人認為,其實#和$的用法完全是一樣的,只要你將需要輸出的對象裝進不同范圍的map(servletContext,request,session和application),在view中展示時,使用<s:property value="#scope.object.attribute">跟$理解完全是一樣的。但是你在使用struts的標簽時,比如:
1
|
< s:textfield name = "person.name" ></ s:textfield > |
完全可以理解為
1
|
<input type= "text" name= "persom.name" id= "person.name" value= "<s:property value=" #person.name "/>" /> |
即struts的標簽已經在HTML的text中給我們封裝了<s:property value="#target.name"/>,可以給我省去很多代碼的。
同理,那么#的用法為:<s:property value="#scope.object.attribute" />
當然完全可以使用struts2給我們定義的標簽,這樣完全可以省去寫過多重復代碼的麻煩。其實#還有其他的用法,比如用來構造map等對象,但是個人覺得在view中寫過多代碼的時代已經過去,這種用法已經沒有太多的意義,況且這次我只寫出在view展示的過程,因此其它地方不扯了。
最后,扯一點%的用法,簡單的看,%{}就是字符串計算表達式,舉個例子,view中存在某個環節,一般都存在CRUD等基本功能,對于add和uppdate功能,完全可以在同一個頁面完成,不同的是我們提交的地址是不同的,比如可能只這樣的:對于add方法,地址為user_add.action,對于udpate方法,地址為user_update.action,那么在form中,可以使用%進行判斷:
1
|
<s:form action= "user_%{ id == 0 ? 'add' : 'update' }" ></form> |
呵呵,這樣以前的兩個頁面現在完全一個頁面可以解決掉。
同理,%與struts中的if,ifelse等判斷標簽聯合起來用得比較多,畢竟是比較的嗎。。。。
1
2
3
4
5
6
7
8
9
|
<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 > |
最后,說說這個%很有用的做法,假設存在一個列表展示student全部及格的成績(即不及格的成績將不會展示在上面),如果使用的%將是非常簡單的。不扯,先上代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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; } /** * 此處判斷成績是否及格 * @param socre * @return */ public boolean isPast( int socre){ return getScore() > 60 ; } } |
那么,現在數據庫中查找學生成績,放到list中暫時存放起來,在JSP頁面,我們可以使用以下代碼來控成績制輸出是否及格:
1
2
3
4
5
6
7
8
|
<s:iterator value= "#allUser" > <!-- 判斷是否過線,過線即輸出,否則舍去! --> <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> |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/u013762572/article/details/44540035