当前位置: 首页 > news >正文

文山壮族苗族自治州网站建设_网站建设公司_一站式建站_seo优化

织梦大气金融类通用企业网站模板,漳州正规网站建设价格,沈阳网站制作平台,网站建设兼职合同MVC框架 一个实现 MVC 模式的应用包含模型、视图、控制器 3 个模块#xff1a; 模型#xff1a;封装了应用的数据和业务逻辑#xff0c;负责管理系统业务数据 视图#xff1a;负责应用的展示 控制器#xff1a;负责与用户进行交互#xff0c;接收用户输入、改变模型、调整…MVC框架 一个实现 MVC 模式的应用包含模型、视图、控制器 3 个模块 模型封装了应用的数据和业务逻辑负责管理系统业务数据 视图负责应用的展示 控制器负责与用户进行交互接收用户输入、改变模型、调整视图的显示 基于MVC架构模式的 Java Web 开发 采用了 Servlet JSP JavaBean 的技术实现 基于MVC架构模式的 Java Web 开发步骤 1. 定义一系列的 Bean 来表示数据 2. 使用一个 Dispatcher Servlet 和控制类来处理用户请求 3. 在 Servlet 中填充 Bean 4. 在 Servlet 中将 Bean 存储到 request、session、servletContext中 5. 将请求转发到 JSP 页面 6. 在 JSP 页面中从 Bean 中提取数据。 其中 Dispatcher servlet 必须完成如下功能 1. 根据 URI 调用相应的 action 2. 实例化正确的控制器类 3. 根据请求参数值来构造表单 bean 4. 调用控制器对象的相应方法 5. 转发到一个视图JSP页面 每个 HTTP 请求都发送给控制器请求中的 URI 标识出对应的 action。action 代表了应用可以执行的一个操作。一个提供了 Action 的 Java 对象称为 action 对象。 控制器会解析 URI 并调用相应的 action然后将模型对象放到视图可以访问的区域以便服务器端数据可以展示在浏览器上。最后控制器利用 RequestDispatcher 跳转到视图JSP页面在JSP页面使用 EL 以及定制标签显示数据。 注意调用 RequestDispatcher.forward 方法并不会停止执行剩余的代码。因此若 forward 方法不是最后一行代码则应显示的返回。 使用MVC模式的实例  目录结构如下 代码如下 DispatcherServlet.java package app16c.servlet;import java.io.IOException;import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import app16c.controller.InputProductController; import app16c.controller.SaveProductController;WebServlet(name DispatcherServlet, urlPatterns {/product_input.action, /product_save.action}) public class DispatcherServlet extends HttpServlet {private static final long serialVersionUID 1L;public DispatcherServlet() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {process(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String uri request.getRequestURI();int lastIndex uri.lastIndexOf(/);String action uri.substring(lastIndex 1);String dispatcherUrl null;if (action.equals(product_input.action)) {InputProductController controller new InputProductController();dispatcherUrl controller.handleRequest(request, response);} else if (action.equals(product_save.action)) {SaveProductController controller new SaveProductController();dispatcherUrl controller.handleRequest(request, response);}if (dispatcherUrl ! null) {RequestDispatcher rd request.getRequestDispatcher(dispatcherUrl);rd.forward(request, response);}} } Controller.java package app16c.controller;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public interface Controller {public abstract String handleRequest(HttpServletRequest request, HttpServletResponse response); } InputProductController.java package app16c.controller;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class InputProductController implements Controller {Overridepublic String handleRequest(HttpServletRequest request, HttpServletResponse response) {return /WEB-INF/jsp/ProductForm.jsp;} } SaveProductController.java package app16c.controller;import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import app16c.domain.Product; import app16c.form.ProductForm; import app16c.validator.ProductValidator;public class SaveProductController implements Controller {Overridepublic String handleRequest(HttpServletRequest request, HttpServletResponse response) {ProductForm productForm new ProductForm();productForm.setName(request.getParameter(name));productForm.setDescription(request.getParameter(description));productForm.setPrice(request.getParameter(price));ProductValidator productValidator new ProductValidator();ListString errors productValidator.validate(productForm);System.out.println(errors);if (errors.isEmpty()) {Product product new Product();product.setName(productForm.getName());product.setDescription(productForm.getDescription());product.setPrice(Float.parseFloat(productForm.getPrice()));// insert code to save product to the databaserequest.setAttribute(product, product);return /WEB-INF/jsp/ProductDetails.jsp;} else {request.setAttribute(errors, errors);request.setAttribute(form, productForm);return /WEB-INF/jsp/ProductForm.jsp;}} } ProductValidator.java package app16c.validator;import java.util.ArrayList; import java.util.List; import app16c.form.ProductForm;public class ProductValidator {public ListString validate(ProductForm productForm) {ListString errors new ArrayList();String name productForm.getName();if (name null || name.trim().isEmpty()) {errors.add(Product must have a name);}String price productForm.getPrice();if (price null || price.trim().isEmpty()) {errors.add(Product must have a price);}else {try {Float.parseFloat(price);} catch (NumberFormatException e) {errors.add(Invalid price value.);}}return errors;} } Product.java package app16c.domain;import java.io.Serializable;public class Product implements Serializable { // 一个JavaBean 实现该接口其实例可以安全的将数据保存到 HttpSession 中。private static final long serialVersionUID 1L;private String name;private String description;private float price;public String getName() {return name;}public void setName(String name) {this.name name;}public String getDescription() {return description;}public void setDescription(String description) {this.description description;}public float getPrice() {return price;}public void setPrice(float price) {this.price price;} } ProductForm.java package app16c.form;// 表单类与 HTML表单相映射是HTML表单在服务端的代表 public class ProductForm { // 表单类不需要实现 Serializable 接口因为表单对象很少保存在 HttpSession 中。private String name;private String description;private String price;public String getName() {return name;}public void setName(String name) {this.name name;}public String getDescription() {return description;}public void setDescription(String description) {this.description description;}public String getPrice() {return price;}public void setPrice(String price) {this.price price;} } ProductForm.jsp % page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8% % taglib urihttp://java.sun.com/jsp/jstl/core prefixc % !-- taglib指令用来引用标签库并设置标签库的前缀 --!DOCTYPE html html headmeta charsetUTF-8titleAdd Product Form/titlelink relstylesheet typetext/css hrefcss/main.css / !-- 外部样式表 -- /head bodydiv idglobalc:if test${requestScope.errors ! null } !-- EL表达式 --p iderrorsError(s)!ulc:forEach varerror items${requestScope.errors } !-- jstl 遍历 --li${error }/li/c:forEach/ul/p/c:ifform actionproduct_save.action methodpostfieldsetlegendAdd a product/legendplabel fornameProduct Name: /labelinput typetext idname namename tabindex1 //pplabel fordescriptionDescription: /labelinput typetext iddescription namedescription tabindex2 //pplabel forpricePrice: /labelinput typetext idprice nameprice tabindex3 //pp idbuttonsinput idreset typereset tabindex4 /input idsubmit typesubmit tabindex5 valueAdd Product //p/fieldset/form/div /body /html ProductDetails.jsp % page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8% !DOCTYPE html html headmeta charsetUTF-8titleSave Product/titlelink relstylesheet typetext/scc hrefcss/main.css / /head bodydiv idglobalh4The product has been saved./h4ph5Details/h5Product Name: ${product.name } br /Description: ${product.description } br /Price: $${product.price }/p/div /body /html 测试结果  转载于:https://www.cnblogs.com/0820LL/p/9949873.html
http://www.ihoyoo.com/news/23028.html

