Spring学习基础---配置文件(4)_JSP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:Spring学习基础---多框架集成
ApplicationContextctx 1,定义资源文件获得资源文件的消息,国际化信息 beanid=messageResourceclass=org.springFramework.context.support.ResourceBoundleMessageSource propertyname=basenames xxxx /property /bean 将会搜索xxxx.properties,xxxx_


16, 点击分类后,显示分类中的items,点items可以进入viewProduct.do?productId=xxx,来观看产品。
 <bean name="/shop/viewProduct.do" class="org.springframework.samples.jpetstore.web.spring.ViewProductController">
  <property name="petStore" ref="petStore"/>
 </bean>
 这是一个翻页功能的Controller。
 没弄清楚成功后跳转到什么地方? 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,再根据viewResolver中的定义,就可以知道是/jsp/spring/Product.jsp了。
也就是说,viewName也就是jsp文件的名字。

17,ModelAndView传递给页面之后页面如何使用其中的数据 ?
 Controller传递的model是一个map,一共传递了两个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当作attributename放到了request范围内了。这样就ok了,model的key实际上就是request的属性名字啊。
 model的value就是request的属性值。jstl真正发挥简洁的威力了。

18,viewProduct.do里还有一个翻页的逻辑,没看明白怎么回事。


19,viewProduct.do之后再点链接就进入了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. 
 可以排序。可以设置页数。
这个类明显是把所有的结果一次性查询出来后,设定每页个数,之后再把当页数据发送给页面。虽然不是把全部数据发送给页面由页面来分页,但是一次把全部数据都查询出来的做法只适合少量数据。如果多量数据几万条的话同时查出来,存放到session,用不了多久服务器的内存就被耗光了。
还不太清楚放到session中的对象什么时候被晴空,好像只有在退出的时候才晴空一次。

20,addItemToCart.do?workingItemId=EST-11,代码很清楚。有两点主意:
 一,webUtil org.springframework.web.util.webUtil提供了有限的几个方法。
 二,return new ModelAndView("Cart", "cart", cart); // Cart.jsp , key ,value
 因为不熟悉ibatis所以ORM层的代码都没有阅读,也就是PetsoreImpl实现类的各个DAO实例都没有阅读。

    removeItemFromCart.do?workingItemId=EST-11 也是同一页面上的购物车操作 ,过于简单。略
 updateCartQuantities.do //更新的是内存中的数据,所以没有什么技术。

21,checkout.do有一点需要注意,别的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初级教程之跟我学JSP(八)
第八章Blob类型数据的存取和使用第一个Servlet—— 图片文件的操作 以下是我经过改编得到的 jsp 代码: ------------------------------upphoto.htm------------------------------------ html head metahttp-equiv=Content-Typecontent=text/html;charse

共4页上一页1234下一页
来源:模板无忧//所属分类:JSP教程/更新时间:2010-03-08
相关JSP教程