- 浏览: 16621722 次
- 性别:
- 来自: 济南
最新评论
-
wu1236:
ef0793cd94337324b6fefc4c9474af5 ...
Android ApiDemos示例解析(87):Media->MediaPlayer -
77219634:
0127bf2236bee4dd1f632ce430f1af1 ...
本博客文章都为转载,没有任何版权! -
77219634:
0127bf2236bee4dd1f632ce430f1af1 ...
VPLEX - EMC的RAC -
77219634:
0127bf2236bee4dd1f632ce430f1af1 ...
qTip2 Show -
77219634:
0127bf2236bee4dd1f632ce430f1af1 ...
SecureCRT中文乱码、复制粘贴乱码解决办法(修改版)
JBuilder开发Spring实例
1. 下载spring包,网址如下
http://www.springframework.org/download.html
解压后的目录中包含了dist、lib等子目录
2. 在JBuilder2005中增加spring库,选择菜单Tools-Configure-Libraries,在弹出的对话框中点击New按钮,输入spring库的名称:spring,点击Add按钮,将dist目录中的所有jar文件增加到spring库中
3. 新建工程文件,选菜单File-New Project,给工程文件取名为myProject
设置工程文件的属性,选菜单Project-Project Properties,选择Tomcat作为服务器,将spring库加入Path/Required Libraries。因为例子中用到了log4j,将包含了log4j的库加入Path/Required Libraries,注意到lib目录下有两个子目录log4j和jakarta-commons,它们的用法是不同的,如下面的代码片断所示:
log4j
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
static Logger logger = Logger.getLogger(SpringappController.class);
jakarta-commons
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());
4. 新建Web模块,选菜单File-New,给Web模块取名为springapp
5. 新建hello.jsp文件如下:
<%@ page contentType="text/html; charset=Big5" %>
<html>
<head><title>Example :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings.</p>
</body>
</html>
6. 新建类文件SpringappController.java如下:
package spring;
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 org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
// protected final Log logger = LogFactory.getLog(getClass());
static Logger logger = Logger.getLogger(SpringappController.class);
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("SpringappController - returning hello view");
return new ModelAndView("hello.jsp");
}
}
7. 修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
8. 在web-inf目录中新建springapp-servlet.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="springappController" class="spring.SpringappController"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
</props>
</property>
</bean>
</beans>
9. 在web-inf目录中新建log4j.properties文件如下:
log4j.rootCategory=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=springapp.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#Pattern to output : date priority [category] - <message>line_separator
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
10. 设置web模块springapp的属性Content,加入文件log4j.properties,因为classes目录是自动产生的,这一步是为了把文件log4j.properties加到classes目录。
11. 设置Run Configuration,选菜单Run-Configurations,新建一个Run Configuration,Type选Server,Launch URI设置为:/springapp/hello.htm,给它取名为hello
12. 运行,你会在messages窗口中看到如下信息:
資訊: Server startup in 9253 ms
2004-11-05 15:05:00,585 INFO [spring.SpringappController] - <SpringappController - returning hello view>
2004/11/5 下午 03:05:00 org.springframework.web.servlet.view.AbstractCachingViewResolver resolveViewName
資訊: Cached view 'hello.jsp'
在myProject/Tomcat/springapp.log文件中增加了一行如下所示:
2004-11-05 15:11:32,348 INFO [spring.SpringappController] - <SpringappController - returning hello view>
这说明你已成功建立了基本的spring应用。
13. 增加对jstl的支持,设置工程文件的属性,选菜单Project-Project Properties,将jstl库加入Path/Required Libraries。
14. 改进前面的例子,改进的文件如下:
增加“header”文件include.jsp,这是一些jsp文件的公用部分,这样可以使得开发和维护更容易。所有jsp文件放在web-inf/jsp目录下,为的是只有Controller可以访问View。
springapp/war/WEB-INF/jsp/include.jsp
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
利用JSTL,<c:redirect>将页面重定向到Controller,这样就建立了index.jsp和应用架构的联系。
springapp/war/index.jsp
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%-- Redirected because we can't set the welcome page to a virtual URL. --%>
<c:redirect url="/hello.htm"/>
利用JSTL的<c:out>标记,把从Model提取的,传递给View的当前日期和时间作为输出。
springapp/war/WEB-INF/jsp/hello.jsp
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<html>
<head><title>Hello :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings, it is now <c:out value="${now}"/>
</p>
</body>
</html>
在SpringappController.java中增加包含了当前日期和时间的string作为Model
springapp/src/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 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());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new java.util.Date()).toString();
logger.info("returning hello view with " + now);
return new ModelAndView("WEB-INF/jsp/hello.jsp", "now", now);
}
}
15. 浏览改进后的结果http://localhost:8080/springapp,首先访问index.jsp,然后重定向到hello.htm,接著把控制交给Controller,Controller把日期和时间传送给View。
只能在浏览器这样测试。在JBuilder2005中设置Run Configuration不能访问http://localhost:8080/springapp,但可以访问http://localhost:8080/springapp/index.jsp
在上面的例子中,Controller指定了View的完全路径,使得Controller和View之间存在不必要的依赖。为了移出这种依赖,我们可以用类ResourceBundleViewResolver 和 a SimpleUrlHandlerMapping在属性文件中定义这种依赖,对于简单的情形,我们也可以利用InternalResourceViewResolver设置前缀和后缀,下面就用第二种方法。修改文件springapp-servlet.xml包含ViewResolver设置,我们还选择使用JstlView,这使得我们可以结合使用JSTL和message resource bundles,因此可以支持国际化。
springapp/war/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="springappController" class="SpringappController"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
现在,我们就可以移出Controller中View名称的前缀和后缀了。
springapp/src/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 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());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new java.util.Date()).toString();
logger.info("returning hello view with " + now);
return new ModelAndView("hello", "now", now);
}
}
这样修改后的例子应该仍能运行。
16. 增加商业逻辑类
为了分开Web逻辑和商业逻辑,我们建立两个不同的包,web和bus
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;
}
}
springapp/src/bus/ProductManager.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.java
springapp/src/web/SpringappController.java
package web;
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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import bus.Product;
import bus.ProductManager;
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;
}
}
17. 修改View以便显示商业数据,增加对message bundle的支持
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>
18. 增加一些测试数据
我们现在还不打算增加代码,从数据库装载商业对象。我们只springapp-servlet.xml中增加bean和messageSource设置,提供一些测试数据和对messages resource bundle的支持。
springapp/war/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="springappController" class="web.SpringappController">
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<bean id="prodMan" class="bus.ProductManager">
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
</bean>
<bean id="product1" class="bus.Product">
<property name="description"><value>Lamp</value></property>
<property name="price"><value>5.75</value></property>
</bean>
<bean id="product2" class="bus.Product">
<property name="description"><value>Table</value></property>
<property name="price"><value>75.25</value></property>
</bean>
<bean id="product3" class="bus.Product">
<property name="description"><value>Chair</value></property>
<property name="price"><value>22.79</value></property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"><value>messages</value></property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
19. 增加message bundle
springapp/war/WEB-INF/classes/messages.properties
title=SpringApp
heading=Hello :: SpringApp
greeting=Greetings, it is now
20. 浏览改进后的结果http://localhost:8080/springapp
实现数据库持久层
21. 设置HSQL数据库,在JBuilder2005中增加HSQL库,选择菜单Tools-Configure-Libraries,在弹出的对话框中点击New按钮,输入HSQL库的名称:HSQL,点击Add按钮,将lib/hsqldb目录下的文件hsqldb.jar增加到HSQL库中。
选择菜单Enterprise-Enterprise Setup,在弹出的对话框中选择Database Drivers,按Add按钮增加HSQL库。
选择菜单Tools-Database Pilot,然后选择菜单Files-New输入Driver:org.hsqldb.jdbcDriver,URL:jdbc:hsqldb:db/test, 双击hsqldb:db/test,输入UserName:sa,不必输入Passsword。输入如下的SQL语句并执行
CREATE TABLE products (
id INTEGER NOT NULL PRIMARY KEY,
description varchar(255),
price decimal(15,2)
);
CREATE INDEX products_description ON products(description);
INSERT INTO products (id, description, price) values(1, 'Lamp', 5.78);
INSERT INTO products (id, description, price) values(2, 'Table', 75.29);
INSERT INTO products (id, description, price) values(3, 'Chair', 22.81);
这样就会在JBuilder_Home/bin目录下创建一个目录db,存放了数据库test的数据
22. 创建JDBC DAO (Data Access Object)实现
springapp/src/db/ProductManagerDao.java
package db;
import bus.Product;
import java.util.List;
public interface ProductManagerDao {
public List getProductList();
public void increasePrice(Product prod, int pct);
}
springapp/src/db/ProductManagerDaoJdbc.java
package db;
import bus.Product;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.jdbc.core.SqlParameter;
public class ProductManagerDaoJdbc implements ProductManagerDao {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private DataSource ds;
public List getProductList() {
logger.info("Getting products!");
ProductQuery pq = new ProductQuery(ds);
return pq.execute();
}
public void increasePrice(Product prod, int pct) {
logger.info("Increasing price by " + pct + "%");
SqlUpdate su =
new SqlUpdate(ds, "update products set price = price * (100 + ?) / 100 where id = ?");
su.declareParameter(new SqlParameter("increase", Types.INTEGER));
su.declareParameter(new SqlParameter("ID", Types.INTEGER));
su.compile();
Object[] oa = new Object[2];
oa[0] = new Integer(pct);
oa[1] = new Integer(prod.getId());
int count = su.update(oa);
logger.info("Rows affected: " + count);
}
public void setDataSource(DataSource ds) {
this.ds = ds;
}
class ProductQuery extends MappingSqlQuery {
ProductQuery(DataSource ds) {
super(ds, "SELECT id, description, price from products");
compile();
}
protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Product prod = new Product();
prod.setId(rs.getInt("id"));
prod.setDescription(rs.getString("description"));
prod.setPrice(new Double(rs.getDouble("price")));
return prod;
}
}
}
springapp/src/bus/Product.java
package bus;
import java.io.Serializable;
public class Product implements Serializable {
private int id;
private String description;
private Double price;
public void setId(int i) {
id = i;
}
public int getId() {
return id;
}
public void setDescription(String s) {
description = s;
}
public String getDescription() {
return description;
}
public void setPrice(Double d) {
price = d;
}
public Double getPrice() {
return price;
}
}
springapp/src/test/TestProductManagerDaoJdbc.java
package tests;
import java.util.List;
import java.util.ArrayList;
import junit.framework.TestCase;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import db.ProductManagerDaoJdbc;
import bus.Product;
public class TestProductManagerDaoJdbc extends TestCase {
private ProductManagerDaoJdbc pmdao;
public void setUp() {
pmdao = new ProductManagerDaoJdbc();
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:db/test");
ds.setUsername("sa");
ds.setPassword("");
pmdao.setDataSource(ds);
}
public void testGetProductList() {
List l = pmdao.getProductList();
Product p1 = (Product) l.get(0);
assertEquals("Lamp", p1.getDescription());
Product p2 = (Product) l.get(1);
assertEquals("Table", p2.getDescription());
}
public void testIncreasePrice() {
List l1 = pmdao.getProductList();
Product p1 = (Product) l1.get(0);
assertEquals(new Double("5.78"), p1.getPrice());
pmdao.increasePrice(p1, 10);
List l2 = pmdao.getProductList();
Product p2 = (Product) l2.get(0);
assertEquals(new Double("6.36"), p2.getPrice());
}
}
23. 修改Web应用使其使用数据库持久层
springapp/src/bus/ProductManager.java
package bus;
import java.io.Serializable;
import java.util.ListIterator;
import java.util.List;
import db.ProductManagerDao;
public class ProductManager implements Serializable {
private ProductManagerDao pmd;
private List products;
public void setProductManagerDao(ProductManagerDao pmd) {
this.pmd = pmd;
}
/*
public void setProducts(List p) {
products = p;
}
*/
public List getProducts() {
products = pmd.getProductList();
return products;
}
public void increasePrice(int pct) {
ListIterator li = products.listIterator();
while (li.hasNext()) {
Product p = (Product) li.next();
/*
double newPrice = p.getPrice().doubleValue() * (100 + pct)/100;
p.setPrice(new Double(newPrice));
*/
pmd.increasePrice(p, pct);
}
}
}
springapp/war/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<!-- Controller for the initial "Hello" page -->
<bean id="springappController" class="web.SpringappController">
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<!-- Validator and Form Controller for the "Price Increase" page -->
<bean id="priceIncreaseValidator" class="bus.PriceIncreaseValidator"/>
<bean id="priceIncreaseForm" class="web.PriceIncreaseFormController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>priceIncrease</value></property>
<property name="commandClass"><value>bus.PriceIncrease</value></property>
<property name="validator"><ref bean="priceIncreaseValidator"/></property>
<property name="formView"><value>priceincrease</value></property>
<property name="successView"><value>hello.htm</value></property>
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
<property name="url">
<value>jdbc:hsqldb:/home/trisberg/workspace/springapp/db/test</value>
</property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>
<bean id="prodManDao" class="db.ProductManagerDaoJdbc">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="prodMan" class="bus.ProductManager">
<property name="productManagerDao">
<ref bean="prodManDao"/>
</property>
<!--
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
-->
</bean>
<!--
<bean id="product1" class="bus.Product">
<property name="description"><value>Lamp</value></property>
<property name="price"><value>5.75</value></property>
</bean>
<bean id="product2" class="bus.Product">
<property name="description"><value>Table</value></property>
<property name="price"><value>75.25</value></property>
</bean>
<bean id="product3" class="bus.Product">
<property name="description"><value>Chair</value></property>
<property name="price"><value>22.79</value></property>
</bean>
-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"><value>messages</value></property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
<prop key="/priceincrease.htm">priceIncreaseForm</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
springapp/src/tests/MockProductManagerDaoImpl.java
package tests;
import bus.Product;
import java.util.List;
import db.ProductManagerDao;
import bus.Product;
public class MockProductManagerDaoImpl implements ProductManagerDao {
private List products;
public void setProducts(List p) {
products = p;
}
public List getProductList() {
return products;
}
public void increasePrice(Product prod, int pct) {
double newPrice = prod.getPrice().doubleValue() * (100 + pct)/100;
prod.setPrice(new Double(newPrice));
}
}
springapp/src/tests/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="springappController" class="web.SpringappController">
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<bean id="prodManDao" class="tests.MockProductManagerDaoImpl">
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
</bean>
<bean id="prodMan" class="bus.ProductManager">
<property name="productManagerDao">
<ref bean="prodManDao"/>
</property>
<!--
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
-->
</bean>
<bean id="product1" class="bus.Product">
<property name="description"><value>Lamp</value></property>
<property name="price"><value>5.75</value></property>
</bean>
<bean id="product2" class="bus.Product">
<property name="description"><value>Table</value></property>
<property name="price"><value>75.25</value></property>
</bean>
<bean id="product3" class="bus.Product">
<property name="description"><value>Chair</value></property>
<property name="price"><value>22.79</value></property>
</bean>
</beans>
springapp/src/tests/TestProductManager .java
package tests;
import java.util.List;
import java.util.ArrayList;
import junit.framework.TestCase;
import db.ProductManagerDao;
import bus.ProductManager;
import bus.Product;
public class TestProductManager extends TestCase {
private ProductManager pm;
public void setUp() {
pm = new ProductManager();
Product p = new Product();
p.setDescription("Chair");
p.setPrice(new Double("20.50"));
ArrayList al = new ArrayList();
al.add(p);
p = new Product();
p.setDescription("Table");
p.setPrice(new Double("150.10"));
al.add(p);
/*
pm.setProducts(al);
*/
MockProductManagerDaoImpl pmdao = new MockProductManagerDaoImpl();
pmdao.setProducts(al);
pm.setProductManagerDao(pmdao);
pm.getProducts();
}
public void testGetProducs() {
List l = pm.getProducts();
Product p1 = (Product) l.get(0);
assertEquals("Chair", p1.getDescription());
Product p2 = (Product) l.get(1);
assertEquals("Table", p2.getDescription());
}
public void testIncreasePrice() {
pm.increasePrice(10);
List l = pm.getProducts();
Product p = (Product) l.get(0);
assertEquals(new Double("22.55"), p.getPrice());
p = (Product) l.get(1);
assertEquals(new Double("165.11"), p.getPrice());
}
}
相关推荐
根据给定的文件信息,我们可以总结出以下关于使用JBuilder开发Spring实例的关键知识点: ### 1. 下载Spring框架 - 首先,需要访问Spring官网(http://www.springframework.org/download.html)下载Spring框架包。 ...
《JBuilder程序设计实例》是针对Java开发工具JBuilder的一份详细的学习资源,它包含了丰富的编程源码,旨在帮助开发者深入理解JBuilder的使用方法和Java编程实践。JBuilder是Borland公司(现被Embarcadero ...
介绍如何在jbuilder中使用spring,通过理论讲解和web实例
本次我们将深入探讨Spring Framework 1.2.7 版本,并结合JBuilder这款强大的Java集成开发环境,阐述如何将两者完美融合,提升开发效率。 首先,Spring Framework 1.2.7 是Spring早期的一个稳定版本,它包含了对Java...
这份资料书可能按照上述章节顺序,逐步深入讲解JBuilder的使用方法,每个章节都包含实例演示和练习,帮助读者快速掌握JBuilder的使用技巧,从而提高Java开发效率。通过阅读01到08的PDF文件,你可以逐个掌握这些知识...
} } } 也是如我所想 抛出java.lang.IllegalAccessException异常,当时就怀疑Spring框架是否使用反射的一些特性,后来查了相关文档才知 道原因何在: 实际上java在反射创建一个类的实例时,默认会检测是否符合相关...
本实例将通过JBuilder 2006这一集成开发环境来展示如何实现Java分页功能。 首先,我们需要理解分页的基本原理。分页允许用户按页面浏览数据,而不是一次性加载所有数据,这可以减少内存占用,提升响应速度,并且对...
许多知名产品,如Oracle数据库管理系统、Borland的JBuilder集成开发环境(IDE)、Eclipse IDE等,都是基于Java开发的。Java的强大之处在于其跨平台能力,使得开发者能够轻松地创建可以在不同操作系统上运行的应用...
通过学习这个实例,开发者可以深入了解JBuilder 2005在B/S模式下开发网上购物系统的过程,掌握相关技术和最佳实践。这不仅有助于提升Java Web开发技能,也有助于理解和应用电子商务系统的架构设计。
2. **前沿技术动态**:书中涵盖了Java技术的最新发展动态,如Java SE 11及以上版本的新特性、Spring框架的更新、微服务架构、容器技术(如Docker)、云原生开发、函数式编程、响应式编程、大数据处理、机器学习集成...
2. 熟练掌握Struts2+Hibernate+Spring进行Java EE项目开发,深入理解Struts2核心思想 3. 熟练掌握Jbpm工作流,理解Jbpm流程实例、任务、执行、候选人、泳道等工作流概念 4. 熟练掌握Lucene(全文检索)、MyBatis、...
这个**JBuild2005电子教程**集合了关于该软件的详细知识,包括集成实例、基本操作以及用法说明,旨在帮助初学者快速掌握JBuilder2005的使用技巧。 首先,让我们深入了解一下JBuilder2005的核心特性: 1. **集成...
### Struts Web设计与开发大全知识点总结 ...通过以上内容,本书不仅提供了丰富的理论知识,还通过实例详细讲解了Struts框架的实际应用,非常适合初学者和有一定Java Web开发经验的人士参考学习。
EJB(Enterprise JavaBeans)是Java EE(Java Platform, Enterprise ...不过,随着Java EE演进到Jakarta EE,现代的开发趋势更倾向于轻量级框架,如Spring,但EJB依然是理解和学习企业级Java开发历史的重要部分。
简历中还展示了作者对开发工具的熟练掌握,包括“Jbuilder”和“Eclipse”,这两者都是流行的集成开发环境(IDE),为Java开发提供了丰富的功能和插件。 对于Web服务器和应用服务器,简历中提到了“Apache Tomcat”...
4. **IDE**: 选择一个Java集成开发环境,如JBuilder、Eclipse、IntelliJ IDEA或JCreator。这里推荐JCreator,轻量级且功能齐全。为了获得代码提示,需要在IDE中导入Struts和Hibernate相关的jar文件。 5. **前端开发...
- **开发工具**: 询问候选人使用过哪些开发工具,如Eclipse或JBuilder,并讨论它们之间的区别。 - **JDK版本**: 考察候选人对不同版本JDK的了解程度,以及它们之间的差异。 **6. 业务层** - **EJB**: 讨论EJB的...
- **非PC/Windows下的计算机应用**:探索在Linux、嵌入式系统等平台上的软件开发技术和应用实例。 - **企业参观体会**:通过参观企业的实际运作情况,了解企业的组织架构、工作流程以及企业文化等。 - **基于JSP的...
1. **页面编辑**:可以选择DreamWeaver或其他专业网页编辑器,也可以用简单的文本编辑器如Notepad++或IDE如Eclipse、NetBeans、JBuilder。 **Tomcat文件夹结构** 1. **webapps**:存放所有Web应用程序的目录。 2. *...
- **配置weblogic、weblogic与jbuilder集成**:指导如何在WebLogic服务器上部署EJB,并将其与开发工具集成。 - **使用EJB的目的、包含的元素和开发EJB的过程**:说明EJB的主要用途,包括远程接口、本地接口等组成...