相关文章:

  • 网站后台登陆素材上海建网站费用优帮云
  • 室内设计网站资源wordpress版本
  • 成都网站建设策划网站图片模板源码
  • 个人做电影网站服务器放国外安全吗贵阳微网站建设公司
  • 中美网站建设网页设计在邯郸能干什么
  • 怎么防止网站被注册机软件开发报价明细有哪些
  • 南通单位网站建设济南市城市建设集团网站
  • 搜不到wordpress 网站ui设计兼职平台有哪些
  • 重庆网站建设公司咨询亿企帮免费发帖平台
  • 网站开发进度把握跨境电商个人可以做吗
  • 网站建设情况自查报告开发公司建酒店科目
  • 杂志在线设计网站做网站好的网站建设公司排名
  • seo外包优化网站大数据培训机构排名前十
  • 怎么建立淘宝客网站家具外贸网站
  • 懒人免费建站模板保定网站建设浩森宇特
  • 建材板材网站源码 asp影视广告公司宣传片
  • 专业做相册书的网站温州制作企业网站
  • 天水市网站建设网站排名优化培训哪家好
  • 承德建设网站wordpress 个性图标
  • 外贸推广网站公司手机报价网最新价格
  • 做个人博客的网站如何制作一个报名微信小程序
  • 上海企业网站黄页成都市建设工程施工安监站网站
  • 建设云网站旅游网站模板文章
  • 网站建设西安阿里云 wordpress建站
  • 广州网站制作在线二级学院网站制度建设
  • 支付通道网站怎么做现在那个网站做宣传有效果
  • 南通企业网站排名优化网站开发摊销
  • 介绍西安网页设计深圳优化seo排名
  • 网站要求手机网站 pc网站模板
  • 温州英文网站建设php 网站伪静态