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;
} }

  ControllerServlet

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.bean.Product;
import com.mstf.bean.form.ProductForm; public class ControllerServlet 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名称
// 执行方法
if(action.equals("product_input.action")) {
// 没有找到,不进行任何操作
} else if (action.equals("product_save.action")) {
// 根据请求参数构建一个ProductForm表单对象
ProductForm productForm=new ProductForm();
// 填充表单对象数据
productForm.setName(req.getParameter("name"));
productForm.setDescription(req.getParameter("description"));
productForm.setPrice(req.getParameter("price")); // 创建Product模型(领域对象),并通过表单对象设置相应属性
Product product=new Product();
product.setName(productForm.getName());
product.setDescription(productForm.getDescription());
try {
product.setPrice(Float.parseFloat(productForm.getPrice()));
} catch (Exception e) {
e.printStackTrace();
}
// 将Product模型放入HttpServletRequest对象中,以便对应的试图可以访问
req.setAttribute("product", product);
} // 转发到这个视图
String dispatchUrl=null;
if(action.equals("product_input.action")) {
dispatchUrl="WEB-INF/jsp/ProductForm.jsp";
} else if(action.equals("product_save.action")) {
dispatchUrl="WEB-INF/jsp/ProductDetails.jsp";
}
if(dispatchUrl!=null) {
RequestDispatcher rd=req.getRequestDispatcher(dispatchUrl);
rd.forward(req, resp);
}
}
}

  web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.mstf.servlet.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping> </web-app>

  ProductDetails.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>详情</title>
<style type="text/css">@IMPORT url("css/main.css");</style>
</head>
<body>
<div id="global">
<h4>产品已保存</h4>
<p>
<h5>详细列表:</h5>
名称: ${product.name}<br>
简介: ${product.description}<br>
价格: ¥${product.price}
</p>
</div>
</body>
</html>

  ProductForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加</title>
<style type="text/css">@IMPORT url("css/main.css");</style>
</head>
<body>
<div id="global">
<form action="product_save.action" method="post">
<fieldset>
<legend>添加:</legend>
<p>
<label for="name">名称: </label>
<input type="text" id="name" name="name" tabindex="1">
</p>
<p>
<label for="description">简介: </label>
<input type="text" id="description" name="description" tabindex="2">
</p>
<p>
<label for="price">价格: </label>
<input type="text" id="price" name="price" tabindex="3">
</p>
<p id="buttons">
<input id="reset" type="reset" tabindex="4">
<input id="submit" type="submit" tabindex="5" value="添加">
</p>
</fieldset>
</form>
</div>
</body>
</html>

  

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 模式小结

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

  4. Spring MVC 项目示例

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

  5. Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解

    一.控制器定义 控制器提供访问应用程序的行为,通常通过服务接口定义或注解定义两种方法实现. 控制器解析用户的请求并将其转换为一个模型.在Spring MVC中一个控制器可以包含多个Action(动作. ...

  6. 1.【转】spring MVC入门示例(hello world demo)

    1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...

  7. Spring MVC 入门示例讲解

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

  8. Spring MVC 完整示例

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

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

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

随机推荐

  1. HDU 5616 Jam's balance 背包DP

    Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell ...

  2. zzulioj--1705--小明在工作(模拟水题)

    1705: 小明在工作 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 179  Solved: 59 SubmitStatusWeb Board De ...

  3. 创建ios界面的三步骤

    1.加载数据 (包括懒加载和字典转模型等) 2.搭建界面 (常见的有九宫格算法和for循环的嵌套等) 3.实现用户交互 (通常用按钮实现)

  4. 三个获取浏览器URL中参数值的方法

    这三个是一般的获取浏览器传的参数值的方法,之前有用unescape()解码的方法,但是遇到汉字会产生乱码,所以用decodeURI(); 方法一: function getQueryString(na ...

  5. winforms控件

     我们在开发窗体应用时,控件是必不可少的今天我们就来认识一下控件 在认识控件之前还要先来认识一下窗体具体如下: 认识窗体和控件 窗体                                   ...

  6. c#邮件发送服务

    邮件发送服务 项目中会遇到定时给某人发送邮件的功能要求,这里是京东的一段代码,当然也是我同事找的,我记录学习一下,以免忘记. 这是解决方案 这里主要是工具:日志工具,链接数据库工具,发送邮件工具 这里 ...

  7. (转载) Android studio如何生成aar包

    Android studio如何生成aar包 标签: Android studio如何生成aaAndroid studio aarAndroid 如何生成aar包 2016-12-21 14:42 1 ...

  8. Servlet学习(七)——cookie

    一.会话技术简介 1.存储客户端的状态 例如网站的购物系统,用户将购买的商品信息存储到哪里?因为Http协议是无状态的,也就是说每个客户访问服务器端资源时,服务器并不知道该客户端是谁,所以需要会话技术 ...

  9. JavaScript学习——使用JS完成省市二级联动

    1.我们希望在注册页面中添加一个字段(籍贯),当用户选择一个具体的省份,在后面的下拉列表中动态加载该省份下所有的城市.显示的效果如下: 2.步骤分析: 第一步:确定事件(onchange)并为其绑定一 ...

  10. SQL中的union

    在SQL中,如果我们查询一个班级的考试成绩数据,再统计考试成绩的总和,我们使用以下两条语句: select StudentName,Grade from Student select '总成绩',SU ...