Spring 注解MVC框架搭建

基于Spring的注解mvc框架搭建实现包括如下步骤:

1.引入相应的jar

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>3.1.0.RELEASE</version>
</dependency>

2.web.xml中加入ContextLoaderListener和IntrospectorCleanupListener以及contextConfigLocation

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
	<listener-class>
		org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/spring-*.xml</param-value>
</context-param>

3.web.xml中加入springServlet以及编码

<servlet>
	<servlet-name>spring</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>spring</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
	<filter-name>encodingFilter</filter-name>
	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>utf-8</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>encodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

4.spring-*.xml部分包括包扫描以及view处理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/task   
    http://www.springframework.org/schema/task/spring-task-3.1.xsd">
	<mvc:annotation-driven>
		<mvc:message-converters>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
			<bean
				class="org.springframework.http.converter.ResourceHttpMessageConverter" />
			<bean
				class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
				<property name="supportedMediaTypes" value="application/json" />
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	<context:component-scan base-package="xxx.xxx.xxx.platform" />
	<bean id="propertyConfigure"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/jsp/" p:suffix=".jsp" />
 
</beans>

5.至此已经完成,控制层代码如下:

@Controller
@RequestMapping("api")
public class ApiController {
 
	@RequestMapping(value = "/show/{id}", method = RequestMethod.GET)
	@ResponseBody
	public List<String> show(@PathVariable String id,
	        @RequestParam(value = "id", required = false) String uid) {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			list.add(id + i);
		}
		return list;
	}

	 
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public String delete(@PathVariable String id,
	        @RequestParam(value = "id", required = false) String uid) {

		return id + " is delete!";
	}
}

 

 

 

你可能感兴趣的:(spring)