SpringMVC 3:注解配置快速入门

需要导入的包:

com.springsource.org.aopalliance-1.0.0.jar  
    spring-aop-3.2.5.RELEASE.jar  
    spring-aspects-3.2.5.RELEASE.jar  
    spring-beans-3.2.5.RELEASE.jar  
    spring-context-3.2.5.RELEASE.jar  
    spring-context-support-3.2.5.RELEASE.jar  
    spring-core-3.2.5.RELEASE.jar  
    spring-expression-3.2.5.RELEASE.jar  
    spring-web-3.2.5.RELEASE.jar  
    spring-webmvc-3.2.5.RELEASE.jar

Spring3.1之前和Spring3.2的解析器和适配器是不一样的

在spring3.1之前使用的注解映射器

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

在spring3.1之后使用的注解映射器。

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

在spring3.1之前使用的注解适配器

org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

在spring3.1之后使用的注解适配器

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

这里使用spring3.2只有的

建立springMVC的配置文件,配置注解驱动、注解需要扫描的包、内部资源视图解析器

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
			http://www.springframework.org/schema/mvc 
			http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
	<!-- 注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 自动扫描 -->
	<context:component-scan base-package="controller"></context:component-scan>
	<!-- 内部资源视图解析器 -->
	<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/page/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

 在web.xml中配置当容器启动时加载该文件

<servlet>
    <servlet-name>defaultServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMVC-annotation.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>defaultServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

需要用到的注解

@Controller :生命这个类是一个Controller

@RequestMapping:url路径映射,可以配置多个

建立Controller类,不需要继承任何类

@Controller
public class AnnoController {
	@RequestMapping(value={"a1.do","a2.do","/"})
	public String method(){
		System.out.println("AnnoController method()");
		return "springMVC";
	}
}

 RUL访问路径分别为:

http://127.0.0.1:8080/SpringMVC/a1.do

http://127.0.0.1:8080/SpringMVC/a2.do

http://127.0.0.1:8080/SpringMVC/.do

“/“访问方式是.do要有扩展名,Servlet拦截是需要扩展名

View和Controller的参数传递

第一种使用Map,需要在定义的方法内使用Map,jsp页面通过el直接获取

@RequestMapping(value = { "p" })
	public String param(Map<String, Object> model) {
		System.out.println("AnnoController param()");
		Person p = new Person(1, 17, "lisi");
		model.put("p", p);
		return "springMVC";
	}

 第二种使用Model对象,页面使用<c:forEach items="${personList}" var="p">,命名规则是entiti名+List

@RequestMapping(value = { "p2" })
	public String param(Model model) {
		System.out.println("AnnoController param()");
		Person p = new Person(2, 27, "wangwu");
		List<Person> list = new ArrayList<Person>();
		list.add(p);
		model.addAttribute(list);
		return "springMVC";
	}

相关推荐