配置完成Spring且创建maven项目之后开始我的第一个SpringMVC应用:
应用介绍:简单的商品信息输入和显示
程序结构:InputProductController.java(继承controller类),SaveProductController.java(继承controller类),Product.java(商品表单类),ProductForm.java,ProductForm.jsp(输入商品信息视图),ProductDetails.jsp(显示商品信息视图),web.xml,firstSpringMVC.xml,firstSpringMVC-config.xml。
项目目录结构:
代码部分:
InputProductController.java
package controller; public class InputProductController implements Controller { private static final Log logger = LogFactory.getLog(InputProductController.class); @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { logger.info("InputProductController called"); return new ModelAndView("ProductForm"); } }
SaveProductController.java
package controller; public class SaveProductController implements Controller { private static final Log logger = LogFactory.getLog(SaveProductController.class); @Override public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { logger.info("SaveProductController called"); 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){} return new ModelAndView("ProductDetails","product", product); } }
Product.java
package domain; public class Product implements Serializable{ private static final long serialVersionUID = 748392348L; 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.java
package form; 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; } }
ProductForm.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>在此处插入标题</title> </head> <body> <form action='/firstSpringMVC/save-product' method='post'> <fieldset> <legend>添加商品</legend> <label for='name'>商品名称</label> <input type='text' id='name' name='name' tabindex='1'><br> <label for='description'>商品明细</label> <input type='text' id='description' name='description' tabindex='2'><br> <label for='price'>商品价格</label> <input type='text' id='price' name='price' tabindex='3'><br> <input type='reset' tabindex='4'> <input type='submit' tabindex='5' value='提交'> </fieldset> </form> </body> </html>
ProductDetails.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>在此处插入标题</title> </head> <body> <h4>商品成功保存</h4> <h5>商品</h5> 商品名称:${product.name }<br> 商品明细:${product.description }<br> 商品价格:${product.price } </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>firstSpringMVC</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>firstSpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/firstSpringMVC-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>firstSpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
firstSpringMVC-servlet.xml
写拥有视图解释器Spring配置文件之前使用的Spring配置文件,记录Controller类路径和url
<?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="/input-product" class="controller.InputProductController"></bean> <bean name="/save-product" class="controller.SaveProductController"></bean> </beans>
firstSpringMVC-config.xml
这是拥有视图解释器Spring配置文件,这样可以缩短视图路径,仅需写“ProductForm”就行不必写成“/WEB-INF/jsp/ProductForm.jsp”视图解释器会自动增加前缀和后缀。之后可以替代firstSpringMVC-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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="/input-product" class="controller.InputProductController"></bean> <bean name="/save-product" class="controller.SaveProductController"></bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
问题一:为什么需要ProductForm类?
·书上原话(实际上,表单对象会传递ServletRequest给其他组件,类似Validator而ServletRequest时一个Servlet层的对象,不应当暴露给应用的其他层)这个是当时没有理解的,其次还有另一个原因是当数据效验失败时,表单对象将用于保存和展示用户在原始表单上的输入。
问题二:当时第一次写这个程序的时候爬的坑
当写完程序后运行的时候先访问InputProductController.java类及http://localhost:8080/firstSpringMVC/input-product之后会运行完InputProductController.java后会进入ProductForm.jsp视图呈现这种界面
不过当时我的程序出现错误提示找不到input-product,之后发现.xml文件部署的位置不对并且查看我的项目在服务器上的位置(之前.xml文件部署在webapp文件根目录上)所以运行时出错。
问题三:继承Controller之后返回类型为ModelAndView的handelRequest函数的用法
此问题通过以下博主得到理解:
原文链接:https://blog.csdn.net/tongxinxiao/article/details/39502519
业务处理器调用模型层处理完用户请求后,把结果数据存储在该类的model属性中,把要返回的视图信息存储在该类的view属性中,然后让该ModelAndView返回该Spring MVC框架。框架通过调用配置文件中定义的视图解析器,对该对象进行解析,最后把结果数据显示在指定的页面上。
通过这次程序实验简单了解SPringMVC!