Spring學(xué)習(xí)基礎(chǔ)---配置文件(4)_JSP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:Spring學(xué)習(xí)基礎(chǔ)---多框架集成ApplicationContextctx 1,定義資源文件獲得資源文件的消息,國際化信息 beanid=messageResourceclass=org.springFramework.context.support.ResourceBoundleMessageSource propertyname=basenames xxxx /property /bean 將會(huì)搜索xxxx.properties,xxxx_
16, 點(diǎn)擊分類后,顯示分類中的items,點(diǎn)items可以進(jìn)入viewProduct.do?productId=xxx,來觀看產(chǎn)品。
<bean name="/shop/viewProduct.do" class="org.springframework.samples.jpetstore.web.spring.ViewProductController">
<property name="petStore" ref="petStore"/>
</bean>
這是一個(gè)翻頁功能的Controller。
沒弄清楚成功后跳轉(zhuǎn)到什么地方? return new ModelAndView("Product", model);沒有理解。
public class ModelAndViewextends ObjectHolder for both Model and View in the web MVC framework. Note that these are entirely distinct. This class merely holds both to make it possible for a controller to return both model and view in a single return value.
Class to represent a model and view returned by a handler used by a DispatcherServlet. The view can take the form of a reference to a View object, or a String view name which will need to be resolved by a ViewResolver object. The model is a Map, allowing the use of multiple data objects keyed by name.
public ModelAndView(String viewName,
Map model)Creates new ModelAndView given a view name and a model.
Parameters:
viewName - name of the View to render, to be resolved by the DispatcherServlet
model - Map of model names (Strings) to model objects (Objects). Model entries may not be null, but the model Map may be null if there is no model data.
這樣viewName就知道了,返回給DispatcherServerlet,再根據(jù)viewResolver中的定義,就可以知道是/jsp/spring/Product.jsp了。
也就是說,viewName也就是jsp文件的名字。
17,ModelAndView傳遞給頁面之后頁面如何使用其中的數(shù)據(jù) ?
Controller傳遞的model是一個(gè)map,一共傳遞了兩個(gè)key-value對。
model.put("itemList", itemList);
model.put("product", product);
ok,看jsp頁面。<c:out value="${product.name}"/>
<c:forEach var="item" items="${itemList.pageList}">
<tr bgcolor="#FFFF88">
<td><b>
<a href="<c:url value="/shop/viewItem.do"><c:param name="itemId" value="${item.itemId}"/></c:url>">
<c:out value="${item.itemId}"/>
</a></b></td>
<td><c:out value="${item.productId}"/></td>
<td>
<c:out value="${item.attribute1}"/>
<c:out value="${item.attribute2}"/>
<c:out value="${item.attribute3}"/>
<c:out value="${item.attribute4}"/>
<c:out value="${item.attribute5}"/>
<c:out value="${product.name}"/>
</td>
<td><fmt:formatNumber value="${item.listPrice}" pattern="$#,##0.00"/></td>
<td><a href="<c:url value="/shop/addItemToCart.do"><c:param name="workingItemId" value="${item.itemId}"/></c:url>">
<img border="0" src="../images/button_add_to_cart.gif"/>
</a></td>
</tr>
</c:forEach>
原來是把key當(dāng)作attributename放到了request范圍內(nèi)了。這樣就ok了,model的key實(shí)際上就是request的屬性名字啊。
model的value就是request的屬性值。jstl真正發(fā)揮簡潔的威力了。
18,viewProduct.do里還有一個(gè)翻頁的邏輯,沒看明白怎么回事。
19,viewProduct.do之后再點(diǎn)鏈接就進(jìn)入了viewItem.do,相對簡單。不用看了。
PagedListHolder itemList = new PagedListHolder(this.petStore.getItemListByProduct(productId));
java.lang.Object
org.springframework.beans.support.PagedListHolder
PagedListHolder is a simple state holder for handling lists of objects, separating them into pages. Page numbering starts with 0.
Constructor Summary
PagedListHolder()
Create a new holder instance.
PagedListHolder(List source)
Create a new holder instance with the given source list, starting with a default sort definition (with "toggleAscendingOnProperty" activated).
PagedListHolder(List source, SortDefinition sort)
Create a new holder instance with the given source list.
boolean isFirstPage()
Return if the current page is the first one.
boolean isLastPage()
Return if the current page is the last one.
void nextPage()
Switch to next page.
void previousPage()
Switch to previous page.
可以排序�?梢栽O(shè)置頁數(shù)。
這個(gè)類明顯是把所有的結(jié)果一次性查詢出來后,設(shè)定每頁個(gè)數(shù),之后再把當(dāng)頁數(shù)據(jù)發(fā)送給頁面。雖然不是把全部數(shù)據(jù)發(fā)送給頁面由頁面來分頁,但是一次把全部數(shù)據(jù)都查詢出來的做法只適合少量數(shù)據(jù)。如果多量數(shù)據(jù)幾萬條的話同時(shí)查出來,存放到session,用不了多久服務(wù)器的內(nèi)存就被耗光了。
還不太清楚放到session中的對象什么時(shí)候被晴空,好像只有在退出的時(shí)候才晴空一次。
20,addItemToCart.do?workingItemId=EST-11,代碼很清楚。有兩點(diǎn)主意:
一,webUtil org.springframework.web.util.webUtil提供了有限的幾個(gè)方法。
二,return new ModelAndView("Cart", "cart", cart); // Cart.jsp , key ,value
因?yàn)椴皇煜batis所以O(shè)RM層的代碼都沒有閱讀,也就是PetsoreImpl實(shí)現(xiàn)類的各個(gè)DAO實(shí)例都沒有閱讀。
removeItemFromCart.do?workingItemId=EST-11 也是同一頁面上的購物車操作 ,過于簡單。略
updateCartQuantities.do //更新的是內(nèi)存中的數(shù)據(jù),所以沒有什么技術(shù)。
21,checkout.do有一點(diǎn)需要注意,別的Controller沒有傳入viewName。它傳了,
<bean name="/shop/checkout.do" class="org.springframework.samples.jpetstore.web.spring.ViewCartController">
<property name="successView" value="Checkout"/>
</bean>
Controller中:
private String successView;
public void setSuccessView(String successView) {
this.successView = successView;
}
最后return new ModelAndView(this.successView, "cart", cart);
分享:JSP初級教程之跟我學(xué)JSP(八)第八章Blob類型數(shù)據(jù)的存取和使用第一個(gè)Servlet—— 圖片文件的操作 以下是我經(jīng)過改編得到的 jsp 代碼: ------------------------------upphoto.htm------------------------------------ html head metahttp-equiv=Content-Typecontent=text/html;charse
相關(guān)JSP教程:
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復(fù)習(xí)整理
- JSP腳本元素和注釋復(fù)習(xí)總結(jié)示例
- JSP FusionCharts Free顯示圖表 具體實(shí)現(xiàn)
- 網(wǎng)頁模板:關(guān)于jsp頁面使用jstl的異常分析
- JSP頁面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項(xiàng)目中連接Access數(shù)據(jù)庫的配置方法
- JDBC連接Access數(shù)據(jù)庫的幾種方式介紹
- 網(wǎng)站圖片路徑的問題:絕對路徑/虛擬路徑
- (jsp/html)網(wǎng)頁上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對路徑下的圖片解決方法
- 相關(guān)鏈接:
- 教程說明:
JSP教程-Spring學(xué)習(xí)基礎(chǔ)---配置文件(4)
。