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. HDUOj 看病要排队 优先队列的使用 题目1873

    STL优先队列的具体描写叙述 http://blog.csdn.net/yueloveme/article/details/47106639 题目地址:http://acm.hdu.edu.cn/s ...

  2. class.forName的官方使用方法说明

    原文地址:http://yanwushu.sinaapp.com/class_forname/ 使用jdbc方式链接数据库时会常常看到这句代码:Class.forName(String classNa ...

  3. iOS (封装)一句话调用系统的alertView和alertController

    前言: 本文仅作参考存留,请用新版封装:iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用 UIAlertController是iOS8.0之后出来的新方法,其将 ...

  4. m_Orchestrate learning system---八、下拉列表(select标签)如何实现链接功能

    m_Orchestrate learning system---八.下拉列表(select标签)如何实现链接功能 一.总结 一句话总结:option的值就是链接地址,选择事件为指向选中的option的 ...

  5. zzuoj--10401--物资调度(dfs)

    A.物资调度 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 93  Solved: 52 [Submit][Status][Web Board] De ...

  6. hiho1509 异或排序

    题目链接 题目大意: 给定一个长度为 n 的非负整数序列 a[1..n] 你需要求有多少个非负整数 S 满足以下两个条件: (1).0 ≤ S < 260 (2).对于所有 1 ≤ i < ...

  7. 洛谷 P2633 Count on a tree 主席树

    在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$  ,建立 $i$  到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...

  8. GET,POST请求

    get请求 post请求

  9. (WC2018模拟十二)【FJOI2016集训Day7T1】挑选子序列

    题解: 这题太神仙了,先咕着,放个令人****的官方题解(精确覆盖问题是NP完全问题啊摔) Dancing Link X学习资料:QAQ TAT qwq Orz Donald Knuth大神... p ...

  10. HDU 6051 If the starlight never fade(原根+推式子)

    题目大意: 设\(f(i)\)为使\((x+y)^i \equiv x^i (mod\ p)\)成立的(x,y)的对数.其中\(1 \leq x \leq p-1 , 1\leq y\leq m\), ...