一、<s:select/> struts2下拉框標籤:
<s:select/>裏有好多屬性,其中list這個屬性時必須的,因爲它是數據源,它的形式可以使List,Map,Set等。這個list我們可以寫成死的,也可以動態的獲取。
動態的獲取就會出現兩種情況。首先,我們知道,struts2表單裏各項和Action裏的屬性是對應的,比如表單裏的username,password輸入框,在Action裏就要有username和password對應的屬性,或者乾脆在Action裏封裝成一個user對象。同樣的,這時在Action裏也要有個List屬性。它裏面存了要在下拉框裏顯示的數據。
第二種情況是,這個數據源是從本頁面或者其他頁面獲得的:
<%
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
ManagerInterface manImpl = (ManagerInterface)ctx.getBean("managerImpl");
List<Menu> lst_menu = manImpl.selectMenu();
request.setAttribute("lst_menu",lst_menu); //下拉框接收
%>
表單裏:
<s:select name="m_id" list="#request.lst_menu" listKey="m_id" listValue="m_name" headerKey="0" headerValue="-請選菜名-"/>
另外關於<s:select/>的問題,請參考最後一段點擊打開鏈接,我也遇到了動態的獲取的問題。博主說是因爲會出現空格空字符串的原因。解決方法是去空格。
二、struts2的時間選擇器:
1.導入struts2-dojo-plugin-2.1.6.jar包
2.在JSP頁面添加標籤:<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
3.在<head></head>中加入<sx:head/>
4.現在就可以使用時間選擇器的標籤了
<sx:datetimepicker name="startDate" displayFormat="yyyy-MM-dd" required="true" toggleDuration="200" toggleType="explode" />
關於datetimepicker 標籤裏詳細的屬性,用的時候查文檔,查資料。
三、hibernate裏HQL查詢結果的返回類型問題:
若查詢單個字段,則返回類型爲List<Object>。若查詢多個字段,則返回類型爲List<Object[ ]>,obj[0]爲字段A,obj[1]爲字段B,obj[2]爲字段C......
若查詢整個對象,則返回類型爲List,默認返回封裝好的對象。
可以參考這篇博文點擊打開鏈接
在Jsp界面,也不能通過new 來新建一個對象,必須統一用applicationCopntext.xml.而且不能新建出來的對象類型若是實現類,則它前面的累心要寫被實現的接口或父類。
注意:在加了jfreeChart的jar包以後,要刪除 gnujaxp.jar 包,不然會出現找不到JSP界面問題。
struts2的標籤和html的標籤是完全不同,struts2的標籤是運行在服務器的,而html的是運行在web前端的。所以:<s:hidden name="m.id" value="<%=intId %>"/>這是錯誤的。<%= %>是JSP,web前端的東西。
最終解決這個問題的方法是最笨的
out.println(" <tr>");
out.println( "<td><input type=\"hidden\" name=\"m.id\" value=\""+intId+"\"/>"+intId+"</td>");
struts2標籤的高級應用
.struts2標籤截取字符串
<s:if test="%{xxx.length()<5}">
<s:property value="#xxx" />
</s:if>
<s:else>
<s:property value="xxx.substring(0,3)+'<br/>'xxx.substring(3,xxx.length())" escape="false"/>
</s:else>
2.用於取到後臺的時間:
<s:date name=”#xxx.date” fromate=”YYYY-MM-DD hh:mm”/>
3.判斷是否爲空字符串或者爲null
<s:if test="xxx.name==null||''.equals(#xxx.name.trim())">
4.struts2寫隱藏表單(便於JS調用)
<input id="log" value='<s:property value="log"/>' type="hidden" />
5.將某個特定resource bundle放入value stack. 然後通過text標籤拿到相應message, 而不是僅限於綁定到當前action的bundle.
<span style="font-family:Arial;"><s:i18n name=<span class="code-quote">"myCustomBundle"</span>> <s:property value=<span class="code-quote">"text('aaa.bbb.ccc')"</span> /> </s:i18n></span>
<span style="font-family:Arial;">6.導入jsp(servlet或JSP頁面)</span>
<span style="font-family:Arial;"><s:include value=<span class="code-quote">"myJsp.jsp"</span>> <s:param name=<span class="code-quote">"param1"</span> value=<span class="code-quote">"value2"</span> /> <s:param name=<span class="code-quote">"param2"</span> value=<span class="code-quote">"value2"</span> /> </s:include></span>
<span style="font-family:Arial;">7.用傳入的 Comparator對List進行排序</span><pre class="code-java" name="code"><span style="font-family:Arial;"><s:sort id=<span class="code-quote">"mySortedList"</span> comparator=<span class="code-quote">"myComparator"</span> source=<span class="code-quote">"myList"</span> /> <% Iterator sortedIterator = (Iterator) pageContext .getAttribute(<span class="code-quote">"mySortedList"</span>); <span class="code-keyword">for</span> (Iterator i = sortedIterator; i.hasNext(); ) { </span><span style="font-family:Arial;"><span class="code-comment">// <span class="code-keyword">do</span> something with each of the sorted elements </span>} %></span>
怎麼把map遍歷,把裏面的值取出來