Spring利用扫描机制来找到应用程序所有基于注解的控制器。
1.配置文件中声明:
xmlns:context="http://www.springframework.org/schema/context";---------自注解命名空间
2. 指定控制器的基本包:
<context:component-scan base-package="packageName"/> ;
例:
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true"> <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>/</url-pattern> </servlet-mapping> </web-app>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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <context:component-scan base-package="controller"/> <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>应用:
1.目录结构:
2.web.xml 与config见上面例子;
3.Controller类:
package controller; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import domian.Product; import form.ProductForm; @Controller public class ProductController { private static final Log logger=LogFactory.getLog(ProductController.class); //添加商品 @RequestMapping(value="/add2") public String addForm() { logger.info("inputProduct called!!!"); return "FormProduct"; } //保存商品 @RequestMapping(value="/saved2") public String savedProduct(ProductForm productform,Model model) { Product product = null; try { logger.info("savedProduct called"); product=new Product(productform.getName(), productform.getDescription(), Double.parseDouble(productform.getPrice())); } catch (Exception e) { // TODO: handle exception } model.addAttribute("product", product); return "savedProduct"; } }//domain
4.Product类:
package domian; public class Product { private String name; private String description; private double price; public Product(String name, String description, double price) { super(); this.name = name; this.description = description; this.price = 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 double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }5.ProductForm类
package form; public class ProductForm { private String name; private String description; private String price; public ProductForm() { super(); } public ProductForm(String name, String description, String price) { super(); this.name = name; this.description = description; this.price = 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; } }View:
6.产品添加表单:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="saved2"> <fieldset> <legend>Add a product</legend> <table> <!-- 商品名称填写--> <tr> <td>Product Name:</td> <td><input type="text" name="name"/></td> </tr> <!-- 商品说明 --> <tr> <td>Product Description:</td> <td><input type="text" name="description"></td> </tr> <!-- 商品价格--> <tr> <td>Product price:</td> <td><input type="text" name="price"></td> </tr> <!-- 按钮 --> <tr> <td><input type="reset"/> </td><td><input type="submit"/></td> </tr> </table> </fieldset> </form> </body> </html>6.产品保存:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <ul> <li>Product name:${product.name}</li> <li>Product description:${product.description }</li> <li>Product pricce:${product.price}</li> </ul> </body> </html>