1 DispatcherServlet
调度员servlet
org.springframework.web.servlet.DispatcherServlet
部署描述符(web.xml):
<?xml version ="1.0" encoding="UTF-8" ?>
<web-app version="3.0"
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">
<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>
它会寻找应用程序的配置文件:servletName-servlet.xml
声明两个控制器类 Controller,对应不同的逻辑
<?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.controller.InputProductController"/>
<bean name="/product_save.action"
class="com.controller.SaveProductController"/>
</beans>
Product 类:
package com.domain;
import java.io.Serializable;
public class Product implements Serializable {
private static final long serivalVersionUID = 78963434L;
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.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;
}
}
视图解析器:
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.controller.InputProductController"/>
<bean name="/product_save.action" class="com.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>
输入产品信息:
package com.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);
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)throws Exception{
logger.info("InputProductController Called");
return new ModelAndView("ProductForm");
}
}
保存产品信息:
package com.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.domain.Product;
import com.form.ProductForm;
public class SaveProductController implements Controller{
private static final Log logger = LogFactory.getLog(SaveProductController.class);
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)throws Exception{
logger.info("SaveProductController Called");
ProductForm productFrom = new ProductForm();
//populate action properties
productFrom.setName(request.getParameter("name"));
productFrom.setDescription(request.getParameter("description"));
productFrom.setPrice(request.getParameter("price"));
//create model
Product product = new Product();
product.setName(productFrom.getName());
product.setDescription(productFrom.getDescription());
try{
product.setPrice(Float.parseFloat(productFrom.getPrice()));
}catch (NumberFormatException e){
}
return new ModelAndView("ProductDetails","product",product);
}
}
显示产品信息网页:ProductDetails.jsp
<! DOCTYPE HTML>
<html>
<head>
<title>Save Product</title>
<style type="text/css">@import url(css/main.css);</style>
</head>
<body>
<div id="global">
<h4>The Product has been Saved.</h4>
<p>
<h5>Details:</h5>
Product Name: ${product.name}<br/>
Description: ${product.description}<br/>
Price:${product.price}
</p>
</div>
</body>
</html>
输入产品信息网页:ProductForm.jsp
<! DOCTYPE HTML>
<html>
<head>
<title>Add Product Form</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>Add a Product</legend>
<label for="name">Product Name:</label>
<input type="text" id="name" name = "name" tabindex="1">
<label for="description">Des:</label>
<input type="text" id="description" name="description" tabindex="2">
<label for="price">Price:</label>
<input type="text" id="price" name="price" tabindex="3">
<div id="buttons">
<label for="dummy"></label>
<input id="reset" type="reset" tabindex="4">
<input id="submit" type="submit" tabindex="5" value="Add Product">
</div>
</fieldset>
</form>
</div>
</body>
</html>
依赖库