第四章节-Spring MVC介绍(注解方式——一)

一、Spring MVC注解类型

优点:

1.一个控制器可以处理多个动作。(而一个实现了Controller类的控制器只能处理一个动作)。

2.请求映射不要存储在配置文件中,使用RequestMapping注解就可以对一个方法进行请求处理。

  • Controller注解类型

用于指定Spring类的实例是一个控制器。Spring使用扫描机制来找到应用程序中所有的基于注解的控制器。为了让Spring 可以找到控制器,我们要:

 在Spring MVC配置文件中声明spring context.先在xmlns中加入context。再

<context:component-scan base-package="basepackage"/>

basepackage是控制器类的基本包。

  • RequestMapping注解类型

在控制器内部为每一个动作做相应的处理方法。RequestMapping映射URI与方法。

RequestMapping的属性有value(默认),method .

value :URI映射到方法,如果只有value属性时,可以不写value,直接写URI .

method: method={RequestMethod.POST ,RequestMehtod.GET},method=RequestMethod.GET。

RequestMapping还可以用来注释一个控制器类。

二、编写请求处理的方法

每个请求处理的方法可以有不同类型的参数,和一个多种类型的返回结果。如我们要在方法中访问HttpSession参数,则可以添加HttpSession作为参数,Spring将会正确的

传递给方法。

请求处理方法中可以传递进去的参数

太多,略。在每次调用处理方法的时候,Spring MVC会创建一个Model对象 ,并将其Map注入到各种对象。

请求处理方法中可以返回的参数类型

ModelAndView.

Model

Map包含的属性

View

代表视图名的String 

void 

....

其他任意类型,Spring将其视作输出给View的对象模型。

三、例子

工程目录

  •  web.xml文件(部署描述符)
<?xml version="1.0" encoding="ISO-8859-1"?>
<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"
> 
	<!-- 	注意文件名一定是servletName-servlet -->
	<servlet>
	    <servlet-name>springmvc</servlet-name>
	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	    <!-- 当springmvc的配置文件不是默认情况时,要设置contextConfigLocation -->
	    <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>
	    <!-- map all request to this DispatcherServlet -->
	    <url-pattern>/</url-pattern>
	</servlet-mapping>  

</web-app>

springmvc-config.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans 
	xsi:schemaLocation=" http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:p="http://www.springframework.org/schema/p" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://www.springframework.org/schema/beans">
	
	<!-- 扫描controller -->
	<context:component-scan base-package="app04a.controller"></context:component-scan>
	<!-- 注册用于支持基于注解的控制器的请求处理方法的bean 对象 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 那些静态资源要单独处理 ,而不通过dispatcher servlet-->
	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
	<mvc:resources location="/" mapping="/*.html"/> 
	
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>

</beans>
  • 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");
		//no need to create and instantiate a ProductForm
		//这里对比原来的ProductDetailsController
		//这里不是很明白,数据怎么传递到productForm中的
		//create product
		Product product = new Product();
		product.setName(productForm.getName()); 
		product.setDescription(productForm.getDescription());
		try {
			product.setPrice(Float.parseFloat(productForm.getPrice()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//add product
		model.addAttribute("product" , product);
		return "ProductDetails";
	}
}

注意,无论是否会使用,Spring MVC都会在每一个请求处理方法被调用时创建一个Model对象 ,用于增加要显示在视图中的属性。  

 

 

原文地址:https://www.cnblogs.com/chuiyuan/p/4617477.html