我的Spring MVC第一个应用 (最终版)
项目结构图:

代码如下:
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,包含视图,且没有模型,所有直接转发
*/
@Override
public ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception {
logger.info("进入handleRequest成功");
return new ModelAndView("ProductForm");
}
}
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); @Override
public 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("ProductDetails", "product",product);
}
}
springmvc-config.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/beans
http://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"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/springmvc-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springmvc</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第一个应用 (最终版)的更多相关文章
- Spring MVC第一课:用IDEA构建一个基于Spring MVC, Hibernate, My SQL的Maven项目
作为一个Spring MVC新手最基本的功夫就是学会如何使用开发工具创建一个完整的Spring MVC项目,本文站在一个新手的角度讲述如何一步一步创建一个基于Spring MVC, Hibernate ...
- 基于注解的Spring MVC的简单入门——简略版
网上关于此教程各种版本,太多太多了,因为我之前没搭过框架,最近带着两个实习生,为了帮他们搭框架,我只好...惭愧啊...基本原理的话各位自己了解下,表示我自己从来没研究过Spring的源码,所以工作了 ...
- Eclipse下创建Spring MVC web程序--非maven版
首先, 安装eclipse和tomcat, 这里我下载的是tomcat9.0版本64位免安装的:地址https://tomcat.apache.org/download-90.cgi 免安装的如何启动 ...
- spring mvc 第一天【注解实现springmvc的基本配置】
创建pojo,添加标识类的注解@Controller,亦可添加该Handler的命名空间:设置类的@RequestMapping(value="/hr") 该类中的方法(Handl ...
- {新人笔记 勿看} spring mvc第一步
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 我的Spring MVC第一个应用
Product package com.mstf.bean; import java.io.Serializable; /** * Product类,封装了一些信息,包含三个属性 * @author ...
- Spring MVC介绍和第一个例子
1.Spring mvc概述 spring mvc是spring提供给web应用框架设计,实际上MVC框架是一个设计理念.它不仅存在java世界中而且广泛在于各类语言和开发中,包括web的前端应用.对 ...
- Spring MVC实践
MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...
- Spring MVC教程——检视阅读
Spring MVC教程--检视阅读 参考 Spring MVC教程--一点--蓝本 Spring MVC教程--c语言中午网--3.0版本太老了 Spring MVC教程--易百--4.0版本不是通 ...
随机推荐
- (转载) 使用DrawerLayout和NavigationView从右侧出现
使用DrawerLayout和NavigationView从右侧出现 2016-07-21 17:53 957人阅读 评论(0) 收藏 举报 分类: android(9) 版权声明:本文为博主原创 ...
- JS高级之简单类的定义和继承
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- FCC编程题之中级算法篇(上)
介绍 FCC: 全称为freeCodeCamp,是一个非盈利性的.面向全世界的编程练习网站.这次的算法题来源于FCC的中级算法题. FCC中级算法篇共分为(上).(中).(下)三篇.每篇各介绍7道算法 ...
- js效果之导航中英文转换
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 快速沃尔什变换(FWT)笔记
开头Orz hy,Orz yrx 部分转载自hy的博客 快速沃尔什变换,可以快速计算两个多项式的位运算卷积(即and,or和xor) 问题模型如下: 给出两个多项式$A(x)$,$B(x)$,求$C( ...
- tensorflow学习笔记(一)安装
1.tensorflow介绍 中文社区地址 http://www.tensorfly.cn/ TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库. ...
- CSS常用样式--font
CSS font 属性 参考:W3school- CSS font 所有浏览器都支持 font 属性,可在一个声明中设置所有字体属性,各属性需按顺序,语法如下: selector{ font:styl ...
- CF1000G Two-Paths (树形DP)
题目大意:给你一棵树,点有点权$a_{i}$,边有边权$w_{e}$,定义一种路径称为$2-path$,每条边最多经过2次且该路径的权值为$\sum _{x} a_{x}\;-\;\sum_{e}w_ ...
- pip 出错
pip 升级到10以上出错 ImportError: cannot import name 'main' 解决方法一: 降低pip的版本号 python -m pip install pip==9.0 ...
- echars 在vue v-if 切换会 显示不出来或者显示出来但是不是百分百显示
我也是百度看别人写的原因,然后自己总结,以后用到的时候来复制就可以将 v-if换成 v-show 第二不是百分百显示 可以用 this.$nextTick(function() { this.in ...