spring mvc循序渐进(5)
关键字: spring第十六步——为业务逻辑添加一些类
到目前位置,我们运用程序的实用性还不强。我们将要添加一个商品类Product和一个管理所有商品的类ProductManager。为了分离网站和业务,我们将用两个包分离java代码——网站和业务。如果这是某个公司的运用程序,我就把程序包命名为类似于com.mycompany.web和com.mycompany.bus,但到目前为止这仅仅是个演示程序,所以程序包名称将保持简短。产品类将以javaBean实现,就是用getter和setter的那种。同时使它可序列化,虽然我们的程序还不需要,但如果我们要在不同的程序层之间传递这个类,我们将很快用到。
springapp/src/bus/Product.java
- package bus;
- import java.io.Serializable;
- public class Product implements Serializable
- {
- private String description;
- private Double price;
- public void setDescription(String s)
- {
- description = s;
- }
- public String getDescription()
- {
- return description;
- }
- public void setPrice(Double d)
- {
- price = d;
- }
- public Double getPrice()
- {
- return price;
- }
- }
产品管理类ProductManager 保存这一个商品的List,除此之外它也是个JavaBean。
springapp/src/web/SpringappController.java
- package bus;
- import java.io.Serializable;
- import java.util.List;
- public class ProductManager implements Serializable
- {
- private List products;
- public void setProducts(List p)
- {
- products = p;
- }
- public List getProducts()
- {
- return products;
- }
- }
下一步,我修改SpringappController让它控制一个到ProductManager 的链接。正如你所见,它现在在一个分离的包web里——要记得把源代码移到这个新的位置。我同时添加了一些把产品信息传到视图的代码。getModelAndView 方法返回一个装着日期、时间和产品管理器的Map。
springapp/src/web/SpringappController.java
- import org.springframework.web.servlet.mvc.Controller;
- import org.springframework.web.servlet.ModelAndView;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.Map;
- import java.util.HashMap;
- import bus.Product;
- import bus.ProductManager;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class SpringappController implements Controller
- {
- /** Logger for this class and subclasses */
- protected final Log logger = LogFactory.getLog(getClass());
- private ProductManager prodMan;
- public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- String now = (new java.util.Date()).toString();
- logger.info("returning hello view with " + now);
- Map myModel = new HashMap();
- myModel.put("now", now);
- myModel.put("products", getProductManager().getProducts());
- return new ModelAndView("hello", "model", myModel);
- }
- public void setProductManager(ProductManager pm) {
- prodMan = pm;
- }
- public ProductManager getProductManager() {
- return prodMan;
- }
- }
第十七步——修改视图让它显示业务数据同时添加代码让它支持消息池(message bundle)
使用JSTL的<c:forEach>标签,我添加了一小片显示产品信息的代码。我同时用JSTL的 <fmt:message>标把title由“heading and greeting”换成消息池里的信息——这个代码将在稍后显示。
springapp/war/WEB-INF/jsp/hello.jsp
- <%@ include file="/WEB-INF/jsp/include.jsp" %>
- <html>
- <head>
- <title><fmt:message key="title"/></title>
- </head>
- <body>
- <h1><fmt:message key="heading"/></h1>
- <p><fmt:message key="greeting"/> <c:out value="${model.now}"/></p>
- <h3>Products</h3>
- <c:forEach items="${model.products}" var="prod">
- <c:out value="${prod.description}"/> <i>$<c:out value="${prod.price}"/></i><br><br>
- </c:forEach>
- </body>
- </html>
发表评论
- 浏览: 7622 次

- 详细资料
搜索本博客
最新评论
-
spring mvc循序渐进(6)
辞职了,没时间写了
-- by aninfeel -
spring mvc循序渐进(6)
楼主继续后面的哈,测试那里有点没看懂
-- by gowhere -
javascript控制html dom
Dom.这是Dom规范要实现的东西. 不只Javascript, 在.net里操 ...
-- by 白发红颜 -
javascript控制html dom
像getElementById(),getElementsByTagName() ...
-- by just4you -
javascript控制html dom
w3cshcool办得不错,其文章言骇意简--当初学XML,用WEBZIP把整个 ...
-- by sp42






评论排行榜