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

  InputProductController

package com.mstf.controller;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;public class InputProductController implements Controller {private static final Log logger=LogFactory.getLog(InputProductController.class);/*** 返回一个ModelAndView,包含视图,且没有模型,所有直接转发*/@Overridepublic ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception {logger.info("进入handleRequest成功");return new ModelAndView("/WEB-INF/jsp/ProductForm.jsp");}
}

  SaveProductController

package com.mstf.controller;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;import com.mstf.bean.Product;
import com.mstf.bean.form.ProductForm;public class SaveProductController implements Controller {private static final Log logger=LogFactory.getLog(InputProductController.class);@Overridepublic ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception {req.setCharacterEncoding("UTF-8"); //转码resp.setCharacterEncoding("UTF-8");logger.info("进入SaveProductController成功");// 构建一个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 (Exception e) {e.printStackTrace();}return new ModelAndView("/WEB-INF/jsp/ProductDetails.jsp", "product",product);}
}

  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>

  springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean name="/product_input.action" class="com.mstf.controller.InputProductController"/><bean name="/product_save.action" class="com.mstf.controller.SaveProductController"/></beans>

  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>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping>  </web-app>

  

转载于:https://www.cnblogs.com/ceet/p/6594515.html

我的Spring MVC第一个应用相关推荐

  1. 写出我的第一个框架:迷你版Spring MVC

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:张丰哲 https://www.jianshu.com/p/ ...

  2. 第一个使用Spring Tool Suite(STS)和Maven建立的Spring mvc 项目

    一.目标 在这篇文章中.我将要向您展示怎样使用Spring Frameworks 和 Maven build创建您的第一个J2ee 应用程序. 二.信息 Maven是一个java项目的构建工具(或者自 ...

  3. 【手写系列】写出我的第一个框架:迷你版Spring MVC

    你没有看错标题,今天,我将实现我人生中第一个框架,^_^ 前期准备 我这里要写的是一个迷你版的Spring MVC,我将在一个干净的web工程开始开发,不引入Spring,完全通过JDK来实现. 我们 ...

  4. 你的第一杯Web 2.0 —— 快速浏览jQuery、Spring MVC和XStream/Jettison

    不再有页面刷新:使用jQuery 相关厂商内容 免费迷你书下载:Flex入门指南--PHP开发者 Flash Builder 4 Beta和FlexUnit下的测试驱动开发 下载Flex 4 SDK, ...

  5. Java之Spring mvc详解(非原创)

    文章大纲 一.Spring mvc介绍 二.Spring mvc代码实战 三.项目源码下载 四.参考文章 一.Spring mvc介绍 1. 什么是springmvc   springmvc是spri ...

  6. Spring MVC 五大组件

    欢迎关注方志朋的博客,回复"666"获面试宝典 是一个MVC架构,用来简化基于MVC架构的Web应用开发.SpringMVC最重要的就是五大组件 1. DispatcherServ ...

  7. 爸爸又给Spring MVC生了个弟弟叫Spring WebFlux

    作者:李新杰 来自:编程新说 情景引入 很早之前,Java就火起来了,是因为它善于开发和处理网络方面的应用. Java有一个爱好,就是喜欢制定规范标准,但自己又不善于去实现. 反倒是一些服务提供商使用 ...

  8. [Spring mvc 深度解析(三)] 创建Spring MVC之器

    第9章 创建Spring MVC之器 ​ 本章将分析Spring MVC自身的创建过程.首先分析Spring MVC的整体结构,然后具体分析每一层的创建过程. 1 整体结构介绍 Spring MVC中 ...

  9. Spring MVC 原理探秘 - 一个请求的旅行过程

    1.简介 在前面的文章中,我较为详细的分析了 Spring IOC 和 AOP 部分的源码,并写成了文章.为了让我的 Spring 源码分析系列文章更为丰富一些,所以从本篇文章开始,我将来向大家介绍一 ...

最新文章

  1. PHP性能调优,PHP慢日志---PHP脚本执行效率性能检测之WebGrind的使用
  2. [How TO]-外网访问自己的HTTP服务器
  3. SQL   PL/SQL   SQL*PLUS三者的区别
  4. MyBatis 集成到Spring 的原理是什么?
  5. 牛客 - Connie(AC自动机+dp/KMP+dp)
  6. idea退出首界面_如何取消Idea开始界面打开默认项目配置
  7. SilverLight 初探一
  8. Uber无人车为何危险:长期忽视模拟器,只在意路测 | 内部声音
  9. LeetCode 118. Pascal’s Triangle
  10. linux之mktemp命令
  11. [命令模式]在游戏开发中的应用
  12. Spring项目启动后报连接MYSQL错误两则
  13. 感知机原理及代码实现小结
  14. 电脑蓝屏日志存在哪里_Win10蓝屏日志在哪里 蓝屏查看工具BlueScreenView使用教程...
  15. python 获取foobar2000官网全部插件
  16. java调用chrome内核_selenium中如何测试360等基于chrome内核的浏览器
  17. ValueError: Duplicate plugins for name projector
  18. 如何将证件照片打印在A4纸上
  19. OSPF 协议中的一个普通区域通过ASBR 注入192.168.0.0/24~192.168.3.0/24 共4 条路由,在ABR 中配置聚合为一条聚合路由192.168.0.0/22,此时ABR 会
  20. 【hdu2298】【三分】Toxophily

热门文章

  1. 利用MATLAB对数据进行切片并绘制图表
  2. matlab闭式网络潮流计算,闭式网络潮流计算.ppt
  3. vue当前页引入js_「vue基础」新手入门导航(一)
  4. Python中字符串的连接
  5. Linux切换slave,热备服务器中,切换master中切换SQL
  6. Pytest之收集用例及命令行参数
  7. 测试面试题集-Python花式打印九九乘法口诀表
  8. SAP License:SAP顾问该不该参与数据搜集
  9. SAP License:”事后借记”与第三方外币支付处理
  10. SQL50道练习题(1-15)