1、目录结构
2、部署描述符文件和SpringMVC配置文件
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_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>/</url-pattern>
</servlet-mapping>
</web-app>
告诉Servlet容器,我们将使用Spring MVC的Dispatcher Servlet,并配置url-pattern元素为“/”,意思是将所有的URL映射到该serlvet
Spring MVC配置文件
<?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
">
<!--
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
-->
<!-- <context:component-scan base-package="controller" /> -->
<bean name="/product_input.action" class="controller.InputProductController" />
<bean name="/product_save.action" class="controller.SaveProductController" />
</beans>
声明了两个控制器类InputProductController和SaveProductController,分别映射product_input.action和product_save.action
3、Controller
InputProductController
/*
* handleRequest方法只是返回ModelAndView,包含一个视图,
* 且没有模型,该请求被转发到/WEB-INF/jsp/ProductForm.jsp
*/
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("/WEB-INF/jsp/ProductForm.jsp");
}
}
只是返回视图,没有模型
SaveProductController
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 productForm = new ProductForm();
productForm.setName(request.getParameter("name"));
productForm.setDescription(request.getParameter("description"));
productForm.setPrice(request.getParameter("price"));
Product product = new Product();
product.setName(productForm.getName());
product.setDescription(productForm.getDescription());
product.setPrice(Float.parseFloat(productForm.getPrice()));
return new ModelAndView("/WEB-INF/jsp/ProductDetails.jsp", "product",
product);
}
}
在handleRequest方法中,使用请求参数创建一个ProductForm对象,然后根据ProductForm对象创建Product对象,最后返回ModelAndView模型包含了视图的路径、模型名称以及模型(product模型),该模型将目标视图,用于界面显示。
4、View
ProductForm.jsp
<html>
<head>
<title>Add Product Form</title>
<!-- <style type="text/css"@imput url(css/main.css);</style> -->
</head>
<body>
<div id="global">
<form action="product_save.action" method="post">
<fieldset>
<legend>Add Product Form</legend>
<label for="name">Product Name:</label> <input type="text" id="name"
name="name" value="" tabindex="1"> <label for="description">Description:</label>
<input type="text" id="description" name="description" value=""
tabindex="2"> <label for="price">Price:</label> <input
type="text" id="price" name="price" value="" 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>
ProductDetails.jsp
<html>
<head>
<title>Save Product</title>
<!-- <style type="text/css">@imput 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>
5、运行结果
输入http://jyx:8080/app17a/product_input.action
6、加入视图解析器View Resovler
SpringMVC的视图解析器负责解析视图,在配置文件springmvc-config.xml定义ViewResolver来配置视图解析器
<?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="controller.InputProductController" />
<bean name="/product_save.action" class="controller.SaveProductController" />
<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>
7、在部署描述符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_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>
<!--
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-->
</web-app>
然后在两个控制器类返回ModelAndView模型时,去掉路径,只声明页面名字就行
return new ModelAndView("ProductForm");
return new ModelAndView("ProductDetails", "product",
product);
输入http://jyx:8080/app17a/product_input.action
同样运行结果
把上面的例子改为基于注解形式
使用注解的控制器有几个优点:
1、一个控制器类可以处理很多动作(如果用实现Controller接口的控制器,它只能处理一个动作,上面的例子就是这样,处理输入一个控制器,输出结果一个控制器),这样就减少了类的数量
2、请求映射不需要存储在配置文件,使用RequestMapping注解类型,对一个方法进行请求处理,也就把
<bean name="/product_input.action" class="controller.InputProductController" />
去掉,在方法使用注解RequestMapping
@RequestMapping(value = "/product_input")
我们还需要在配置文件添加用于扫描目标包的类,和注册用于支持注解的
<!-- 扫描包 -->
<context:component-scan base-package="controller"></context:component-scan>
<!-- 支持注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
下面完整代码
使用注解后的Controller类
@Controller
public class ProductController {
private static final Log logger = LogFactory
.getLog(ProductController.class);
@RequestMapping(value = "/product_input")
public String inputProduct() {
logger.info("inputProduct called");
return "ProductForm";
}
@RequestMapping(value = "/product_save")
public String saveProduct(ProductForm productForm, Model model) {
logger.info("saveProduct called");
Product product=new Product();
product.setName(productForm.getName());
product.setDescription(productForm.getDescription());
product.setPrice(Float.parseFloat(productForm.getPrice()));
//在model添加属性product
model.addAttribute("product",product);
return "ProductDetails";
}
}
配置文件springmvc-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 扫描包 -->
<context:component-scan base-package="controller"></context:component-scan>
<!-- 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<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>
web.xml文件不变,不过
如果是
<url-pattern>/</url-pattern>
访问路径是http://jyx:8080/app17a/product_input
如果是
<url-pattern>*.action</url-pattern>
访问路径是http://jyx:8080/app17a/product_input.action
同样运行结果