Product

package com.mstf.bean;

import java.io.Serializable;
/**
* Product类,封装了一些信息,包含三个属性
* @author wangzheng
*
*/
public class Product implements Serializable { 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

package com.mstf.bean.form;
/**
* ProductForm是表单类
* 作用:当数据校验失败时,用于保存和展示用户在原始表单的输入
* @author wangzheng
*
*/
public class ProductForm {
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;
} }

  Controller

package com.mstf.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public interface Controller { String handleRequest(HttpServletRequest req,HttpServletResponse resp);
}

  InputProductController

package com.mstf.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class InputProductController implements Controller { @Override
public String handleRequest(HttpServletRequest req,HttpServletResponse resp) {
return "/WEB-INF/jsp/ProductForm.jsp";
}
}

  SaveProductController

package com.mstf.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.mstf.bean.Product;
import com.mstf.bean.form.ProductForm; public class SaveProductController implements Controller { @Override
public String handleRequest(HttpServletRequest req,HttpServletResponse resp) {
// 构建一个ProductForm表单对象
ProductForm productForm = new ProductForm();
// 写入表单对象
productForm.setName(req.getParameter("name"));
productForm.setDescription(req.getParameter("description"));
productForm.setPrice(req.getParameter("price")); // 创建模型
Product product = new Product();
product.setName(productForm.getName());
product.setDescription(productForm.getDescription());
try {
product.setPrice(Float.parseFloat(productForm.getPrice()));
} catch (NumberFormatException e) {
e.printStackTrace();
}
// 添加
req.setAttribute("product", product);
return "/WEB-INF/jsp/ProductDetails.jsp";
}
}

  DispatcherServlet

package com.mstf.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.mstf.controller.InputProductController;
import com.mstf.controller.SaveProductController; public class DispatcherServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
process(req, resp);
} @Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
process(req, resp);
}
/**
* 这个方法用来处理所有输入请求
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
private void process(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException {
req.setCharacterEncoding("UTF-8"); //转码
resp.setCharacterEncoding("UTF-8");
String uri=req.getRequestURI(); // 获取请求URI
int lastIndex=uri.lastIndexOf("/");
String action=uri.substring(lastIndex+1); // 获取action名称
String dispatchUrl=null;
// 执行方法
if(action.equals("product_input.action")) {
InputProductController controller=new InputProductController();
dispatchUrl=controller.handleRequest(req, resp);
} else if (action.equals("product_save.action")) {
SaveProductController controller=new SaveProductController();
dispatchUrl=controller.handleRequest(req, resp);
}
if(dispatchUrl!=null) {
RequestDispatcher rd=req.getRequestDispatcher(dispatchUrl);
rd.forward(req, resp);
}
}
}

  

Spring MVC模式示例(采用解耦控制器)的更多相关文章

  1. Spring MVC模式示例(采用解耦控制器+校验器)

    Product package com.mstf.bean; import java.io.Serializable; /** * Product类,封装了一些信息,包含三个属性 * @author ...

  2. Spring MVC模式示例(未采用解耦控制器)

    Product package com.mstf.bean; import java.io.Serializable; /** * Product类,封装了一些信息,包含三个属性 * @author ...

  3. Spring MVC 入门示例讲解

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  4. Spring MVC 完整示例

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  5. Spring MVC 入门示例讲解 - howtodoinjava

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  6. Spring mvc 模式小结

    http://www.taobaotesting.com/blogs/2375 1.spring mvc简介 Spring MVC框架是一个MVC框架,通过实现Model-View-Controlle ...

  7. Spring MVC体系结构和处理请求控制器

    Spring MVC体系结构和处理请求控制器 一:MVC设计模式: (1.)数据访问接口:DAO层 (2.)处理业务逻辑层:Service层 (3.)数据实体:POJO (4.)负责前段请求接受并处理 ...

  8. Spring MVC 项目示例

    Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架.Spring的web框架围绕DispatcherServlet设计, 作用是将请求分发到不同 ...

  9. spring mvc: 可参数化的视图控制器(在配置中指定jsp文件)MultiActionController/SimpleUrlHandlerMapping/ParameterizableViewController

    spring mvc: 可参数化的视图控制器(在配置中指定jsp文件)MultiActionController/SimpleUrlHandlerMapping/ParameterizableView ...

随机推荐

  1. 网线直连Window和Ubuntu

      找根网线直接连接两台电脑. 然后设置对应的网络到相同的网关和Mask下面.  检测能否ping通就可以直连了.   Ubuntu设置网络后可在IP设置界面里设置Route, 选择直连网线仅用于连接 ...

  2. 15.boost最小生成树 prim_minimum_spanning_tree

    #include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...

  3. BootStrap学习(三)——重写首页之导航栏和轮播图

    1.按钮 1)帮助文档:http://v3.bootcss.com/css/#buttons 2).btn-lg..btn-sm..btn-xs可以设置按钮的不同尺寸 3).active类设置按钮的激 ...

  4. hiho 171周 - 水题,并查集

    题目链接 题目描述: 输入4 alice 2 alice@hihocoder.com alice@gmail.com bob 1 bob@qq.com alicebest 2 alice@gmail. ...

  5. php如何实现文件下载

    php如何实现文件下载 1. 设置超链接的href属性 <ahref="文件地址"></a> 如果浏览器不能解析该文件,浏览器会自动下载.而如果文件是图片或 ...

  6. javascript中的正则示例

    // 方式一var obj_re = new RegExp("\d+","gi"); //g 全局,i 不区分大小写obj_re.test("fasf ...

  7. swift语言点评四-Closure

    总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...

  8. Web前端为什么这么火爆?

    Web前端为什么这么火爆? 互联网发展到今天,全球已有28.9亿互联网用户,中国有355万网站,6.5亿网民,13亿手机用户,5亿微信用户,当步入互联网+时代后,互联网已经越来越复杂,纷繁复杂的互联网 ...

  9. Ubuntu PPA 使用指南

    作者: Abhishek Prakash 译者: LCTT jlztan | 2019-01-19 11:02 一篇涵盖了在 Ubuntu 和其他 Linux 发行版中使用 PPA 的几乎所有问题的深 ...

  10. idea运行提示Error:java:无效的源发行版:1.9

    如果你是jdk1.8 改到8即可,如图: