




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 建立工程首先在eclipses上建立一个web工程,点击File-new-Others,选择Web Project,如下图所示:点击next,在Project Name中填入工程名称,在J2EE Specificatio Level选项中选择J2EE1.4,点击Finish就建立了一个Structs工程。2 引入Spring需要的jar包将spring.jar,commons-logging-1.0.4.jar,standard.jar,log4j-1.2.9.jar,jstl.jar包拷贝到工程所在目录WebRoot/WEB-INF/lib下。3. 发JSP页面主要有两个JSP页面,分别为hello.jsp和priceincrease.jsp。Include.jsp:引入了jstl的两个标签Index.jsp 启动后便跳转到hello.htm页面 Example - Spring Application This is my test. Hello.jsp:headingGreeting Products$a href=Increase PricesPriceincrease.jsp:Increase (%):input type=text name=percentage value=Please fix all errors!a href=Home4. 开发业务逻辑程序1)com.zznode.dp.springapp.bus包Poduct.java : Product的JavaBeanpackage com.zznode.dp.springapp.bus;import java.io.Serializable;public class Product implements Serializable private String description; private double price; private int id;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; ProductManager.java 类似EJB,提供了两个对Product的操作:getProducts()和increasePrice(),分别来获得所有产品列表和增加产品价格。package com.zznode.dp.springapp.bus;import java.io.Serializable;import java.util.List;import java.util.ListIterator;import com.zznode.dp.springapp.db.ProductManagerDao;public class ProductManager implements Serializable private List products; private ProductManagerDao pmd;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(); pmd.increasePrice(p, pct); PriceIncrease.java package com.zznode.dp.springapp.bus;import mons.logging.Log;import mons.logging.LogFactory;public class PriceIncrease /* Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass(); private int percentage; public void setPercentage(int i) percentage = i; (Percentage set to + i); public int getPercentage() return percentage; PriceIncreaseValidator.java 对增加的价格进行验证的类,验证输入的增加价格是否超过50%package com.zznode.dp.springapp.bus;import java.io.Serializable;import org.springframework.validation.Validator;import org.springframework.validation.Errors;import mons.logging.Log;import mons.logging.LogFactory;public class PriceIncreaseValidator implements Validator private int DEFAULT_MIN_PERCENTAGE = 0; private int DEFAULT_MAX_PERCENTAGE = 50; private int minPercentage = DEFAULT_MIN_PERCENTAGE; private int maxPercentage = DEFAULT_MAX_PERCENTAGE; /* Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass(); public boolean supports(Class clazz) return clazz.equals(PriceIncrease.class); public void validate(Object obj, Errors errors) PriceIncrease pi = (PriceIncrease) obj; if (pi = null) errors.rejectValue(percentage, error.not-specified, null, Value required.); else (Validating with + pi + : + pi.getPercentage(); if (pi.getPercentage() maxPercentage) errors.rejectValue(percentage, error.too-high,new Object new Integer(maxPercentage), Value too high.); if (pi.getPercentage() = minPercentage) errors.rejectValue(percentage, error.too-low,new Object new Integer(minPercentage), Value too low.); public void setMinPercentage(int i) minPercentage = i; public int getMinPercentage() return minPercentage; public void setMaxPercentage(int i) maxPercentage = i; public int getMaxPercentage() return maxPercentage; 2)com.zznod.dp.springapp.webPriceIncreaseFormController.java PriceIncrease.jsp的窗口控制类,当用户输入增加价格后点击提交按钮后,将调用本类的onSubmit()函数,然后调用ProductManager类提供的接口对价格进行计算和入库。package com.zznode.dp.springapp.web;import org.springframework.web.servlet.mvc.SimpleFormController;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.view.RedirectView;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import mons.logging.Log;import mons.logging.LogFactory;import com.zznode.dp.springapp.bus.ProductManager;import com.zznode.dp.springapp.bus.PriceIncrease;public class PriceIncreaseFormController extends SimpleFormController /* Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass(); private ProductManager prodMan; public ModelAndView onSubmit(Object command) throws ServletException int increase = (PriceIncrease) command).getPercentage(); prodMan.increasePrice(increase); (Increasing prices by + increase + %.); (returning from PriceIncreaseForm view to + getSuccessView(); return new ModelAndView(new RedirectView(getSuccessView(); protected Object formBackingObject(HttpServletRequest request) throws ServletException PriceIncrease priceIncrease = new PriceIncrease(); priceIncrease.setPercentage(20); return priceIncrease; public void setProductManager(ProductManager pm) prodMan = pm; public ProductManager getProductManager() return prodMan; SpringappController.java 主页面的控制类,调用handleRequest()函数对请求进行处理,在本例中,在页面显示当前时间,并显示所有产品的描述和价格信息package com.zznode.dp.springapp.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 mons.logging.Log;import mons.logging.LogFactory;import java.util.Map;import java.util.HashMap;import com.zznode.dp.springapp.bus.Product;import com.zznode.dp.springapp.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 (SpringappController - returning hello view);String now = (new java.util.Date().toString(); (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 setProductMana
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 消防工程补充协议书
- 工业炉燃料系统装配工服务质量抽查考核试卷及答案
- 铁氧体材料烧成工服务响应速度考核试卷及答案
- 线上签协议书注意什么
- 2025版权委托代理服务合同
- 脚轮制作工安全警示标识认知考核试卷及答案
- 山东省济宁兖州区七校联考2026届八年级数学第一学期期末预测试题含解析
- 江苏省南京东山外国语学校2026届七年级数学第一学期期末综合测试试题含解析
- 2025年国有土地使用权转让合同(现状补办类)模板范文
- 山东东营市2026届数学八年级第一学期期末学业质量监测试题含解析
- 合肥市社会化工会工作者招聘考试真题2024
- 2025年安全员b证考试安徽省题库及答案解析
- 首台套申报培训课件
- GB/T 14193.1-2025液化气体气瓶充装规定第1部分:工业气瓶
- 保安安检培训课件
- 凯里市大风洞夸山重晶石矿场环评报告
- 2021基层2型糖尿病胰岛素应用专家共识(全文)
- 乳腺增生病讲座
- DG1022型双通道函数任意波形发生器
- 安全监理现场巡视检查记录表
- GB/T 9113.2-2000凹凸面整体钢制管法兰
评论
0/150
提交评论